1 package org.opentrafficsim.road.gtu.lane.perception;
2
3 import java.io.Serializable;
4
5 import org.djutils.exceptions.Throw;
6 import org.opentrafficsim.core.network.LateralDirectionality;
7
8
9
10
11
12
13
14
15
16
17 public class RelativeLane implements Comparable<RelativeLane>, Serializable
18 {
19
20
21 private static final long serialVersionUID = 20160502L;
22
23
24 public static final RelativeLaneperception/RelativeLane.html#RelativeLane">RelativeLane SECOND_LEFT = new RelativeLane(LateralDirectionality.LEFT, 2);
25
26
27 public static final RelativeLaneu/lane/perception/RelativeLane.html#RelativeLane">RelativeLane LEFT = new RelativeLane(LateralDirectionality.LEFT, 1);
28
29
30 public static final RelativeLaneane/perception/RelativeLane.html#RelativeLane">RelativeLane CURRENT = new RelativeLane(LateralDirectionality.NONE, 0);
31
32
33 public static final RelativeLane/lane/perception/RelativeLane.html#RelativeLane">RelativeLane RIGHT = new RelativeLane(LateralDirectionality.RIGHT, 1);
34
35
36 public static final RelativeLaneerception/RelativeLane.html#RelativeLane">RelativeLane SECOND_RIGHT = new RelativeLane(LateralDirectionality.RIGHT, 2);
37
38
39
40
41
42 private final int rank;
43
44
45
46
47
48 private RelativeLane(final int rank)
49 {
50 this.rank = rank;
51 }
52
53
54
55
56
57
58
59
60 public RelativeLane(final LateralDirectionality lat, final int numLanes)
61 {
62 Throw.whenNull(lat, "Lateral directionality may not be null.");
63 Throw.when(lat.isNone() && numLanes != 0, IllegalArgumentException.class,
64 "Number of lanes must be zero if the lateral directionality is NONE.");
65 Throw.when((!lat.isNone()) && numLanes <= 0, IllegalArgumentException.class,
66 "Relative lane with %d lanes in %s direction is not allowed, use values > 0.", numLanes, lat);
67 this.rank = lat.isLeft() ? -numLanes : numLanes;
68 }
69
70
71
72
73
74 public final LateralDirectionality getLateralDirectionality()
75 {
76
77 return this.rank == 0 ? LateralDirectionality.NONE
78 : this.rank < 0 ? LateralDirectionality.LEFT : LateralDirectionality.RIGHT;
79 }
80
81
82
83
84
85 public final int getNumLanes()
86 {
87 return Math.abs(this.rank);
88 }
89
90
91
92
93
94 public final boolean isSecondLeft()
95 {
96 return this.equals(SECOND_LEFT);
97 }
98
99
100
101
102
103 public final boolean isLeft()
104 {
105 return this.equals(LEFT);
106 }
107
108
109
110
111
112 public final boolean isCurrent()
113 {
114 return this.equals(CURRENT);
115 }
116
117
118
119
120
121 public final boolean isRight()
122 {
123 return this.equals(RIGHT);
124 }
125
126
127
128
129
130 public final boolean isSecondRight()
131 {
132 return this.equals(SECOND_RIGHT);
133 }
134
135
136
137
138
139 public final RelativeLane getLeft()
140 {
141 return this.add(LEFT);
142 }
143
144
145
146
147
148 public final RelativeLane getRight()
149 {
150 return this.add(RIGHT);
151 }
152
153
154
155
156
157
158
159 public final RelativeLanetu/lane/perception/RelativeLane.html#RelativeLane">RelativeLane add(final RelativeLane relativeLane)
160 {
161 return new RelativeLane(this.rank + relativeLane.rank);
162 }
163
164
165 @Override
166 public int hashCode()
167 {
168 final int prime = 31;
169 int result = 1;
170 result = prime * result + this.rank;
171 return result;
172 }
173
174
175 @Override
176 public boolean equals(final Object obj)
177 {
178 if (this == obj)
179 {
180 return true;
181 }
182 if (obj == null)
183 {
184 return false;
185 }
186 if (getClass() != obj.getClass())
187 {
188 return false;
189 }
190 RelativeLane/../../../../org/opentrafficsim/road/gtu/lane/perception/RelativeLane.html#RelativeLane">RelativeLane other = (RelativeLane) obj;
191 if (this.rank != other.rank)
192 {
193 return false;
194 }
195 return true;
196 }
197
198
199 @Override
200 public final String toString()
201 {
202 if (this.equals(CURRENT))
203 {
204 return "RelativeLane [CURRENT]";
205 }
206 return new StringBuilder("RelativeLane [").append(getLateralDirectionality()).append(", ").append(getNumLanes())
207 .append("]").toString();
208 }
209
210
211 @Override
212 public final int compareTo(final RelativeLane rel)
213 {
214 return this.rank - rel.rank;
215 }
216
217 }