Changeset 16212 in josm for trunk


Ignore:
Timestamp:
2020-03-28T18:22:51+01:00 (5 years ago)
Author:
Don-vip
Message:

fix #18993 - allow to clone a locked dataset

Location:
trunk
Files:
6 edited

Legend:

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

    r16187 r16212  
    211211            }
    212212            for (Way w : copyFrom.getWays()) {
    213                 Way newWay = new Way(w);
     213                Way newWay = new Way(w, false, false);
    214214                primMap.put(w, newWay);
    215215                List<Node> newNodes = new ArrayList<>();
     
    224224            Collection<Relation> relations = copyFrom.getRelations();
    225225            for (Relation r : relations) {
    226                 Relation newRelation = new Relation(r);
    227                 newRelation.setMembers(null);
     226                Relation newRelation = new Relation(r, false, false);
    228227                primMap.put(r, newRelation);
    229228                addPrimitive(newRelation);
    230229            }
    231230            for (Relation r : relations) {
    232                 Relation newRelation = (Relation) primMap.get(r);
    233                 newRelation.setMembers(r.getMembers().stream()
     231                ((Relation) primMap.get(r)).setMembers(r.getMembers().stream()
    234232                        .map(rm -> new RelationMember(rm.getRole(), primMap.get(rm.getMember())))
    235233                        .collect(Collectors.toList()));
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r16077 r16212  
    221221
    222222    @Override
    223     public void cloneFrom(OsmPrimitive osm) {
     223    public void cloneFrom(OsmPrimitive osm, boolean copyChildren) {
    224224        if (!(osm instanceof Node))
    225225            throw new IllegalArgumentException("Not a node: " + osm);
    226226        boolean locked = writeLock();
    227227        try {
    228             super.cloneFrom(osm);
     228            super.cloneFrom(osm, copyChildren);
    229229            setCoor(((Node) osm).getCoor());
    230230        } finally {
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r16077 r16212  
    883883     * @param other other primitive
    884884     */
    885     public void cloneFrom(OsmPrimitive other) {
     885    public final void cloneFrom(OsmPrimitive other) {
     886        cloneFrom(other, true);
     887    }
     888
     889    /**
     890     * Get and write all attributes from the parameter. Does not fire any listener, so
     891     * use this only in the data initializing phase
     892     * @param other other primitive
     893     * @param copyChildren whether to copy child primitives too
     894     * @since 16212
     895     */
     896    protected void cloneFrom(OsmPrimitive other, boolean copyChildren) {
    886897        // write lock is provided by subclasses
    887898        if (id != other.id && dataSet != null)
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r15820 r16212  
    199199     * @param clearMetadata If {@code true}, clears the OSM id and other metadata as defined by {@link #clearOsmMetadata}.
    200200     * If {@code false}, does nothing
    201      */
    202     public Relation(Relation clone, boolean clearMetadata) {
     201     * @param copyMembers whether to copy relation members too
     202     * @since 16212
     203     */
     204    public Relation(Relation clone, boolean clearMetadata, boolean copyMembers) {
    203205        super(clone.getUniqueId(), true);
    204         cloneFrom(clone);
     206        cloneFrom(clone, copyMembers);
    205207        if (clearMetadata) {
    206208            clearOsmMetadata();
    207209        }
     210    }
     211
     212    /**
     213     * Constructs an identical clone of the argument.
     214     * @param clone The relation to clone
     215     * @param clearMetadata If {@code true}, clears the OSM id and other metadata as defined by {@link #clearOsmMetadata}.
     216     * If {@code false}, does nothing
     217     */
     218    public Relation(Relation clone, boolean clearMetadata) {
     219        this(clone, clearMetadata, true);
    208220    }
    209221
     
    237249
    238250    @Override
    239     public void cloneFrom(OsmPrimitive osm) {
     251    public void cloneFrom(OsmPrimitive osm, boolean copyMembers) {
    240252        if (!(osm instanceof Relation))
    241253            throw new IllegalArgumentException("Not a relation: " + osm);
    242254        boolean locked = writeLock();
    243255        try {
    244             super.cloneFrom(osm);
    245             // It's not necessary to clone members as RelationMember class is immutable
    246             setMembers(((Relation) osm).getMembers());
     256            super.cloneFrom(osm, copyMembers);
     257            if (copyMembers) {
     258                // It's not necessary to clone members as RelationMember class is immutable
     259                setMembers(((Relation) osm).getMembers());
     260            }
    247261        } finally {
    248262            writeUnlock(locked);
  • trunk/src/org/openstreetmap/josm/data/osm/Way.java

    r15820 r16212  
    204204     * @param clearMetadata If {@code true}, clears the OSM id and other metadata as defined by {@link #clearOsmMetadata}.
    205205     * If {@code false}, does nothing
    206      * @since 2410
    207      */
    208     public Way(Way original, boolean clearMetadata) {
     206     * @param copyNodes whether to copy nodes too
     207     * @since 16212
     208     */
     209    public Way(Way original, boolean clearMetadata, boolean copyNodes) {
    209210        super(original.getUniqueId(), true);
    210         cloneFrom(original);
     211        cloneFrom(original, copyNodes);
    211212        if (clearMetadata) {
    212213            clearOsmMetadata();
    213214        }
     215    }
     216
     217    /**
     218     * Constructs a new {@code Way} from an existing {@code Way}.
     219     * @param original The original {@code Way} to be identically cloned. Must not be null
     220     * @param clearMetadata If {@code true}, clears the OSM id and other metadata as defined by {@link #clearOsmMetadata}.
     221     * If {@code false}, does nothing
     222     * @since 2410
     223     */
     224    public Way(Way original, boolean clearMetadata) {
     225        this(original, clearMetadata, true);
    214226    }
    215227
     
    286298
    287299    @Override
    288     public void cloneFrom(OsmPrimitive osm) {
     300    public void cloneFrom(OsmPrimitive osm, boolean copyNodes) {
    289301        if (!(osm instanceof Way))
    290302            throw new IllegalArgumentException("Not a way: " + osm);
    291303        boolean locked = writeLock();
    292304        try {
    293             super.cloneFrom(osm);
    294             Way otherWay = (Way) osm;
    295             setNodes(otherWay.getNodes());
     305            super.cloneFrom(osm, copyNodes);
     306            if (copyNodes) {
     307                setNodes(((Way) osm).getNodes());
     308            }
    296309        } finally {
    297310            writeUnlock(locked);
  • trunk/test/unit/org/openstreetmap/josm/data/osm/DataSetTest.java

    r15891 r16212  
    316316        new DataSet().mergeFrom(ds);
    317317    }
     318
     319    /**
     320     * Checks that a read-only dataset can be cloned.
     321     */
     322    @Test
     323    public void testCloneReadOnly() {
     324        DataSet ds = new DataSet();
     325        Node n1 = new Node(LatLon.SOUTH_POLE);
     326        Node n2 = new Node(LatLon.NORTH_POLE);
     327        ds.addPrimitive(n1);
     328        ds.addPrimitive(n2);
     329        Way w = new Way();
     330        w.setNodes(Arrays.asList(n1, n2));
     331        ds.addPrimitive(w);
     332        Relation r = new Relation();
     333        r.setMembers(Arrays.asList(new RelationMember(null, w)));
     334        ds.addPrimitive(r);
     335        ds.lock();
     336
     337        DataSet copy = new DataSet(ds);
     338
     339        assertEquals(4, copy.allPrimitives().size());
     340        assertTrue(copy.isLocked());
     341    }
    318342}
Note: See TracChangeset for help on using the changeset viewer.