Ticket #21881: 21881-fix-forNodePair.patch

File 21881-fix-forNodePair.patch, 4.2 KB (added by GerdP, 13 days ago)

add check to make sure that forNodePair doesn't return WaySegment with index -1

  • src/org/openstreetmap/josm/data/osm/IWaySegment.java

     
    9696     * @param first first node
    9797     * @param second second node
    9898     * @return way segment
    99      * @throws IllegalArgumentException if the node pair is not part of way
     99     * @throws IllegalArgumentException if the node pair is a segment of the way
    100100     */
    101101    public static <N extends INode, W extends IWay<N>> IWaySegment<N, W> forNodePair(W way, N first, N second) {
    102102        int endIndex = way.getNodesCount() - 1;
    103103        while (endIndex > 0) {
    104104            final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first);
    105             if (second.equals(way.getNode(indexOfFirst + 1))) {
     105            if (indexOfFirst >= 0 && second.equals(way.getNode(indexOfFirst + 1))) {
    106106                return new IWaySegment<>(way, indexOfFirst);
    107107            }
    108108            endIndex--;
    109109        }
    110         throw new IllegalArgumentException("Node pair is not part of way!");
     110        throw new IllegalArgumentException("Node pair is not a segment of the way!");
    111111    }
    112112
    113113    /**
  • src/org/openstreetmap/josm/data/osm/WaySegment.java

     
    2525     * @param first  first node
    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 a segment of the way
    2929     */
    3030    public static WaySegment forNodePair(Way way, Node first, Node second) {
    3131        int endIndex = way.getNodesCount() - 1;
    3232        while (endIndex > 0) {
    3333            final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first);
    34             if (second.equals(way.getNode(indexOfFirst + 1))) {
     34            if (indexOfFirst >= 0 && second.equals(way.getNode(indexOfFirst + 1))) {
    3535                return new WaySegment(way, indexOfFirst);
    3636            }
    3737            endIndex--;
    3838        }
    39         throw new IllegalArgumentException("The node pair is not consecutive part of the way!");
     39        throw new IllegalArgumentException("Node pair is not a segment of the way!");
    4040    }
    4141
    4242    /**
  • test/unit/org/openstreetmap/josm/data/osm/WaySegmentTest.java

     
    3636        assertEquals(WaySegment.forNodePair(w, n1, n4).getLowerIndex(), 4);
    3737        assertEquals(WaySegment.forNodePair(w, n4, n1).getLowerIndex(), 5);
    3838        IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w, n3, n4));
    39         assertEquals("The node pair is not consecutive part of the way!", iae.getMessage());
     39        assertEquals("Node pair is not a segment of the way!", iae.getMessage());
    4040    }
     41
     42    @Test
     43    void testForNodePair2() {
     44        final DataSet ds = new DataSet();
     45        final Node n1 = new Node(LatLon.ZERO);
     46        final Node n2 = new Node(new LatLon(1, 0));
     47        final Node n3 = new Node(new LatLon(2, 0));
     48        final Node n4 = new Node(new LatLon(3, 0));
     49        final Way w = new Way();
     50        for (OsmPrimitive p : Arrays.asList(n1, n2, n3, n4, w)) {
     51            ds.addPrimitive(p);
     52        }
     53        w.addNode(n1);
     54        w.addNode(n2);
     55        w.addNode(n3);
     56        // wrong order
     57        IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w, n2, n1));
     58        assertEquals("Node pair is not a segment of the way!", iae.getMessage());
     59        // node is not in way
     60        iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w, n1, n4));
     61        assertEquals("Node pair is not a segment of the way!", iae.getMessage());
     62    }
     63
    4164}