source: josm/trunk/src/org/openstreetmap/josm/command/ChangePropertyKeyCommand.java@ 9783

Last change on this file since 9783 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: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.LinkedList;
11import java.util.List;
12import java.util.Objects;
13
14import javax.swing.Icon;
15
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.validation.util.NameVisitor;
18import org.openstreetmap.josm.tools.ImageProvider;
19
20/**
21 * Command that replaces the key of one or several objects
22 *
23 */
24public class ChangePropertyKeyCommand extends Command {
25 /**
26 * All primitives, that are affected with this command.
27 */
28 private final List<? extends OsmPrimitive> objects;
29 /**
30 * The key that is subject to change.
31 */
32 private final String key;
33 /**
34 * The mew key.
35 */
36 private final String newKey;
37
38 /**
39 * Constructs a new {@code ChangePropertyKeyCommand}.
40 *
41 * @param object the object subject to change replacement
42 * @param key The key to replace
43 * @param newKey the new value of the key
44 * @since 6329
45 */
46 public ChangePropertyKeyCommand(OsmPrimitive object, String key, String newKey) {
47 this(Collections.singleton(object), key, newKey);
48 }
49
50 /**
51 * Constructs a new {@code ChangePropertyKeyCommand}.
52 *
53 * @param objects all objects subject to change replacement
54 * @param key The key to replace
55 * @param newKey the new value of the key
56 */
57 public ChangePropertyKeyCommand(Collection<? extends OsmPrimitive> objects, String key, String newKey) {
58 this.objects = new LinkedList<>(objects);
59 this.key = key;
60 this.newKey = newKey;
61 }
62
63 @Override
64 public boolean executeCommand() {
65 if (!super.executeCommand())
66 return false; // save old
67 for (OsmPrimitive osm : objects) {
68 if (osm.hasKeys()) {
69 osm.setModified(true);
70 String oldValue = osm.get(key);
71 osm.put(newKey, oldValue);
72 osm.remove(key);
73 }
74 }
75 return true;
76 }
77
78 @Override
79 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
80 modified.addAll(objects);
81 }
82
83 @Override
84 public String getDescriptionText() {
85 String text = tr("Replace \"{0}\" by \"{1}\" for", key, newKey);
86 if (objects.size() == 1) {
87 NameVisitor v = new NameVisitor();
88 objects.get(0).accept(v);
89 text += ' '+tr(v.className)+' '+v.name;
90 } else {
91 text += ' '+objects.size()+' '+trn("object", "objects", objects.size());
92 }
93 return text;
94 }
95
96 @Override
97 public Icon getDescriptionIcon() {
98 return ImageProvider.get("data", "key");
99 }
100
101 @Override
102 public Collection<PseudoCommand> getChildren() {
103 if (objects.size() == 1)
104 return null;
105 List<PseudoCommand> children = new ArrayList<>();
106
107 final NameVisitor v = new NameVisitor();
108 for (final OsmPrimitive osm : objects) {
109 osm.accept(v);
110 children.add(new PseudoCommand() {
111 @Override
112 public String getDescriptionText() {
113 return v.name;
114 }
115
116 @Override
117 public Icon getDescriptionIcon() {
118 return v.icon;
119 }
120
121 @Override
122 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
123 return Collections.singleton(osm);
124 }
125 });
126 }
127 return children;
128 }
129
130 @Override
131 public int hashCode() {
132 return Objects.hash(super.hashCode(), objects, key, newKey);
133 }
134
135 @Override
136 public boolean equals(Object obj) {
137 if (this == obj) return true;
138 if (obj == null || getClass() != obj.getClass()) return false;
139 if (!super.equals(obj)) return false;
140 ChangePropertyKeyCommand that = (ChangePropertyKeyCommand) obj;
141 return Objects.equals(objects, that.objects) &&
142 Objects.equals(key, that.key) &&
143 Objects.equals(newKey, that.newKey);
144 }
145}
Note: See TracBrowser for help on using the repository browser.