Changeset 7675 in josm for trunk/src


Ignore:
Timestamp:
2014-10-29T21:28:16+01:00 (9 years ago)
Author:
Don-vip
Message:

see #10680 - detect invalid delete commands earlier

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/command/DeleteCommand.java

    r7024 r7675  
    4444/**
    4545 * A command to delete a number of primitives from the dataset.
    46  *
     46 * @since 23
    4747 */
    4848public class DeleteCommand extends Command {
     
    6060     */
    6161    public DeleteCommand(Collection<? extends OsmPrimitive> data) throws IllegalArgumentException {
    62         if (data == null)
    63             throw new IllegalArgumentException("Parameter 'data' must not be empty");
     62        CheckParameterUtil.ensureParameterNotNull(data, "data");
    6463        if (data.isEmpty())
    6564            throw new IllegalArgumentException(tr("At least one object to delete required, got empty collection"));
    6665        this.toDelete = data;
     66        checkConsistency();
    6767    }
    6868
     
    7474     */
    7575    public DeleteCommand(OsmPrimitive data) throws IllegalArgumentException {
    76         CheckParameterUtil.ensureParameterNotNull(data, "data");
    77         this.toDelete = Collections.singleton(data);
     76        this(Collections.singleton(data));
    7877    }
    7978
     
    8887     */
    8988    public DeleteCommand(OsmDataLayer layer, OsmPrimitive data) throws IllegalArgumentException {
    90         super(layer);
    91         CheckParameterUtil.ensureParameterNotNull(data, "data");
    92         this.toDelete = Collections.singleton(data);
     89        this(layer, Collections.singleton(data));
    9390    }
    9491
     
    104101    public DeleteCommand(OsmDataLayer layer, Collection<? extends OsmPrimitive> data) throws IllegalArgumentException{
    105102        super(layer);
    106         if (data == null)
    107             throw new IllegalArgumentException("Parameter 'data' must not be empty");
     103        CheckParameterUtil.ensureParameterNotNull(data, "data");
    108104        if (data.isEmpty())
    109105            throw new IllegalArgumentException(tr("At least one object to delete required, got empty collection"));
    110106        this.toDelete = data;
     107        checkConsistency();
     108    }
     109
     110    private void checkConsistency() {
     111        for (OsmPrimitive p : toDelete) {
     112            if (p == null) {
     113                throw new IllegalArgumentException("Primitive to delete must not be null");
     114            } else if (p.getDataSet() == null) {
     115                throw new IllegalArgumentException("Primitive to delete must be in a dataset");
     116            }
     117        }
    111118    }
    112119
     
    231238     * Delete the primitives and everything they reference.
    232239     *
    233      * If a node is deleted, the node and all ways and relations the node is part of are deleted as
    234      * well.
    235      *
     240     * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
    236241     * If a way is deleted, all relations the way is member of are also deleted.
    237      *
    238242     * If a way is deleted, only the way and no nodes are deleted.
    239243     *
     
    257261    }
    258262
     263    /**
     264     * Delete the primitives and everything they reference.
     265     *
     266     * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
     267     * If a way is deleted, all relations the way is member of are also deleted.
     268     * If a way is deleted, only the way and no nodes are deleted.
     269     *
     270     * @param layer the {@link OsmDataLayer} in whose context primitives are deleted. Must not be null.
     271     * @param selection The list of all object to be deleted.
     272     * @return command A command to perform the deletions, or null of there is nothing to delete.
     273     * @throws IllegalArgumentException thrown if layer is null
     274     */
    259275    public static Command deleteWithReferences(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection) {
    260276        return deleteWithReferences(layer, selection, false);
    261277    }
    262278
     279    /**
     280     * Try to delete all given primitives.
     281     *
     282     * If a node is used by a way, it's removed from that way. If a node or a way is used by a
     283     * relation, inform the user and do not delete.
     284     *
     285     * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
     286     * they are part of a relation, inform the user and do not delete.
     287     *
     288     * @param layer the {@link OsmDataLayer} in whose context the primitives are deleted
     289     * @param selection the objects to delete.
     290     * @return command a command to perform the deletions, or null if there is nothing to delete.
     291     */
    263292    public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection) {
    264293        return delete(layer, selection, true, false);
  • trunk/src/org/openstreetmap/josm/data/osm/Way.java

    r7587 r7675  
    285285            WayData wayData = (WayData) data;
    286286
     287            if (!wayData.getNodes().isEmpty() && getDataSet() == null) {
     288                throw new AssertionError("Data consistency problem - way without dataset detected");
     289            }
     290
    287291            List<Node> newNodes = new ArrayList<>(wayData.getNodes().size());
    288292            for (Long nodeId : wayData.getNodes()) {
     
    290294                if (node != null) {
    291295                    newNodes.add(node);
    292                 } else
     296                } else {
    293297                    throw new AssertionError("Data consistency problem - way with missing node detected");
     298                }
    294299            }
    295300            setNodes(newNodes);
Note: See TracChangeset for help on using the changeset viewer.