Changeset 19065 in josm


Ignore:
Timestamp:
2024-04-28T09:37:20+02:00 (7 months ago)
Author:
GerdP
Message:

see #21881 Add a check for loops in directional waterways
Fix code in WaySegment and IWaySegment

  • change text in Exception to "Node pair is not a single segment of the way!" and move it to constant IWaySegment.NOT_A_SEGMENT
  • fix error in WaySegment.forNodePair and IWaySegment.forNodePair
  • add unit test for further invalid node pairs
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/IWaySegment.java

    r17986 r19065  
    1818 */
    1919public class IWaySegment<N extends INode, W extends IWay<N>> implements Comparable<IWaySegment<N, W>> {
     20    protected static final String NOT_A_SEGMENT = "Node pair is not a single segment of the way!";
    2021
    2122    private final W way;
     
    9798     * @param second second node
    9899     * @return way segment
    99      * @throws IllegalArgumentException if the node pair is not part of way
     100     * @throws IllegalArgumentException if the node pair is not a single segment of the way
    100101     */
    101102    public static <N extends INode, W extends IWay<N>> IWaySegment<N, W> forNodePair(W way, N first, N second) {
     
    103104        while (endIndex > 0) {
    104105            final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first);
     106            if (indexOfFirst < 0)
     107                break;
    105108            if (second.equals(way.getNode(indexOfFirst + 1))) {
    106109                return new IWaySegment<>(way, indexOfFirst);
     
    108111            endIndex--;
    109112        }
    110         throw new IllegalArgumentException("Node pair is not part of way!");
     113        throw new IllegalArgumentException(NOT_A_SEGMENT);
    111114    }
    112115
     
    162165    @Override
    163166    public int compareTo(IWaySegment o) {
     167        if (o == null)
     168            return -1;
    164169        final W thisWay;
    165170        final IWay<?> otherWay;
    166171        try {
    167172            thisWay = toWay();
    168             otherWay = o == null ? null : o.toWay();
     173            otherWay = o.toWay();
    169174        } catch (ReflectiveOperationException e) {
    170175            Logging.error(e);
    171176            return -1;
    172177        }
    173         return o == null ? -1 : (equals(o) ? 0 : thisWay.compareTo(otherWay));
     178        return equals(o) ? 0 : thisWay.compareTo(otherWay);
    174179    }
    175180
  • trunk/src/org/openstreetmap/josm/data/osm/WaySegment.java

    r19062 r19065  
    2626     * @param second second node
    2727     * @return way segment
    28      * @throws IllegalArgumentException if the node pair is not part of way
     28     * @throws IllegalArgumentException if the node pair is not single a segment of the way
    2929     */
    3030    public static WaySegment forNodePair(Way way, Node first, Node second) {
     
    3232        while (endIndex > 0) {
    3333            final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first);
     34            if (indexOfFirst < 0)
     35                break;
    3436            if (second.equals(way.getNode(indexOfFirst + 1))) {
    3537                return new WaySegment(way, indexOfFirst);
     
    3739            endIndex--;
    3840        }
    39         throw new IllegalArgumentException("The node pair is not consecutive part of the way!");
     41        throw new IllegalArgumentException(IWaySegment.NOT_A_SEGMENT);
    4042    }
    4143
  • trunk/test/unit/org/openstreetmap/josm/data/osm/WaySegmentTest.java

    r19064 r19065  
    2121        final Node n3 = new Node(new LatLon(2, 0));
    2222        final Node n4 = new Node(new LatLon(3, 0));
    23         final Way w = new Way();
    24         for (OsmPrimitive p : Arrays.asList(n1, n2, n3, n4, w)) {
     23        final Way w1 = new Way();
     24        final Way w2 = new Way();
     25        for (OsmPrimitive p : Arrays.asList(n1, n2, n3, n4, w1, w2)) {
    2526            ds.addPrimitive(p);
    2627        }
    27         w.addNode(n1);
    28         w.addNode(n2);
    29         w.addNode(n1);
    30         w.addNode(n3);
    31         w.addNode(n1);
    32         w.addNode(n4);
    33         w.addNode(n1);
    34         assertEquals(WaySegment.forNodePair(w, n1, n2).getLowerIndex(), 0);
    35         assertEquals(WaySegment.forNodePair(w, n1, n3).getLowerIndex(), 2);
    36         assertEquals(WaySegment.forNodePair(w, n1, n4).getLowerIndex(), 4);
    37         assertEquals(WaySegment.forNodePair(w, n4, n1).getLowerIndex(), 5);
    38         IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w, n3, n4));
    39         assertEquals("The node pair is not consecutive part of the way!", iae.getMessage());
     28        w1.addNode(n1);
     29        w1.addNode(n2);
     30        w1.addNode(n1);
     31        w1.addNode(n3);
     32        w1.addNode(n1);
     33        w1.addNode(n4);
     34        w1.addNode(n1);
     35
     36        w2.addNode(n1);
     37        w2.addNode(n2);
     38        w2.addNode(n3);
     39
     40        assertEquals(0, WaySegment.forNodePair(w1, n1, n2).getLowerIndex());
     41        assertEquals(2, WaySegment.forNodePair(w1, n1, n3).getLowerIndex());
     42        assertEquals(4, WaySegment.forNodePair(w1, n1, n4).getLowerIndex());
     43        assertEquals(5, WaySegment.forNodePair(w1, n4, n1).getLowerIndex());
     44        // two segments between n3 and n4
     45        IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w1, n3, n4));
     46        assertEquals(IWaySegment.NOT_A_SEGMENT, iae.getMessage());
     47        // wrong order
     48        iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w2, n2, n1));
     49        assertEquals(IWaySegment.NOT_A_SEGMENT, iae.getMessage());
     50        // node is not in way
     51        iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w2, n1, n4));
     52        assertEquals(IWaySegment.NOT_A_SEGMENT, iae.getMessage());
    4053    }
    4154}
Note: See TracChangeset for help on using the changeset viewer.