Ticket #21881: 21881-fix-forNodePair.patch
File 21881-fix-forNodePair.patch, 4.2 KB (added by , 13 days ago) |
---|
-
src/org/openstreetmap/josm/data/osm/IWaySegment.java
96 96 * @param first first node 97 97 * @param second second node 98 98 * @return way segment 99 * @throws IllegalArgumentException if the node pair is not part ofway99 * @throws IllegalArgumentException if the node pair is a segment of the way 100 100 */ 101 101 public static <N extends INode, W extends IWay<N>> IWaySegment<N, W> forNodePair(W way, N first, N second) { 102 102 int endIndex = way.getNodesCount() - 1; 103 103 while (endIndex > 0) { 104 104 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))) { 106 106 return new IWaySegment<>(way, indexOfFirst); 107 107 } 108 108 endIndex--; 109 109 } 110 throw new IllegalArgumentException("Node pair is not part ofway!");110 throw new IllegalArgumentException("Node pair is not a segment of the way!"); 111 111 } 112 112 113 113 /** -
src/org/openstreetmap/josm/data/osm/WaySegment.java
25 25 * @param first first node 26 26 * @param second second node 27 27 * @return way segment 28 * @throws IllegalArgumentException if the node pair is not part ofway28 * @throws IllegalArgumentException if the node pair is a segment of the way 29 29 */ 30 30 public static WaySegment forNodePair(Way way, Node first, Node second) { 31 31 int endIndex = way.getNodesCount() - 1; 32 32 while (endIndex > 0) { 33 33 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))) { 35 35 return new WaySegment(way, indexOfFirst); 36 36 } 37 37 endIndex--; 38 38 } 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!"); 40 40 } 41 41 42 42 /** -
test/unit/org/openstreetmap/josm/data/osm/WaySegmentTest.java
36 36 assertEquals(WaySegment.forNodePair(w, n1, n4).getLowerIndex(), 4); 37 37 assertEquals(WaySegment.forNodePair(w, n4, n1).getLowerIndex(), 5); 38 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());39 assertEquals("Node pair is not a segment of the way!", iae.getMessage()); 40 40 } 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 41 64 }