source: josm/trunk/src/org/openstreetmap/josm/data/osm/WaySegment.java@ 12098

Last change on this file since 12098 was 11893, checked in by Don-vip, 7 years ago

sonar - squid:S1126 - Return of boolean expressions should not be wrapped into an "if-then-else" statement

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.awt.geom.Line2D;
5import java.util.Objects;
6
7/**
8 * A segment consisting of 2 consecutive nodes out of a way.
9 */
10public final class WaySegment implements Comparable<WaySegment> {
11
12 /**
13 * The way.
14 */
15 public Way way;
16
17 /**
18 * The index of one of the 2 nodes in the way. The other node has the
19 * index <code>lowerIndex + 1</code>.
20 */
21 public int lowerIndex;
22
23 /**
24 * Constructs a new {@code WaySegment}.
25 * @param w The way
26 * @param i The node lower index
27 */
28 public WaySegment(Way w, int i) {
29 way = w;
30 lowerIndex = i;
31 }
32
33 /**
34 * Returns the first node of the way segment.
35 * @return the first node
36 */
37 public Node getFirstNode() {
38 return way.getNode(lowerIndex);
39 }
40
41 /**
42 * Returns the second (last) node of the way segment.
43 * @return the second node
44 */
45 public Node getSecondNode() {
46 return way.getNode(lowerIndex + 1);
47 }
48
49 /**
50 * Determines and returns the way segment for the given way and node pair.
51 * @param way way
52 * @param first first node
53 * @param second second node
54 * @return way segment
55 * @throws IllegalArgumentException if the node pair is not part of way
56 */
57 public static WaySegment forNodePair(Way way, Node first, Node second) {
58 int endIndex = way.getNodesCount() - 1;
59 while (endIndex > 0) {
60 final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first);
61 if (second.equals(way.getNode(indexOfFirst + 1))) {
62 return new WaySegment(way, indexOfFirst);
63 }
64 endIndex--;
65 }
66 throw new IllegalArgumentException("Node pair is not part of way!");
67 }
68
69 /**
70 * Returns this way segment as complete way.
71 * @return the way segment as {@code Way}
72 */
73 public Way toWay() {
74 Way w = new Way();
75 w.addNode(getFirstNode());
76 w.addNode(getSecondNode());
77 return w;
78 }
79
80 @Override
81 public boolean equals(Object o) {
82 if (this == o) return true;
83 if (o == null || getClass() != o.getClass()) return false;
84 WaySegment that = (WaySegment) o;
85 return lowerIndex == that.lowerIndex &&
86 Objects.equals(way, that.way);
87 }
88
89 @Override
90 public int hashCode() {
91 return Objects.hash(way, lowerIndex);
92 }
93
94 @Override
95 public int compareTo(WaySegment o) {
96 return o == null ? -1 : (equals(o) ? 0 : toWay().compareTo(o.toWay()));
97 }
98
99 /**
100 * Checks whether this segment crosses other segment
101 *
102 * @param s2 The other segment
103 * @return true if both segments crosses
104 */
105 public boolean intersects(WaySegment s2) {
106 if (getFirstNode().equals(s2.getFirstNode()) || getSecondNode().equals(s2.getSecondNode()) ||
107 getFirstNode().equals(s2.getSecondNode()) || getSecondNode().equals(s2.getFirstNode()))
108 return false;
109
110 return Line2D.linesIntersect(
111 getFirstNode().getEastNorth().east(), getFirstNode().getEastNorth().north(),
112 getSecondNode().getEastNorth().east(), getSecondNode().getEastNorth().north(),
113 s2.getFirstNode().getEastNorth().east(), s2.getFirstNode().getEastNorth().north(),
114 s2.getSecondNode().getEastNorth().east(), s2.getSecondNode().getEastNorth().north());
115 }
116
117 /**
118 * Checks whether this segment and another way segment share the same points
119 * @param s2 The other segment
120 * @return true if other way segment is the same or reverse
121 */
122 public boolean isSimilar(WaySegment s2) {
123 return (getFirstNode().equals(s2.getFirstNode()) && getSecondNode().equals(s2.getSecondNode()))
124 || (getFirstNode().equals(s2.getSecondNode()) && getSecondNode().equals(s2.getFirstNode()));
125 }
126
127 @Override
128 public String toString() {
129 return "WaySegment [way=" + way.getUniqueId() + ", lowerIndex=" + lowerIndex + ']';
130 }
131}
Note: See TracBrowser for help on using the repository browser.