Ignore:
Timestamp:
2009-11-08T15:19:50+01:00 (17 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/data/osm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.