Changeset 19065 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/IWaySegment.java
r17986 r19065 18 18 */ 19 19 public 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!"; 20 21 21 22 private final W way; … … 97 98 * @param second second node 98 99 * @return way segment 99 * @throws IllegalArgumentException if the node pair is not part ofway100 * @throws IllegalArgumentException if the node pair is not a single segment of the way 100 101 */ 101 102 public static <N extends INode, W extends IWay<N>> IWaySegment<N, W> forNodePair(W way, N first, N second) { … … 103 104 while (endIndex > 0) { 104 105 final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first); 106 if (indexOfFirst < 0) 107 break; 105 108 if (second.equals(way.getNode(indexOfFirst + 1))) { 106 109 return new IWaySegment<>(way, indexOfFirst); … … 108 111 endIndex--; 109 112 } 110 throw new IllegalArgumentException( "Node pair is not part of way!");113 throw new IllegalArgumentException(NOT_A_SEGMENT); 111 114 } 112 115 … … 162 165 @Override 163 166 public int compareTo(IWaySegment o) { 167 if (o == null) 168 return -1; 164 169 final W thisWay; 165 170 final IWay<?> otherWay; 166 171 try { 167 172 thisWay = toWay(); 168 otherWay = o == null ? null : o.toWay();173 otherWay = o.toWay(); 169 174 } catch (ReflectiveOperationException e) { 170 175 Logging.error(e); 171 176 return -1; 172 177 } 173 return o == null ? -1 : (equals(o) ? 0 : thisWay.compareTo(otherWay));178 return equals(o) ? 0 : thisWay.compareTo(otherWay); 174 179 } 175 180 -
trunk/src/org/openstreetmap/josm/data/osm/WaySegment.java
r19062 r19065 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 not single a segment of the way 29 29 */ 30 30 public static WaySegment forNodePair(Way way, Node first, Node second) { … … 32 32 while (endIndex > 0) { 33 33 final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first); 34 if (indexOfFirst < 0) 35 break; 34 36 if (second.equals(way.getNode(indexOfFirst + 1))) { 35 37 return new WaySegment(way, indexOfFirst); … … 37 39 endIndex--; 38 40 } 39 throw new IllegalArgumentException( "The node pair is not consecutive part of the way!");41 throw new IllegalArgumentException(IWaySegment.NOT_A_SEGMENT); 40 42 } 41 43 -
trunk/test/unit/org/openstreetmap/josm/data/osm/WaySegmentTest.java
r19064 r19065 21 21 final Node n3 = new Node(new LatLon(2, 0)); 22 22 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)) { 25 26 ds.addPrimitive(p); 26 27 } 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()); 40 53 } 41 54 }
Note:
See TracChangeset
for help on using the changeset viewer.