Ignore:
Timestamp:
2017-05-03T16:58:33+02:00 (7 years ago)
Author:
michael2402
Message:

Move quad bucket store of dataset to separate class.

Location:
trunk/src/org/openstreetmap/josm/data/osm
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r12048 r12049  
    5555import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    5656import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    57 import org.openstreetmap.josm.tools.JosmRuntimeException;
    5857import org.openstreetmap.josm.tools.ListenerList;
    5958import org.openstreetmap.josm.tools.SubclassFilteredCollection;
     
    104103 * @author imi
    105104 */
    106 public final class DataSet implements Data, ProjectionChangeListener {
     105public final class DataSet extends QuadBucketPrimitiveStore implements Data, ProjectionChangeListener {
    107106
    108107    /**
     
    216215        try {
    217216            Map<OsmPrimitive, OsmPrimitive> primMap = new HashMap<>();
    218             for (Node n : copyFrom.nodes) {
     217            for (Node n : copyFrom.getNodes()) {
    219218                Node newNode = new Node(n);
    220219                primMap.put(n, newNode);
    221220                addPrimitive(newNode);
    222221            }
    223             for (Way w : copyFrom.ways) {
     222            for (Way w : copyFrom.getWays()) {
    224223                Way newWay = new Way(w);
    225224                primMap.put(w, newWay);
     
    233232            // Because relations can have other relations as members we first clone all relations
    234233            // and then get the cloned members
    235             for (Relation r : copyFrom.relations) {
     234            Collection<Relation> relations = copyFrom.getRelations();
     235            for (Relation r : relations) {
    236236                Relation newRelation = new Relation(r, r.isNew());
    237237                newRelation.setMembers(null);
     
    239239                addPrimitive(newRelation);
    240240            }
    241             for (Relation r : copyFrom.relations) {
     241            for (Relation r : relations) {
    242242                Relation newRelation = (Relation) primMap.get(r);
    243243                List<RelationMember> newMembers = new ArrayList<>();
     
    418418
    419419    /**
    420      * All nodes goes here, even when included in other data (ways etc). This enables the instant
    421      * conversion of the whole DataSet by iterating over this data structure.
    422      */
    423     private final QuadBuckets<Node> nodes = new QuadBuckets<>();
    424 
    425     /**
    426420     * Gets a filtered collection of primitives matching the given predicate.
    427421     * @param <T> The primitive type.
     
    443437    }
    444438
    445     /**
    446      * Searches for nodes in the given bounding box.
    447      * @param bbox the bounding box
    448      * @return List of nodes in the given bbox. Can be empty but not null
    449      */
     439    @Override
    450440    public List<Node> searchNodes(BBox bbox) {
    451441        lock.readLock().lock();
    452442        try {
    453             return nodes.search(bbox);
     443            return super.searchNodes(bbox);
    454444        } finally {
    455445            lock.readLock().unlock();
     
    458448
    459449    /**
    460      * Determines if the given node can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
    461      * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
    462      *
    463      * @param n The node to search
    464      * @return {@code true} if {@code n} ban be retrieved in this data set, {@code false} otherwise
    465      * @since 7501
    466      */
    467     public boolean containsNode(Node n) {
    468         return nodes.contains(n);
    469     }
    470 
    471     /**
    472      * All ways (Streets etc.) in the DataSet.
    473      *
    474      * The way nodes are stored only in the way list.
    475      */
    476     private final QuadBuckets<Way> ways = new QuadBuckets<>();
    477 
    478     /**
    479450     * Replies an unmodifiable collection of ways in this dataset
    480451     *
     
    485456    }
    486457
    487     /**
    488      * Searches for ways in the given bounding box.
    489      * @param bbox the bounding box
    490      * @return List of ways in the given bbox. Can be empty but not null
    491      */
     458    @Override
    492459    public List<Way> searchWays(BBox bbox) {
    493460        lock.readLock().lock();
    494461        try {
    495             return ways.search(bbox);
     462            return super.searchWays(bbox);
    496463        } finally {
    497464            lock.readLock().unlock();
    498465        }
    499     }
    500 
    501     /**
    502      * Determines if the given way can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
    503      * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
    504      *
    505      * @param w The way to search
    506      * @return {@code true} if {@code w} ban be retrieved in this data set, {@code false} otherwise
    507      * @since 7501
    508      */
    509     public boolean containsWay(Way w) {
    510         return ways.contains(w);
    511     }
    512 
    513     /**
    514      * All relations/relationships
    515      */
    516     private final Collection<Relation> relations = new ArrayList<>();
    517 
    518     /**
    519      * Replies an unmodifiable collection of relations in this dataset
    520      *
    521      * @return an unmodifiable collection of relations in this dataset
    522      */
    523     public Collection<Relation> getRelations() {
    524         return getPrimitives(Relation.class::isInstance);
    525466    }
    526467
     
    530471     * @return List of relations in the given bbox. Can be empty but not null
    531472     */
     473    @Override
    532474    public List<Relation> searchRelations(BBox bbox) {
    533475        lock.readLock().lock();
    534476        try {
    535             // QuadBuckets might be useful here (don't forget to do reindexing after some of rm is changed)
    536             return relations.stream()
    537                     .filter(r -> r.getBBox().intersects(bbox))
    538                     .collect(Collectors.toList());
     477            return super.searchRelations(bbox);
    539478        } finally {
    540479            lock.readLock().unlock();
     
    543482
    544483    /**
    545      * Determines if the given relation can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
    546      * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
    547      *
    548      * @param r The relation to search
    549      * @return {@code true} if {@code r} ban be retrieved in this data set, {@code false} otherwise
    550      * @since 7501
    551      */
    552     public boolean containsRelation(Relation r) {
    553         return relations.contains(r);
     484     * Replies an unmodifiable collection of relations in this dataset
     485     *
     486     * @return an unmodifiable collection of relations in this dataset
     487     */
     488    public Collection<Relation> getRelations() {
     489        return getPrimitives(Relation.class::isInstance);
    554490    }
    555491
     
    605541     * @param primitive the primitive.
    606542     */
     543    @Override
    607544    public void addPrimitive(OsmPrimitive primitive) {
    608545        Objects.requireNonNull(primitive, "primitive");
     
    616553            primitive.setDataset(this);
    617554            primitive.updatePosition(); // Set cached bbox for way and relation (required for reindexWay and reindexRelation to work properly)
    618             boolean success = false;
    619             if (primitive instanceof Node) {
    620                 success = nodes.add((Node) primitive);
    621             } else if (primitive instanceof Way) {
    622                 success = ways.add((Way) primitive);
    623             } else if (primitive instanceof Relation) {
    624                 success = relations.add((Relation) primitive);
    625             }
    626             if (!success)
    627                 throw new JosmRuntimeException("failed to add primitive: "+primitive);
     555            super.addPrimitive(primitive);
    628556            firePrimitivesAdded(Collections.singletonList(primitive), false);
    629557        } finally {
     
    647575            if (primitive == null)
    648576                return;
    649             boolean success = false;
    650             if (primitive instanceof Node) {
    651                 success = nodes.remove(primitive);
    652             } else if (primitive instanceof Way) {
    653                 success = ways.remove(primitive);
    654             } else if (primitive instanceof Relation) {
    655                 success = relations.remove(primitive);
    656             }
    657             if (!success)
    658                 throw new JosmRuntimeException("failed to remove primitive: "+primitive);
     577            super.removePrimitive(primitive);
    659578            clearSelection(primitiveId);
    660579            allPrimitives.remove(primitive);
     
    11241043        beginUpdate();
    11251044        try {
    1126             for (Relation relation : relations) {
     1045            for (Relation relation : getRelations()) {
    11271046                List<RelationMember> members = relation.getMembers();
    11281047
     
    11811100        }
    11821101        return false;
    1183     }
    1184 
    1185     private void reindexNode(Node node, LatLon newCoor, EastNorth eastNorth) {
    1186         if (!nodes.remove(node))
    1187             throw new JosmRuntimeException("Reindexing node failed to remove");
    1188         node.setCoorInternal(newCoor, eastNorth);
    1189         if (!nodes.add(node))
    1190             throw new JosmRuntimeException("Reindexing node failed to add");
    1191         for (OsmPrimitive primitive: node.getReferrers()) {
    1192             if (primitive instanceof Way) {
    1193                 reindexWay((Way) primitive);
    1194             } else {
    1195                 reindexRelation((Relation) primitive);
    1196             }
    1197         }
    1198     }
    1199 
    1200     private void reindexWay(Way way) {
    1201         BBox before = way.getBBox();
    1202         if (!ways.remove(way))
    1203             throw new JosmRuntimeException("Reindexing way failed to remove");
    1204         way.updatePosition();
    1205         if (!ways.add(way))
    1206             throw new JosmRuntimeException("Reindexing way failed to add");
    1207         if (!way.getBBox().equals(before)) {
    1208             for (OsmPrimitive primitive: way.getReferrers()) {
    1209                 reindexRelation((Relation) primitive);
    1210             }
    1211         }
    1212     }
    1213 
    1214     private static void reindexRelation(Relation relation) {
    1215         BBox before = relation.getBBox();
    1216         relation.updatePosition();
    1217         if (!before.equals(relation.getBBox())) {
    1218             for (OsmPrimitive primitive: relation.getReferrers()) {
    1219                 reindexRelation((Relation) primitive);
    1220             }
    1221         }
    12221102    }
    12231103
     
    13721252        try {
    13731253            cleanupDeleted(Stream.concat(
    1374                     nodes.stream(), Stream.concat(ways.stream(), relations.stream())));
     1254                    getNodes().stream(), Stream.concat(getNodes().stream(), getNodes().stream())));
    13751255        } finally {
    13761256            endUpdate();
     
    13791259
    13801260    private void cleanupDeleted(Stream<? extends OsmPrimitive> it) {
    1381         clearSelection(it
    1382                 .filter(primitive -> primitive.isDeleted() && (!primitive.isVisible() || primitive.isNew()))
    1383                 .peek(allPrimitives::remove)
    1384                 .peek(primitive -> primitive.setDataset(null)));
     1261        it.filter(primitive -> primitive.isDeleted() && (!primitive.isVisible() || primitive.isNew()))
     1262                .collect(Collectors.toList())
     1263                .forEach(primitive -> this.removePrimitive(primitive.getPrimitiveId()));
     1264
    13851265    }
    13861266
     
    13881268     * Removes all primitives from the dataset and resets the currently selected primitives
    13891269     * to the empty collection. Also notifies selection change listeners if necessary.
    1390      *
    1391      */
     1270     */
     1271    @Override
    13921272    public void clear() {
    13931273        beginUpdate();
     
    13971277                primitive.setDataset(null);
    13981278            }
    1399             nodes.clear();
    1400             ways.clear();
    1401             relations.clear();
     1279            super.clear();
    14021280            allPrimitives.clear();
    14031281        } finally {
Note: See TracChangeset for help on using the changeset viewer.