source: josm/trunk/src/org/openstreetmap/josm/command/ChangeCommand.java@ 9870

Last change on this file since 9870 was 9371, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash

  • Property svn:eol-style set to native
File size: 3.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;
6
7import java.util.Collection;
8import java.util.Objects;
9
10import javax.swing.Icon;
11
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
14import org.openstreetmap.josm.data.osm.Way;
15import org.openstreetmap.josm.gui.DefaultNameFormatter;
16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
17import org.openstreetmap.josm.tools.CheckParameterUtil;
18import org.openstreetmap.josm.tools.ImageProvider;
19
20/**
21 * Command that basically replaces one OSM primitive by another of the same type.
22 *
23 * @since 93
24 */
25public class ChangeCommand extends Command {
26
27 private final OsmPrimitive osm;
28 private final OsmPrimitive newOsm;
29
30 /**
31 * Constructs a new {@code ChangeCommand} in the context of the current edit layer, if any.
32 * @param osm The existing primitive to modify
33 * @param newOsm The new primitive
34 */
35 public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
36 this.osm = osm;
37 this.newOsm = newOsm;
38 sanityChecks();
39 }
40
41 /**
42 * Constructs a new {@code ChangeCommand} in the context of a given data layer.
43 * @param layer The data layer
44 * @param osm The existing primitive to modify
45 * @param newOsm The new primitive
46 */
47 public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
48 super(layer);
49 this.osm = osm;
50 this.newOsm = newOsm;
51 sanityChecks();
52 }
53
54 private void sanityChecks() {
55 CheckParameterUtil.ensureParameterNotNull(osm, "osm");
56 CheckParameterUtil.ensureParameterNotNull(newOsm, "newOsm");
57 if (newOsm instanceof Way && ((Way) newOsm).getNodesCount() == 0) {
58 // Do not allow to create empty ways (see #7465)
59 throw new IllegalArgumentException(tr("New way {0} has 0 nodes", newOsm));
60 }
61 }
62
63 @Override
64 public boolean executeCommand() {
65 super.executeCommand();
66 osm.cloneFrom(newOsm);
67 osm.setModified(true);
68 return true;
69 }
70
71 @Override
72 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
73 modified.add(osm);
74 }
75
76 @Override
77 public String getDescriptionText() {
78 String msg = "";
79 switch(OsmPrimitiveType.from(osm)) {
80 case NODE: msg = marktr("Change node {0}"); break;
81 case WAY: msg = marktr("Change way {0}"); break;
82 case RELATION: msg = marktr("Change relation {0}"); break;
83 }
84 return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
85 }
86
87 @Override
88 public Icon getDescriptionIcon() {
89 return ImageProvider.get(osm.getDisplayType());
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hash(super.hashCode(), osm, newOsm);
95 }
96
97 @Override
98 public boolean equals(Object obj) {
99 if (this == obj) return true;
100 if (obj == null || getClass() != obj.getClass()) return false;
101 if (!super.equals(obj)) return false;
102 ChangeCommand that = (ChangeCommand) obj;
103 return Objects.equals(osm, that.osm) &&
104 Objects.equals(newOsm, that.newOsm);
105 }
106}
Note: See TracBrowser for help on using the repository browser.