source: josm/trunk/src/org/openstreetmap/josm/data/validation/TestError.java@ 8840

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

sonar - squid:S3052 - Fields should not be initialized to default values

  • Property svn:eol-style set to native
File size: 12.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.List;
8import java.util.TreeSet;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.command.Command;
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.data.osm.WaySegment;
17import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
18import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
19import org.openstreetmap.josm.data.osm.event.DataSetListener;
20import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
21import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
22import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
23import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
24import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
25import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
26import org.openstreetmap.josm.data.validation.util.MultipleNameVisitor;
27import org.openstreetmap.josm.tools.AlphanumComparator;
28
29/**
30 * Validation error
31 * @since 3669
32 */
33public class TestError implements Comparable<TestError>, DataSetListener {
34 /** is this error on the ignore list */
35 private boolean ignored;
36 /** Severity */
37 private Severity severity;
38 /** The error message */
39 private String message;
40 /** Deeper error description */
41 private String description;
42 private String descriptionEn;
43 /** The affected primitives */
44 private Collection<? extends OsmPrimitive> primitives;
45 /** The primitives or way segments to be highlighted */
46 private Collection<?> highlighted;
47 /** The tester that raised this error */
48 private Test tester;
49 /** Internal code used by testers to classify errors */
50 private int code;
51 /** If this error is selected */
52 private boolean selected;
53
54 /**
55 * Constructs a new {@code TestError}.
56 * @param tester The tester
57 * @param severity The severity of this error
58 * @param message The error message
59 * @param description The translated description
60 * @param descriptionEn The English description
61 * @param primitives The affected primitives
62 * @param code The test error reference code
63 * @param highlighted OSM primitives to highlight
64 */
65 public TestError(Test tester, Severity severity, String message, String description, String descriptionEn,
66 int code, Collection<? extends OsmPrimitive> primitives, Collection<?> highlighted) {
67 this.tester = tester;
68 this.severity = severity;
69 this.message = message;
70 this.description = description;
71 this.descriptionEn = descriptionEn;
72 this.primitives = primitives;
73 this.highlighted = highlighted;
74 this.code = code;
75 }
76
77 /**
78 * Constructs a new {@code TestError} without description.
79 * @param tester The tester
80 * @param severity The severity of this error
81 * @param message The error message
82 * @param primitives The affected primitives
83 * @param code The test error reference code
84 * @param highlighted OSM primitives to highlight
85 */
86 public TestError(Test tester, Severity severity, String message, int code, Collection<? extends OsmPrimitive> primitives,
87 Collection<?> highlighted) {
88 this(tester, severity, message, null, null, code, primitives, highlighted);
89 }
90
91 /**
92 * Constructs a new {@code TestError}.
93 * @param tester The tester
94 * @param severity The severity of this error
95 * @param message The error message
96 * @param description The translated description
97 * @param descriptionEn The English description
98 * @param primitives The affected primitives
99 * @param code The test error reference code
100 */
101 public TestError(Test tester, Severity severity, String message, String description, String descriptionEn,
102 int code, Collection<? extends OsmPrimitive> primitives) {
103 this(tester, severity, message, description, descriptionEn, code, primitives, primitives);
104 }
105
106 /**
107 * Constructs a new {@code TestError} without description.
108 * @param tester The tester
109 * @param severity The severity of this error
110 * @param message The error message
111 * @param primitives The affected primitives
112 * @param code The test error reference code
113 */
114 public TestError(Test tester, Severity severity, String message, int code, Collection<? extends OsmPrimitive> primitives) {
115 this(tester, severity, message, null, null, code, primitives, primitives);
116 }
117
118 /**
119 * Constructs a new {@code TestError} without description, for a single primitive.
120 * @param tester The tester
121 * @param severity The severity of this error
122 * @param message The error message
123 * @param primitive The affected primitive
124 * @param code The test error reference code
125 */
126 public TestError(Test tester, Severity severity, String message, int code, OsmPrimitive primitive) {
127 this(tester, severity, message, null, null, code, Collections.singletonList(primitive), Collections
128 .singletonList(primitive));
129 }
130
131 /**
132 * Constructs a new {@code TestError} for a single primitive.
133 * @param tester The tester
134 * @param severity The severity of this error
135 * @param message The error message
136 * @param description The translated description
137 * @param descriptionEn The English description
138 * @param primitive The affected primitive
139 * @param code The test error reference code
140 */
141 public TestError(Test tester, Severity severity, String message, String description, String descriptionEn,
142 int code, OsmPrimitive primitive) {
143 this(tester, severity, message, description, descriptionEn, code, Collections.singletonList(primitive));
144 }
145
146 /**
147 * Gets the error message
148 * @return the error message
149 */
150 public String getMessage() {
151 return message;
152 }
153
154 /**
155 * Gets the error message
156 * @return the error description
157 */
158 public String getDescription() {
159 return description;
160 }
161
162 /**
163 * Sets the error message
164 * @param message The error message
165 */
166 public void setMessage(String message) {
167 this.message = message;
168 }
169
170 /**
171 * Gets the list of primitives affected by this error
172 * @return the list of primitives affected by this error
173 */
174 public Collection<? extends OsmPrimitive> getPrimitives() {
175 return primitives;
176 }
177
178 /**
179 * Gets the list of primitives affected by this error and are selectable
180 * @return the list of selectable primitives affected by this error
181 */
182 public Collection<? extends OsmPrimitive> getSelectablePrimitives() {
183 List<OsmPrimitive> selectablePrimitives = new ArrayList<>(primitives.size());
184 for (OsmPrimitive o : primitives) {
185 if (o.isSelectable()) {
186 selectablePrimitives.add(o);
187 }
188 }
189 return selectablePrimitives;
190 }
191
192 /**
193 * Sets the list of primitives affected by this error
194 * @param primitives the list of primitives affected by this error
195 */
196 public void setPrimitives(List<? extends OsmPrimitive> primitives) {
197 this.primitives = primitives;
198 }
199
200 /**
201 * Gets the severity of this error
202 * @return the severity of this error
203 */
204 public Severity getSeverity() {
205 return severity;
206 }
207
208 /**
209 * Sets the severity of this error
210 * @param severity the severity of this error
211 */
212 public void setSeverity(Severity severity) {
213 this.severity = severity;
214 }
215
216 /**
217 * Sets the ignore state for this error
218 */
219 public String getIgnoreState() {
220 Collection<String> strings = new TreeSet<>();
221 StringBuilder ignorestring = new StringBuilder(getIgnoreSubGroup());
222 for (OsmPrimitive o : primitives) {
223 // ignore data not yet uploaded
224 if (o.isNew())
225 return null;
226 String type = "u";
227 if (o instanceof Way) {
228 type = "w";
229 } else if (o instanceof Relation) {
230 type = "r";
231 } else if (o instanceof Node) {
232 type = "n";
233 }
234 strings.add(type + "_" + o.getId());
235 }
236 for (String o : strings) {
237 ignorestring.append(':').append(o);
238 }
239 return ignorestring.toString();
240 }
241
242 public String getIgnoreSubGroup() {
243 String ignorestring = getIgnoreGroup();
244 if (descriptionEn != null) {
245 ignorestring += "_" + descriptionEn;
246 }
247 return ignorestring;
248 }
249
250 public String getIgnoreGroup() {
251 return Integer.toString(code);
252 }
253
254 public void setIgnored(boolean state) {
255 ignored = state;
256 }
257
258 public boolean isIgnored() {
259 return ignored;
260 }
261
262 /**
263 * Gets the tester that raised this error
264 * @return the tester that raised this error
265 */
266 public Test getTester() {
267 return tester;
268 }
269
270 public void setTester(Test tester) {
271 this.tester = tester;
272 }
273
274 /**
275 * Gets the code
276 * @return the code
277 */
278 public int getCode() {
279 return code;
280 }
281
282 /**
283 * Returns true if the error can be fixed automatically
284 *
285 * @return true if the error can be fixed
286 */
287 public boolean isFixable() {
288 return tester != null && tester.isFixable(this);
289 }
290
291 /**
292 * Fixes the error with the appropriate command
293 *
294 * @return The command to fix the error
295 */
296 public Command getFix() {
297 if (tester == null || !tester.isFixable(this) || primitives.isEmpty())
298 return null;
299
300 return tester.fixError(this);
301 }
302
303 /**
304 * Sets the selection flag of this error
305 * @param selected if this error is selected
306 */
307 public void setSelected(boolean selected) {
308 this.selected = selected;
309 }
310
311 @SuppressWarnings("unchecked")
312 public void visitHighlighted(ValidatorVisitor v) {
313 for (Object o : highlighted) {
314 if (o instanceof OsmPrimitive) {
315 v.visit((OsmPrimitive) o);
316 } else if (o instanceof WaySegment) {
317 v.visit((WaySegment) o);
318 } else if (o instanceof List<?>) {
319 v.visit((List<Node>) o);
320 }
321 }
322 }
323
324 /**
325 * Returns the selection flag of this error
326 * @return true if this error is selected
327 * @since 5671
328 */
329 public boolean isSelected() {
330 return selected;
331 }
332
333 /**
334 * Returns The primitives or way segments to be highlighted
335 * @return The primitives or way segments to be highlighted
336 * @since 5671
337 */
338 public Collection<?> getHighlighted() {
339 return highlighted;
340 }
341
342 @Override
343 public int compareTo(TestError o) {
344 if (equals(o)) return 0;
345
346 MultipleNameVisitor v1 = new MultipleNameVisitor();
347 MultipleNameVisitor v2 = new MultipleNameVisitor();
348
349 v1.visit(getPrimitives());
350 v2.visit(o.getPrimitives());
351 return AlphanumComparator.getInstance().compare(v1.toString(), v2.toString());
352 }
353
354 @Override public void primitivesRemoved(PrimitivesRemovedEvent event) {
355 // Remove purged primitives (fix #8639)
356 try {
357 primitives.removeAll(event.getPrimitives());
358 } catch (UnsupportedOperationException e) {
359 if (event.getPrimitives().containsAll(primitives)) {
360 primitives = Collections.emptyList();
361 } else {
362 Main.warn("Unable to remove primitives from "+this);
363 }
364 }
365 }
366
367 @Override public void primitivesAdded(PrimitivesAddedEvent event) {}
368
369 @Override public void tagsChanged(TagsChangedEvent event) {}
370
371 @Override public void nodeMoved(NodeMovedEvent event) {}
372
373 @Override public void wayNodesChanged(WayNodesChangedEvent event) {}
374
375 @Override public void relationMembersChanged(RelationMembersChangedEvent event) {}
376
377 @Override public void otherDatasetChange(AbstractDatasetChangedEvent event) {}
378
379 @Override public void dataChanged(DataChangedEvent event) {}
380
381 @Override
382 public String toString() {
383 return "TestError [tester=" + tester + ", code=" + code + ", message=" + message + "]";
384 }
385}
Note: See TracBrowser for help on using the repository browser.