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

Last change on this file since 7489 was 6475, checked in by Don-vip, 10 years ago

fix #9433 - NoSuchElementException in validator at upload (probably after purge of all elements of a given error)

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