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

Last change on this file since 9231 was 8846, checked in by Don-vip, 9 years ago

sonar - fb-contrib - minor performance improvements:

  • Method passes constant String of length 1 to character overridden method
  • Method needlessly boxes a boolean constant
  • Method uses iterator().next() on a List to get the first item
  • Method converts String to boxed primitive using excessive boxing
  • Method converts String to primitive using excessive boxing
  • Method creates array using constants
  • Class defines List based fields but uses them like Sets
  • Property svn:eol-style set to native
File size: 4.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;
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;
12
13import javax.swing.Icon;
14
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.validation.util.NameVisitor;
17import org.openstreetmap.josm.tools.ImageProvider;
18
19/**
20 * Command that replaces the key of one or several objects
21 *
22 */
23public class ChangePropertyKeyCommand extends Command {
24 /**
25 * All primitives, that are affected with this command.
26 */
27 private final List<? extends OsmPrimitive> objects;
28 /**
29 * The key that is subject to change.
30 */
31 private final String key;
32 /**
33 * The mew key.
34 */
35 private final String newKey;
36
37 /**
38 * Constructs a new {@code ChangePropertyKeyCommand}.
39 *
40 * @param object the object subject to change replacement
41 * @param key The key to replace
42 * @param newKey the new value of the key
43 * @since 6329
44 */
45 public ChangePropertyKeyCommand(OsmPrimitive object, String key, String newKey) {
46 this(Collections.singleton(object), key, newKey);
47 }
48
49 /**
50 * Constructs a new {@code ChangePropertyKeyCommand}.
51 *
52 * @param objects all objects subject to change replacement
53 * @param key The key to replace
54 * @param newKey the new value of the key
55 */
56 public ChangePropertyKeyCommand(Collection<? extends OsmPrimitive> objects, String key, String newKey) {
57 this.objects = new LinkedList<>(objects);
58 this.key = key;
59 this.newKey = newKey;
60 }
61
62 @Override
63 public boolean executeCommand() {
64 if (!super.executeCommand())
65 return false; // save old
66 for (OsmPrimitive osm : objects) {
67 if (osm.hasKeys()) {
68 osm.setModified(true);
69 String oldValue = osm.get(key);
70 osm.put(newKey, oldValue);
71 osm.remove(key);
72 }
73 }
74 return true;
75 }
76
77 @Override
78 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
79 modified.addAll(objects);
80 }
81
82 @Override
83 public String getDescriptionText() {
84 String text = tr("Replace \"{0}\" by \"{1}\" for", key, newKey);
85 if (objects.size() == 1) {
86 NameVisitor v = new NameVisitor();
87 objects.get(0).accept(v);
88 text += ' '+tr(v.className)+' '+v.name;
89 } else {
90 text += ' '+objects.size()+' '+trn("object", "objects", objects.size());
91 }
92 return text;
93 }
94
95 @Override
96 public Icon getDescriptionIcon() {
97 return ImageProvider.get("data", "key");
98 }
99
100 @Override
101 public Collection<PseudoCommand> getChildren() {
102 if (objects.size() == 1)
103 return null;
104 List<PseudoCommand> children = new ArrayList<>();
105
106 final NameVisitor v = new NameVisitor();
107 for (final OsmPrimitive osm : objects) {
108 osm.accept(v);
109 children.add(new PseudoCommand() {
110 @Override
111 public String getDescriptionText() {
112 return v.name;
113 }
114
115 @Override
116 public Icon getDescriptionIcon() {
117 return v.icon;
118 }
119
120 @Override
121 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
122 return Collections.singleton(osm);
123 }
124 });
125 }
126 return children;
127 }
128
129 @Override
130 public int hashCode() {
131 final int prime = 31;
132 int result = super.hashCode();
133 result = prime * result + ((key == null) ? 0 : key.hashCode());
134 result = prime * result + ((newKey == null) ? 0 : newKey.hashCode());
135 result = prime * result + ((objects == null) ? 0 : objects.hashCode());
136 return result;
137 }
138
139 @Override
140 public boolean equals(Object obj) {
141 if (this == obj)
142 return true;
143 if (!super.equals(obj))
144 return false;
145 if (getClass() != obj.getClass())
146 return false;
147 ChangePropertyKeyCommand other = (ChangePropertyKeyCommand) obj;
148 if (key == null) {
149 if (other.key != null)
150 return false;
151 } else if (!key.equals(other.key))
152 return false;
153 if (newKey == null) {
154 if (other.newKey != null)
155 return false;
156 } else if (!newKey.equals(other.newKey))
157 return false;
158 if (objects == null) {
159 if (other.objects != null)
160 return false;
161 } else if (!objects.equals(other.objects))
162 return false;
163 return true;
164 }
165}
Note: See TracBrowser for help on using the repository browser.