source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/DuplicatedWayNodes.java@ 13250

Last change on this file since 13250 was 11129, checked in by simon04, 8 years ago

fix #13799 - Use builder pattern for TestError

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collections;
7import java.util.Iterator;
8
9import org.openstreetmap.josm.command.ChangeCommand;
10import org.openstreetmap.josm.command.Command;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Way;
14import org.openstreetmap.josm.data.validation.Severity;
15import org.openstreetmap.josm.data.validation.Test;
16import org.openstreetmap.josm.data.validation.TestError;
17
18/**
19 * Checks for ways with identical consecutive nodes.
20 * @since 3669
21 */
22public class DuplicatedWayNodes extends Test {
23 protected static final int DUPLICATE_WAY_NODE = 501;
24
25 /**
26 * Constructs a new {@code DuplicatedWayNodes} test.
27 */
28 public DuplicatedWayNodes() {
29 super(tr("Duplicated way nodes"),
30 tr("Checks for ways with identical consecutive nodes."));
31 }
32
33 @Override
34 public void visit(Way w) {
35 if (!w.isUsable()) return;
36
37 Node lastN = null;
38 for (Node n : w.getNodes()) {
39 if (lastN == null) {
40 lastN = n;
41 continue;
42 }
43 if (lastN == n) {
44 errors.add(TestError.builder(this, Severity.ERROR, DUPLICATE_WAY_NODE)
45 .message(tr("Duplicated way nodes"))
46 .primitives(w)
47 .highlight(n)
48 .build());
49 break;
50 }
51 lastN = n;
52 }
53 }
54
55 @Override
56 public Command fixError(TestError testError) {
57 // primitives list can be empty if all primitives have been purged
58 Iterator<? extends OsmPrimitive> it = testError.getPrimitives().iterator();
59 if (it.hasNext()) {
60 Way w = (Way) it.next();
61 Way wnew = new Way(w);
62 wnew.setNodes(null);
63 Node lastN = null;
64 for (Node n : w.getNodes()) {
65 if (lastN == null) {
66 wnew.addNode(n);
67 } else if (n == lastN) {
68 // Skip this node
69 } else {
70 wnew.addNode(n);
71 }
72 lastN = n;
73 }
74 if (wnew.getNodesCount() < 2)
75 // Empty way, delete
76 return deletePrimitivesIfNeeded(Collections.singleton(w));
77 else
78 return new ChangeCommand(w, wnew);
79 }
80 return null;
81 }
82
83 @Override
84 public boolean isFixable(TestError testError) {
85 return testError.getTester() instanceof DuplicatedWayNodes;
86 }
87}
Note: See TracBrowser for help on using the repository browser.