source: josm/trunk/src/org/openstreetmap/josm/command/ChangeNodesCommand.java@ 13111

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

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.List;
8import java.util.Objects;
9
10import javax.swing.Icon;
11
12import org.openstreetmap.josm.data.osm.DataSet;
13import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.tools.ImageProvider;
19
20/**
21 * Command that changes the nodes list of a way.
22 * The same can be done with ChangeCommand, but this is more
23 * efficient. (Needed for the duplicate node fixing
24 * tool of the validator plugin, when processing large data sets.)
25 *
26 * @author Imi
27 */
28public class ChangeNodesCommand extends Command {
29
30 private final Way way;
31 private final List<Node> newNodes;
32
33 /**
34 * Constructs a new {@code ChangeNodesCommand}.
35 * @param way The way to modify
36 * @param newNodes The new list of nodes for the given way
37 */
38 public ChangeNodesCommand(Way way, List<Node> newNodes) {
39 this(way.getDataSet(), way, newNodes);
40 }
41
42 /**
43 * Constructs a new {@code ChangeNodesCommand}.
44 * @param ds The target data set. Must not be {@code null}
45 * @param way The way to modify
46 * @param newNodes The new list of nodes for the given way
47 * @since 12726
48 */
49 public ChangeNodesCommand(DataSet ds, Way way, List<Node> newNodes) {
50 super(ds);
51 this.way = way;
52 this.newNodes = newNodes;
53 if (newNodes.isEmpty()) {
54 throw new IllegalArgumentException("Cannot set nodes to be an empty list.");
55 }
56 }
57
58 @Override
59 public boolean executeCommand() {
60 super.executeCommand();
61 way.setNodes(newNodes);
62 way.setModified(true);
63 return true;
64 }
65
66 @Override
67 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
68 modified.add(way);
69 }
70
71 @Override
72 public String getDescriptionText() {
73 return tr("Change nodes of {0}", way.getDisplayName(DefaultNameFormatter.getInstance()));
74 }
75
76 @Override
77 public Icon getDescriptionIcon() {
78 return ImageProvider.get(OsmPrimitiveType.WAY);
79 }
80
81 @Override
82 public int hashCode() {
83 return Objects.hash(super.hashCode(), way, newNodes);
84 }
85
86 @Override
87 public boolean equals(Object obj) {
88 if (this == obj) return true;
89 if (obj == null || getClass() != obj.getClass()) return false;
90 if (!super.equals(obj)) return false;
91 ChangeNodesCommand that = (ChangeNodesCommand) obj;
92 return Objects.equals(way, that.way) &&
93 Objects.equals(newNodes, that.newNodes);
94 }
95}
Note: See TracBrowser for help on using the repository browser.