1 package org.opentrafficsim.road.gtu.lane.perception;
2
3 import java.io.Serializable;
4
5 import org.djunits.value.vdouble.scalar.Length;
6 import org.djutils.exceptions.Throw;
7 import org.opentrafficsim.core.gtu.RelativePosition;
8 import org.opentrafficsim.core.network.LateralDirectionality;
9
10
11
12
13
14
15
16
17
18
19 public class InfrastructureLaneChangeInfo implements Comparable<InfrastructureLaneChangeInfo>, Serializable
20 {
21
22
23 private static final long serialVersionUID = 20160811L;
24
25
26 private final int requiredNumberOfLaneChanges;
27
28
29 private final LaneStructureRecord record;
30
31
32 private final Length afterStartLength;
33
34
35 private boolean deadEnd;
36
37
38 private final LateralDirectionality lat;
39
40
41
42
43
44
45 protected InfrastructureLaneChangeInfo(final int requiredNumberOfLaneChanges, final boolean deadEnd)
46 {
47 this.requiredNumberOfLaneChanges = requiredNumberOfLaneChanges;
48 this.record = null;
49 this.deadEnd = deadEnd;
50 this.afterStartLength = null;
51 this.lat = LateralDirectionality.NONE;
52 }
53
54
55
56
57
58
59
60
61
62
63
64 public InfrastructureLaneChangeInfo(final int requiredNumberOfLaneChanges, final LaneStructureRecord record,
65 final RelativePosition relativePosition, final boolean deadEnd, final LateralDirectionality lat)
66 {
67 Throw.when(requiredNumberOfLaneChanges < 0, IllegalArgumentException.class,
68 "Required number of lane changes may not be negative.");
69 Throw.whenNull(lat, "Lateral directionality may not be null.");
70 Throw.when(requiredNumberOfLaneChanges != 0 && lat.equals(LateralDirectionality.NONE), IllegalArgumentException.class,
71 "Lateral directionality may not be NONE for non-zero lane changes.");
72 Throw.whenNull(record, "Record may not be null.");
73 this.requiredNumberOfLaneChanges = requiredNumberOfLaneChanges;
74 this.record = record;
75 this.afterStartLength = this.record.getLane().getLength().minus(relativePosition.dx());
76 this.deadEnd = deadEnd;
77 this.lat = lat;
78 }
79
80
81
82
83 public final int getRequiredNumberOfLaneChanges()
84 {
85 return this.requiredNumberOfLaneChanges;
86 }
87
88
89
90
91 public Length getRemainingDistance()
92 {
93 return this.record.getStartDistance().plus(this.afterStartLength);
94 }
95
96
97
98
99 public final boolean isDeadEnd()
100 {
101 return this.deadEnd;
102 }
103
104
105
106
107
108 public final void setDeadEnd(final boolean deadEnd)
109 {
110 this.deadEnd = deadEnd;
111 }
112
113
114
115
116
117 public final LateralDirectionality getLateralDirectionality()
118 {
119 return this.lat;
120 }
121
122
123 @SuppressWarnings("checkstyle:designforextension")
124 @Override
125 public String toString()
126 {
127 return "InfrastructureLaneChangeInfo [requiredNumberOfLaneChanges=" + this.requiredNumberOfLaneChanges
128 + ", remainingDistance=" + getRemainingDistance() + "]";
129 }
130
131
132 @Override
133 public final int compareTo(final InfrastructureLaneChangeInfo infrastructureLaneChangeInfo)
134 {
135 return this.getRemainingDistance().compareTo(infrastructureLaneChangeInfo.getRemainingDistance());
136 }
137
138
139
140
141
142
143
144
145 public final InfrastructureLaneChangeInfo left(final LaneStructureRecord rec, final RelativePosition rel,
146 final boolean dead)
147 {
148 return new InfrastructureLaneChangeInfo(this.requiredNumberOfLaneChanges + 1, rec, rel, dead,
149 LateralDirectionality.LEFT);
150 }
151
152
153
154
155
156
157
158
159 public final InfrastructureLaneChangeInfo right(final LaneStructureRecord rec, final RelativePosition rel,
160 final boolean dead)
161 {
162 return new InfrastructureLaneChangeInfo(this.requiredNumberOfLaneChanges + 1, rec, rel, dead,
163 LateralDirectionality.RIGHT);
164 }
165
166
167
168
169
170
171 public static InfrastructureLaneChangeInfo fromInaccessibleLane(final boolean deadEnd)
172 {
173 return new InfrastructureLaneChangeInfoInaccessibleLane(deadEnd);
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187 private static class InfrastructureLaneChangeInfoInaccessibleLane extends InfrastructureLaneChangeInfo
188 {
189
190
191 private static final long serialVersionUID = 20180214L;
192
193
194
195
196 InfrastructureLaneChangeInfoInaccessibleLane(final boolean deadEnd)
197 {
198 super(1, deadEnd);
199 }
200
201
202 @Override
203 public Length getRemainingDistance()
204 {
205 return Length.ZERO;
206 }
207
208 }
209
210 }