source: josm/trunk/src/org/openstreetmap/josm/command/DeleteCommand.java@ 13165

Last change on this file since 13165 was 12869, checked in by Don-vip, 7 years ago

see #15229 - see #15182 - SonarQube - squid:S2444 - make static fields volatile

  • Property svn:eol-style set to native
File size: 27.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.EnumSet;
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.Map;
18import java.util.Map.Entry;
19import java.util.Objects;
20import java.util.Set;
21
22import javax.swing.Icon;
23
24import org.openstreetmap.josm.data.osm.DataSet;
25import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
26import org.openstreetmap.josm.data.osm.Node;
27import org.openstreetmap.josm.data.osm.OsmPrimitive;
28import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
29import org.openstreetmap.josm.data.osm.PrimitiveData;
30import org.openstreetmap.josm.data.osm.Relation;
31import org.openstreetmap.josm.data.osm.RelationToChildReference;
32import org.openstreetmap.josm.data.osm.Way;
33import org.openstreetmap.josm.data.osm.WaySegment;
34import org.openstreetmap.josm.gui.layer.OsmDataLayer;
35import org.openstreetmap.josm.tools.CheckParameterUtil;
36import org.openstreetmap.josm.tools.ImageProvider;
37import org.openstreetmap.josm.tools.Utils;
38
39/**
40 * A command to delete a number of primitives from the dataset.
41 * To be used correctly, this class requires an initial call to {@link #setDeletionCallback(DeletionCallback)} to
42 * allow interactive confirmation actions.
43 * @since 23
44 */
45public class DeleteCommand extends Command {
46 private static final class DeleteChildCommand implements PseudoCommand {
47 private final OsmPrimitive osm;
48
49 private DeleteChildCommand(OsmPrimitive osm) {
50 this.osm = osm;
51 }
52
53 @Override
54 public String getDescriptionText() {
55 return tr("Deleted ''{0}''", osm.getDisplayName(DefaultNameFormatter.getInstance()));
56 }
57
58 @Override
59 public Icon getDescriptionIcon() {
60 return ImageProvider.get(osm.getDisplayType());
61 }
62
63 @Override
64 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
65 return Collections.singleton(osm);
66 }
67
68 @Override
69 public String toString() {
70 return "DeleteChildCommand [osm=" + osm + ']';
71 }
72 }
73
74 /**
75 * Called when a deletion operation must be checked and confirmed by user.
76 * @since 12749
77 */
78 public interface DeletionCallback {
79 /**
80 * Check whether user is about to delete data outside of the download area.
81 * Request confirmation if he is.
82 * @param primitives the primitives to operate on
83 * @param ignore {@code null} or a primitive to be ignored
84 * @return true, if operating on outlying primitives is OK; false, otherwise
85 */
86 boolean checkAndConfirmOutlyingDelete(Collection<? extends OsmPrimitive> primitives, Collection<? extends OsmPrimitive> ignore);
87
88 /**
89 * Confirm before deleting a relation, as it is a common newbie error.
90 * @param relations relation to check for deletion
91 * @return {@code true} if user confirms the deletion
92 * @since 12760
93 */
94 boolean confirmRelationDeletion(Collection<Relation> relations);
95
96 /**
97 * Confirm before removing a collection of primitives from their parent relations.
98 * @param references the list of relation-to-child references
99 * @return {@code true} if user confirms the deletion
100 * @since 12763
101 */
102 boolean confirmDeletionFromRelation(Collection<RelationToChildReference> references);
103 }
104
105 private static volatile DeletionCallback callback;
106
107 /**
108 * Sets the global {@link DeletionCallback}.
109 * @param deletionCallback the new {@code DeletionCallback}. Must not be null
110 * @throws NullPointerException if {@code deletionCallback} is null
111 * @since 12749
112 */
113 public static void setDeletionCallback(DeletionCallback deletionCallback) {
114 callback = Objects.requireNonNull(deletionCallback);
115 }
116
117 /**
118 * The primitives that get deleted.
119 */
120 private final Collection<? extends OsmPrimitive> toDelete;
121 private final Map<OsmPrimitive, PrimitiveData> clonedPrimitives = new HashMap<>();
122
123 /**
124 * Constructor. Deletes a collection of primitives in the current edit layer.
125 *
126 * @param data the primitives to delete. Must neither be null nor empty, and belong to a data set
127 * @throws IllegalArgumentException if data is null or empty
128 */
129 public DeleteCommand(Collection<? extends OsmPrimitive> data) {
130 this(data.iterator().next().getDataSet(), data);
131 }
132
133 /**
134 * Constructor. Deletes a single primitive in the current edit layer.
135 *
136 * @param data the primitive to delete. Must not be null.
137 * @throws IllegalArgumentException if data is null
138 */
139 public DeleteCommand(OsmPrimitive data) {
140 this(Collections.singleton(data));
141 }
142
143 /**
144 * Constructor for a single data item. Use the collection constructor to delete multiple objects.
145 *
146 * @param layer the layer context for deleting this primitive. Must not be null.
147 * @param data the primitive to delete. Must not be null.
148 * @throws IllegalArgumentException if data is null
149 * @throws IllegalArgumentException if layer is null
150 * @deprecated to be removed end of 2017. Use {@link #DeleteCommand(DataSet, OsmPrimitive)} instead
151 */
152 @Deprecated
153 public DeleteCommand(OsmDataLayer layer, OsmPrimitive data) {
154 this(layer, Collections.singleton(data));
155 }
156
157 /**
158 * Constructor for a single data item. Use the collection constructor to delete multiple objects.
159 *
160 * @param dataset the data set context for deleting this primitive. Must not be null.
161 * @param data the primitive to delete. Must not be null.
162 * @throws IllegalArgumentException if data is null
163 * @throws IllegalArgumentException if layer is null
164 * @since 12718
165 */
166 public DeleteCommand(DataSet dataset, OsmPrimitive data) {
167 this(dataset, Collections.singleton(data));
168 }
169
170 /**
171 * Constructor for a collection of data to be deleted in the context of a specific layer
172 *
173 * @param layer the layer context for deleting these primitives. Must not be null.
174 * @param data the primitives to delete. Must neither be null nor empty.
175 * @throws IllegalArgumentException if layer is null
176 * @throws IllegalArgumentException if data is null or empty
177 * @deprecated to be removed end of 2017. Use {@link #DeleteCommand(DataSet, Collection)} instead
178 */
179 @Deprecated
180 public DeleteCommand(OsmDataLayer layer, Collection<? extends OsmPrimitive> data) {
181 super(layer);
182 CheckParameterUtil.ensureParameterNotNull(data, "data");
183 this.toDelete = data;
184 checkConsistency();
185 }
186
187 /**
188 * Constructor for a collection of data to be deleted in the context of a specific data set
189 *
190 * @param dataset the dataset context for deleting these primitives. Must not be null.
191 * @param data the primitives to delete. Must neither be null nor empty.
192 * @throws IllegalArgumentException if dataset is null
193 * @throws IllegalArgumentException if data is null or empty
194 * @since 11240
195 */
196 public DeleteCommand(DataSet dataset, Collection<? extends OsmPrimitive> data) {
197 super(dataset);
198 CheckParameterUtil.ensureParameterNotNull(data, "data");
199 this.toDelete = data;
200 checkConsistency();
201 }
202
203 private void checkConsistency() {
204 if (toDelete.isEmpty()) {
205 throw new IllegalArgumentException(tr("At least one object to delete required, got empty collection"));
206 }
207 for (OsmPrimitive p : toDelete) {
208 if (p == null) {
209 throw new IllegalArgumentException("Primitive to delete must not be null");
210 } else if (p.getDataSet() == null) {
211 throw new IllegalArgumentException("Primitive to delete must be in a dataset");
212 }
213 }
214 }
215
216 @Override
217 public boolean executeCommand() {
218 ensurePrimitivesAreInDataset();
219 // Make copy and remove all references (to prevent inconsistent dataset (delete referenced) while command is executed)
220 for (OsmPrimitive osm: toDelete) {
221 if (osm.isDeleted())
222 throw new IllegalArgumentException(osm + " is already deleted");
223 clonedPrimitives.put(osm, osm.save());
224
225 if (osm instanceof Way) {
226 ((Way) osm).setNodes(null);
227 } else if (osm instanceof Relation) {
228 ((Relation) osm).setMembers(null);
229 }
230 }
231
232 for (OsmPrimitive osm: toDelete) {
233 osm.setDeleted(true);
234 }
235
236 return true;
237 }
238
239 @Override
240 public void undoCommand() {
241 ensurePrimitivesAreInDataset();
242
243 for (OsmPrimitive osm: toDelete) {
244 osm.setDeleted(false);
245 }
246
247 for (Entry<OsmPrimitive, PrimitiveData> entry: clonedPrimitives.entrySet()) {
248 entry.getKey().load(entry.getValue());
249 }
250 }
251
252 @Override
253 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
254 // Do nothing
255 }
256
257 private EnumSet<OsmPrimitiveType> getTypesToDelete() {
258 EnumSet<OsmPrimitiveType> typesToDelete = EnumSet.noneOf(OsmPrimitiveType.class);
259 for (OsmPrimitive osm : toDelete) {
260 typesToDelete.add(OsmPrimitiveType.from(osm));
261 }
262 return typesToDelete;
263 }
264
265 @Override
266 public String getDescriptionText() {
267 if (toDelete.size() == 1) {
268 OsmPrimitive primitive = toDelete.iterator().next();
269 String msg;
270 switch(OsmPrimitiveType.from(primitive)) {
271 case NODE: msg = marktr("Delete node {0}"); break;
272 case WAY: msg = marktr("Delete way {0}"); break;
273 case RELATION:msg = marktr("Delete relation {0}"); break;
274 default: throw new AssertionError();
275 }
276
277 return tr(msg, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
278 } else {
279 Set<OsmPrimitiveType> typesToDelete = getTypesToDelete();
280 String msg;
281 if (typesToDelete.size() > 1) {
282 msg = trn("Delete {0} object", "Delete {0} objects", toDelete.size(), toDelete.size());
283 } else {
284 OsmPrimitiveType t = typesToDelete.iterator().next();
285 switch(t) {
286 case NODE: msg = trn("Delete {0} node", "Delete {0} nodes", toDelete.size(), toDelete.size()); break;
287 case WAY: msg = trn("Delete {0} way", "Delete {0} ways", toDelete.size(), toDelete.size()); break;
288 case RELATION: msg = trn("Delete {0} relation", "Delete {0} relations", toDelete.size(), toDelete.size()); break;
289 default: throw new AssertionError();
290 }
291 }
292 return msg;
293 }
294 }
295
296 @Override
297 public Icon getDescriptionIcon() {
298 if (toDelete.size() == 1)
299 return ImageProvider.get(toDelete.iterator().next().getDisplayType());
300 Set<OsmPrimitiveType> typesToDelete = getTypesToDelete();
301 if (typesToDelete.size() > 1)
302 return ImageProvider.get("data", "object");
303 else
304 return ImageProvider.get(typesToDelete.iterator().next());
305 }
306
307 @Override public Collection<PseudoCommand> getChildren() {
308 if (toDelete.size() == 1)
309 return null;
310 else {
311 List<PseudoCommand> children = new ArrayList<>(toDelete.size());
312 for (final OsmPrimitive osm : toDelete) {
313 children.add(new DeleteChildCommand(osm));
314 }
315 return children;
316
317 }
318 }
319
320 @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
321 return toDelete;
322 }
323
324 /**
325 * Delete the primitives and everything they reference.
326 *
327 * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
328 * If a way is deleted, all relations the way is member of are also deleted.
329 * If a way is deleted, only the way and no nodes are deleted.
330 *
331 * @param layer the {@link OsmDataLayer} in whose context primitives are deleted. Must not be null.
332 * @param selection The list of all object to be deleted.
333 * @param silent Set to true if the user should not be bugged with additional dialogs
334 * @return command A command to perform the deletions, or null of there is nothing to delete.
335 * @throws IllegalArgumentException if layer is null
336 * @deprecated to be removed end of 2017. Use {@link #deleteWithReferences(Collection, boolean)} instead
337 */
338 @Deprecated
339 public static Command deleteWithReferences(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection, boolean silent) {
340 return deleteWithReferences(selection, silent);
341 }
342
343 /**
344 * Delete the primitives and everything they reference.
345 *
346 * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
347 * If a way is deleted, all relations the way is member of are also deleted.
348 * If a way is deleted, only the way and no nodes are deleted.
349 *
350 * @param selection The list of all object to be deleted.
351 * @param silent Set to true if the user should not be bugged with additional dialogs
352 * @return command A command to perform the deletions, or null of there is nothing to delete.
353 * @throws IllegalArgumentException if layer is null
354 * @since 12718
355 */
356 public static Command deleteWithReferences(Collection<? extends OsmPrimitive> selection, boolean silent) {
357 if (selection == null || selection.isEmpty()) return null;
358 Set<OsmPrimitive> parents = OsmPrimitive.getReferrer(selection);
359 parents.addAll(selection);
360
361 if (parents.isEmpty())
362 return null;
363 if (!silent && !callback.checkAndConfirmOutlyingDelete(parents, null))
364 return null;
365 return new DeleteCommand(parents.iterator().next().getDataSet(), parents);
366 }
367
368 /**
369 * Delete the primitives and everything they reference.
370 *
371 * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
372 * If a way is deleted, all relations the way is member of are also deleted.
373 * If a way is deleted, only the way and no nodes are deleted.
374 *
375 * @param layer unused
376 * @param selection The list of all object to be deleted.
377 * @return command A command to perform the deletions, or null of there is nothing to delete.
378 * @throws IllegalArgumentException if layer is null
379 * @deprecated to be removed end of 2017. Use {@link #deleteWithReferences(Collection)} instead
380 */
381 @Deprecated
382 public static Command deleteWithReferences(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection) {
383 return deleteWithReferences(selection);
384 }
385
386 /**
387 * Delete the primitives and everything they reference.
388 *
389 * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
390 * If a way is deleted, all relations the way is member of are also deleted.
391 * If a way is deleted, only the way and no nodes are deleted.
392 *
393 * @param selection The list of all object to be deleted.
394 * @return command A command to perform the deletions, or null of there is nothing to delete.
395 * @throws IllegalArgumentException if layer is null
396 * @since 12718
397 */
398 public static Command deleteWithReferences(Collection<? extends OsmPrimitive> selection) {
399 return deleteWithReferences(selection, false);
400 }
401
402 /**
403 * Try to delete all given primitives.
404 *
405 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
406 * relation, inform the user and do not delete.
407 *
408 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
409 * they are part of a relation, inform the user and do not delete.
410 *
411 * @param layer unused
412 * @param selection the objects to delete.
413 * @return command a command to perform the deletions, or null if there is nothing to delete.
414 * @deprecated to be removed end of 2017. Use {@link #delete(Collection)} instead
415 */
416 @Deprecated
417 public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection) {
418 return delete(selection);
419 }
420
421 /**
422 * Try to delete all given primitives.
423 *
424 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
425 * relation, inform the user and do not delete.
426 *
427 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
428 * they are part of a relation, inform the user and do not delete.
429 *
430 * @param selection the objects to delete.
431 * @return command a command to perform the deletions, or null if there is nothing to delete.
432 * @since 12718
433 */
434 public static Command delete(Collection<? extends OsmPrimitive> selection) {
435 return delete(selection, true, false);
436 }
437
438 /**
439 * Replies the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
440 * can be deleted too. A node can be deleted if
441 * <ul>
442 * <li>it is untagged (see {@link Node#isTagged()}</li>
443 * <li>it is not referred to by other non-deleted primitives outside of <code>primitivesToDelete</code></li>
444 * </ul>
445 * @param primitivesToDelete the primitives to delete
446 * @return the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
447 * can be deleted too
448 */
449 protected static Collection<Node> computeNodesToDelete(Collection<OsmPrimitive> primitivesToDelete) {
450 Collection<Node> nodesToDelete = new HashSet<>();
451 for (Way way : OsmPrimitive.getFilteredList(primitivesToDelete, Way.class)) {
452 for (Node n : way.getNodes()) {
453 if (n.isTagged()) {
454 continue;
455 }
456 Collection<OsmPrimitive> referringPrimitives = n.getReferrers();
457 referringPrimitives.removeAll(primitivesToDelete);
458 int count = 0;
459 for (OsmPrimitive p : referringPrimitives) {
460 if (!p.isDeleted()) {
461 count++;
462 }
463 }
464 if (count == 0) {
465 nodesToDelete.add(n);
466 }
467 }
468 }
469 return nodesToDelete;
470 }
471
472 /**
473 * Try to delete all given primitives.
474 *
475 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
476 * relation, inform the user and do not delete.
477 *
478 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
479 * they are part of a relation, inform the user and do not delete.
480 *
481 * @param layer unused
482 * @param selection the objects to delete.
483 * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
484 * @return command a command to perform the deletions, or null if there is nothing to delete.
485 * @deprecated to be removed end of 2017. Use {@link #delete(Collection, boolean)} instead
486 */
487 @Deprecated
488 public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection,
489 boolean alsoDeleteNodesInWay) {
490 return delete(selection, alsoDeleteNodesInWay);
491 }
492
493 /**
494 * Try to delete all given primitives.
495 *
496 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
497 * relation, inform the user and do not delete.
498 *
499 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
500 * they are part of a relation, inform the user and do not delete.
501 *
502 * @param selection the objects to delete.
503 * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
504 * @return command a command to perform the deletions, or null if there is nothing to delete.
505 * @since 12718
506 */
507 public static Command delete(Collection<? extends OsmPrimitive> selection, boolean alsoDeleteNodesInWay) {
508 return delete(selection, alsoDeleteNodesInWay, false /* not silent */);
509 }
510
511 /**
512 * Try to delete all given primitives.
513 *
514 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
515 * relation, inform the user and do not delete.
516 *
517 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
518 * they are part of a relation, inform the user and do not delete.
519 *
520 * @param layer unused
521 * @param selection the objects to delete.
522 * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
523 * @param silent set to true if the user should not be bugged with additional questions
524 * @return command a command to perform the deletions, or null if there is nothing to delete.
525 * @deprecated to be removed end of 2017. Use {@link #delete(Collection, boolean, boolean)} instead
526 */
527 @Deprecated
528 public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection,
529 boolean alsoDeleteNodesInWay, boolean silent) {
530 return delete(selection, alsoDeleteNodesInWay, silent);
531 }
532
533 /**
534 * Try to delete all given primitives.
535 *
536 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
537 * relation, inform the user and do not delete.
538 *
539 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
540 * they are part of a relation, inform the user and do not delete.
541 *
542 * @param selection the objects to delete.
543 * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
544 * @param silent set to true if the user should not be bugged with additional questions
545 * @return command a command to perform the deletions, or null if there is nothing to delete.
546 * @since 12718
547 */
548 public static Command delete(Collection<? extends OsmPrimitive> selection, boolean alsoDeleteNodesInWay, boolean silent) {
549 if (selection == null || selection.isEmpty())
550 return null;
551
552 Set<OsmPrimitive> primitivesToDelete = new HashSet<>(selection);
553
554 Collection<Relation> relationsToDelete = Utils.filteredCollection(primitivesToDelete, Relation.class);
555 if (!relationsToDelete.isEmpty() && !silent && !callback.confirmRelationDeletion(relationsToDelete))
556 return null;
557
558 if (alsoDeleteNodesInWay) {
559 // delete untagged nodes only referenced by primitives in primitivesToDelete, too
560 Collection<Node> nodesToDelete = computeNodesToDelete(primitivesToDelete);
561 primitivesToDelete.addAll(nodesToDelete);
562 }
563
564 if (!silent && !callback.checkAndConfirmOutlyingDelete(
565 primitivesToDelete, Utils.filteredCollection(primitivesToDelete, Way.class)))
566 return null;
567
568 Collection<Way> waysToBeChanged = new HashSet<>(OsmPrimitive.getFilteredSet(OsmPrimitive.getReferrer(primitivesToDelete), Way.class));
569
570 Collection<Command> cmds = new LinkedList<>();
571 for (Way w : waysToBeChanged) {
572 Way wnew = new Way(w);
573 wnew.removeNodes(OsmPrimitive.getFilteredSet(primitivesToDelete, Node.class));
574 if (wnew.getNodesCount() < 2) {
575 primitivesToDelete.add(w);
576 } else {
577 cmds.add(new ChangeNodesCommand(w, wnew.getNodes()));
578 }
579 }
580
581 // get a confirmation that the objects to delete can be removed from their parent relations
582 //
583 if (!silent) {
584 Set<RelationToChildReference> references = RelationToChildReference.getRelationToChildReferences(primitivesToDelete);
585 references.removeIf(ref -> ref.getParent().isDeleted());
586 if (!references.isEmpty() && !callback.confirmDeletionFromRelation(references)) {
587 return null;
588 }
589 }
590
591 // remove the objects from their parent relations
592 //
593 for (Relation cur : OsmPrimitive.getFilteredSet(OsmPrimitive.getReferrer(primitivesToDelete), Relation.class)) {
594 Relation rel = new Relation(cur);
595 rel.removeMembersFor(primitivesToDelete);
596 cmds.add(new ChangeCommand(cur, rel));
597 }
598
599 // build the delete command
600 //
601 if (!primitivesToDelete.isEmpty()) {
602 cmds.add(new DeleteCommand(primitivesToDelete.iterator().next().getDataSet(), primitivesToDelete));
603 }
604
605 return new SequenceCommand(tr("Delete"), cmds);
606 }
607
608 /**
609 * Create a command that deletes a single way segment. The way may be split by this.
610 * @param layer unused
611 * @param ws The way segment that should be deleted
612 * @return A matching command to safely delete that segment.
613 * @deprecated to be removed end of 2017. Use {@link #deleteWaySegment(WaySegment)} instead
614 */
615 @Deprecated
616 public static Command deleteWaySegment(OsmDataLayer layer, WaySegment ws) {
617 return deleteWaySegment(ws);
618 }
619
620 /**
621 * Create a command that deletes a single way segment. The way may be split by this.
622 * @param ws The way segment that should be deleted
623 * @return A matching command to safely delete that segment.
624 * @since 12718
625 */
626 public static Command deleteWaySegment(WaySegment ws) {
627 if (ws.way.getNodesCount() < 3)
628 return delete(Collections.singleton(ws.way), false);
629
630 if (ws.way.isClosed()) {
631 // If the way is circular (first and last nodes are the same), the way shouldn't be splitted
632
633 List<Node> n = new ArrayList<>();
634
635 n.addAll(ws.way.getNodes().subList(ws.lowerIndex + 1, ws.way.getNodesCount() - 1));
636 n.addAll(ws.way.getNodes().subList(0, ws.lowerIndex + 1));
637
638 Way wnew = new Way(ws.way);
639 wnew.setNodes(n);
640
641 return new ChangeCommand(ws.way, wnew);
642 }
643
644 List<Node> n1 = new ArrayList<>();
645 List<Node> n2 = new ArrayList<>();
646
647 n1.addAll(ws.way.getNodes().subList(0, ws.lowerIndex + 1));
648 n2.addAll(ws.way.getNodes().subList(ws.lowerIndex + 1, ws.way.getNodesCount()));
649
650 Way wnew = new Way(ws.way);
651
652 if (n1.size() < 2) {
653 wnew.setNodes(n2);
654 return new ChangeCommand(ws.way, wnew);
655 } else if (n2.size() < 2) {
656 wnew.setNodes(n1);
657 return new ChangeCommand(ws.way, wnew);
658 } else {
659 return SplitWayCommand.splitWay(ws.way, Arrays.asList(n1, n2), Collections.<OsmPrimitive>emptyList());
660 }
661 }
662
663 @Override
664 public int hashCode() {
665 return Objects.hash(super.hashCode(), toDelete, clonedPrimitives);
666 }
667
668 @Override
669 public boolean equals(Object obj) {
670 if (this == obj) return true;
671 if (obj == null || getClass() != obj.getClass()) return false;
672 if (!super.equals(obj)) return false;
673 DeleteCommand that = (DeleteCommand) obj;
674 return Objects.equals(toDelete, that.toDelete) &&
675 Objects.equals(clonedPrimitives, that.clonedPrimitives);
676 }
677}
Note: See TracBrowser for help on using the repository browser.