source: josm/trunk/src/org/openstreetmap/josm/command/AbstractNodesCommand.java@ 17335

Last change on this file since 17335 was 15013, checked in by Don-vip, 5 years ago

remove duplicated code

File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import java.util.Collection;
5import java.util.Objects;
6
7import javax.swing.Icon;
8
9import org.openstreetmap.josm.data.osm.DataSet;
10import org.openstreetmap.josm.data.osm.Node;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
13import org.openstreetmap.josm.data.osm.Way;
14import org.openstreetmap.josm.tools.ImageProvider;
15
16/**
17 * Abstracts superclass of {@link ChangeNodesCommand} / {@link RemoveNodesCommand}.
18 * @param <C> type of nodes collection used for this command
19 * @since 15013
20 */
21public abstract class AbstractNodesCommand<C extends Collection<Node>> extends Command {
22
23 protected final Way way;
24 protected final C cmdNodes;
25
26 /**
27 * Constructs a new {@code AbstractNodesCommand}.
28 * @param way The way to modify
29 * @param cmdNodes The collection of nodes for this command
30 */
31 protected AbstractNodesCommand(Way way, C cmdNodes) {
32 this(way.getDataSet(), way, cmdNodes);
33 }
34
35 /**
36 * Constructs a new {@code AbstractNodesCommand}.
37 * @param ds The target data set. Must not be {@code null}
38 * @param way The way to modify
39 * @param cmdNodes The collection of nodes for this command
40 */
41 protected AbstractNodesCommand(DataSet ds, Way way, C cmdNodes) {
42 super(ds);
43 this.way = Objects.requireNonNull(way, "way");
44 this.cmdNodes = Objects.requireNonNull(cmdNodes, "cmdNodes");
45 if (cmdNodes.isEmpty()) {
46 throw new IllegalArgumentException("Nodes collection is empty");
47 }
48 }
49
50 protected abstract void modifyWay();
51
52 @Override
53 public boolean executeCommand() {
54 super.executeCommand();
55 modifyWay();
56 way.setModified(true);
57 return true;
58 }
59
60 @Override
61 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
62 modified.add(way);
63 }
64
65 @Override
66 public Icon getDescriptionIcon() {
67 return ImageProvider.get(OsmPrimitiveType.WAY);
68 }
69
70 @Override
71 public int hashCode() {
72 return Objects.hash(super.hashCode(), way, cmdNodes);
73 }
74
75 @Override
76 public boolean equals(Object obj) {
77 if (this == obj) return true;
78 if (obj == null || getClass() != obj.getClass()) return false;
79 if (!super.equals(obj)) return false;
80 AbstractNodesCommand<?> that = (AbstractNodesCommand<?>) obj;
81 return Objects.equals(way, that.way) &&
82 Objects.equals(cmdNodes, that.cmdNodes);
83 }
84}
Note: See TracBrowser for help on using the repository browser.