001    // License: GPL. See LICENSE file for details.
002    package org.openstreetmap.josm.data.validation.tests;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.util.Arrays;
007    import java.util.HashSet;
008    
009    import org.openstreetmap.josm.data.osm.Node;
010    import org.openstreetmap.josm.data.osm.Way;
011    import org.openstreetmap.josm.data.validation.Severity;
012    import org.openstreetmap.josm.data.validation.Test;
013    import org.openstreetmap.josm.data.validation.TestError;
014    
015    /**
016     * Checks for self-intersecting ways.
017     */
018    public class SelfIntersectingWay extends Test {
019    
020        protected static final int SELF_INTERSECT = 401;
021    
022        public SelfIntersectingWay() {
023            super(tr("Self-intersecting ways"),
024                    tr("This test checks for ways " +
025                            "that contain some of their nodes more than once."));
026        }
027    
028        @Override public void visit(Way w) {
029            HashSet<Node> nodes = new HashSet<Node>();
030    
031            for (int i = 1; i < w.getNodesCount() - 1; i++) {
032                Node n = w.getNode(i);
033                if (nodes.contains(n)) {
034                    errors.add(new TestError(this,
035                            Severity.WARNING, tr("Self-intersecting ways"), SELF_INTERSECT,
036                            Arrays.asList(w), Arrays.asList(n)));
037                    break;
038                } else {
039                    nodes.add(n);
040                }
041            }
042        }
043    }