Changeset 2410 in josm for trunk


Ignore:
Timestamp:
2009-11-08T15:19:50+01:00 (15 years ago)
Author:
jttt
Message:

Add clearId parameter to primitives copy constructor, replace some clearOsmId calls, throw exception if id is set after primitive was added to the dataset

Location:
trunk/src/org/openstreetmap/josm
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/UnGlueAction.java

    r2381 r2410  
    22package org.openstreetmap.josm.actions;
    33
     4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    5 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    66
    77import java.awt.event.ActionEvent;
     
    157157        cmds.add(new ChangeCommand(selectedNode, c));
    158158
    159         Node n = new Node(selectedNode);
    160         n.clearOsmId();
     159        Node n = new Node(selectedNode, true);
    161160
    162161        // If this wasn't called from menu, place it where the cursor is/was
     
    302301            if (originalNode == pushNode) {
    303302                // clone the node for all other ways
    304                 pushNode = new Node(pushNode);
    305                 pushNode.clearOsmId();
     303                pushNode = new Node(pushNode, true);
    306304                newNodes.add(pushNode);
    307305                cmds.add(new AddCommand(pushNode));
  • trunk/src/org/openstreetmap/josm/command/UndeletePrimitivesCommand.java

    r2070 r2410  
    66import java.util.ArrayList;
    77import java.util.Collection;
     8import java.util.List;
    89import java.util.logging.Logger;
    910
     
    2526
    2627    /** the node to undelete */
    27     private ArrayList<OsmPrimitive> toUndelete;
     28    private final List<OsmPrimitive> toUndelete = new ArrayList<OsmPrimitive>();
    2829
    29     protected UndeletePrimitivesCommand() {
    30         toUndelete = new ArrayList<OsmPrimitive>();
    31     }
    3230    /**
    3331     * constructor
     
    3533     */
    3634    public UndeletePrimitivesCommand(OsmPrimitive node) {
    37         this();
    3835        toUndelete.add(node);
    3936    }
     
    4441     */
    4542    public UndeletePrimitivesCommand(OsmPrimitive ... toUndelete) {
    46         this();
    4743        for (int i=0; i < toUndelete.length; i++) {
    4844            this.toUndelete.add(toUndelete[i]);
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r2405 r2410  
    6868
    6969    /**
     70     *
     71     * @param clone
     72     * @param clearId If true, set version to 0 and id to new unique value
     73     */
     74    public Node(Node clone, boolean clearId) {
     75        super(clone.getUniqueId(), true);
     76        cloneFrom(clone);
     77        if (clearId) {
     78            clearOsmId();
     79        }
     80    }
     81
     82    /**
    7083     * Create an identical clone of the argument (including the id)
    7184     */
    7285    public Node(Node clone) {
    73         super(clone.getUniqueId(), true);
    74         cloneFrom(clone);
     86        this(clone, false);
    7587    }
    7688
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r2407 r2410  
    378378     * @throws IllegalArgumentException thrown if id <= 0
    379379     * @throws IllegalArgumentException thrown if version <= 0
     380     * @throws DataIntegrityProblemException If id is changed and primitive was already added to the dataset
    380381     */
    381382    public void setOsmId(long id, int version) {
     
    384385        if (version <= 0)
    385386            throw new IllegalArgumentException(tr("Version > 0 expected. Got {0}.", version));
     387        if (dataSet != null && id != this.id)
     388            throw new DataIntegrityProblemException("Id cannot be changed after primitive was added to the dataset");
    386389        this.id = id;
    387390        this.version = version;
     
    391394    /**
    392395     * Clears the id and version known to the OSM API. The id and the version is set to 0.
    393      * incomplete is set to false.
     396     * incomplete is set to false. It's preferred to use copy constructor with clearId set to true instead
     397     * of calling this method.
    394398     *
    395399     * <strong>Caution</strong>: Do not use this method on primitives which are already added to a {@see DataSet}.
    396      * Ways and relations might already refer to the primitive and clearing the OSM ID
    397      * result in corrupt data.
    398      *
    399      * Here's an example use case:
    400      * <pre>
    401      *     // create a clone of an already existing node
    402      *     Node copy = new Node(otherExistingNode);
    403      *
    404      *     // reset the clones OSM id
    405      *     copy.clearOsmId();
    406      * </pre>
    407      *
     400     *
     401     * @throws DataIntegrityProblemException If primitive was already added to the dataset
    408402     */
    409403    public void clearOsmId() {
     404        if (dataSet != null)
     405            throw new DataIntegrityProblemException("Method cannot be called after primitive was added to the dataset");
    410406        this.id = generateUniqueId();
    411407        this.version = 0;
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r2407 r2410  
    140140    }
    141141
    142     /**
    143      * Create an identical clone of the argument (including the id)
    144      */
    145     public Relation(Relation clone) {
     142    public Relation(Relation clone, boolean clearId) {
    146143        super(clone.getUniqueId(), true);
    147144        cloneFrom(clone);
     145        if (clearId) {
     146            clearOsmId();
     147        }
     148    }
     149
     150    /**
     151     * Create an identical clone of the argument (including the id)
     152     */
     153    public Relation(Relation clone) {
     154        this(clone, false);
    148155    }
    149156
  • trunk/src/org/openstreetmap/josm/data/osm/Way.java

    r2407 r2410  
    149149
    150150    /**
    151      * Create an identical clone of the argument (including the id).
    152      *
    153      * @param original  the original way. Must not be null.
    154      */
    155     public Way(Way original) {
     151     *
     152     * @param original
     153     * @param clearId
     154     */
     155    public Way(Way original, boolean clearId) {
    156156        super(original.getUniqueId(), true);
    157157        cloneFrom(original);
     158        if (clearId) {
     159            clearOsmId();
     160        }
     161    }
     162
     163    /**
     164     * Create an identical clone of the argument (including the id).
     165     *
     166     * @param original  the original way. Must not be null.
     167     */
     168    public Way(Way original) {
     169        this(original, false);
    158170    }
    159171
  • trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java

    r2402 r2410  
    471471
    472472        public void launchEditorForDuplicate(Relation original) {
    473             Relation copy = new Relation(original.getId());
    474             copy.cloneFrom(original);
    475             copy.clearOsmId();
     473            Relation copy = new Relation(original, true);
    476474            copy.setModified(true);
    477475            RelationEditor editor = RelationEditor.getEditor(
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r2398 r2410  
    9292
    9393        public void copyTo(OsmPrimitive osm) {
    94             //  id < 0 possible if read from a file
    95             if (id <= 0) {
    96                 osm.clearOsmId();
    97             } else {
     94            // It's not necessary to call clearOsmId for id < 0. The same thing is done by parameterless constructor
     95            if (id > 0) {
    9896                osm.setOsmId(id, version);
    9997            }
Note: See TracChangeset for help on using the changeset viewer.