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

Last change on this file since 17243 was 17141, checked in by GerdP, 4 years ago

see #19885: memory leak with "temporary" objects in validator and actions
next bunch of changes:

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