1 package org.opentrafficsim.road.network.lane;
2
3 import java.io.Serializable;
4
5 import org.djutils.exceptions.Throw;
6 import org.opentrafficsim.base.HierarchicalType;
7 import org.opentrafficsim.core.compatibility.Compatibility;
8 import org.opentrafficsim.core.compatibility.GTUCompatibility;
9 import org.opentrafficsim.core.gtu.GTUDirectionality;
10 import org.opentrafficsim.core.gtu.GTUType;
11 import org.opentrafficsim.core.network.LongitudinalDirectionality;
12 import org.opentrafficsim.road.network.RoadNetwork;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class LaneType extends HierarchicalType<LaneType> implements Serializable, Compatibility<GTUType, LaneType>
29 {
30
31 private static final long serialVersionUID = 20140821L;
32
33
34 private final GTUCompatibility<LaneType> compatibility;
35
36
37 private final RoadNetwork network;
38
39
40 public enum DEFAULTS
41 {
42
43 NONE("NONE"),
44
45
46 TWO_WAY_LANE("TWO_WAY_LANE"),
47
48
49 ONE_WAY_LANE("ONE_WAY_LANE"),
50
51
52 FREEWAY("FREEWAY"),
53
54
55 HIGHWAY("HIGHWAY"),
56
57
58 RURAL_ROAD_LANE("RURAL_ROAD_LANE"),
59
60
61 URBAN_ROAD_LANE("URBAN_ROAD_LANE"),
62
63
64 RESIDENTIAL_ROAD_LANE("RESIDENTIAL_ROAD_LANE"),
65
66
67 BUS_LANE("BUS_LANE"),
68
69
70 MOPED_PATH("MOPED_PATH"),
71
72
73 BICYCLE_PATH("BICYCLE_PATH"),
74
75
76 FOOTPATH("FOOTPATH");
77
78
79 private final String id;
80
81
82
83
84
85 DEFAULTS(final String id)
86 {
87 this.id = id;
88 }
89
90
91 public String getId()
92 {
93 return this.id;
94 }
95 }
96
97
98
99
100
101
102
103
104
105
106 public LaneType(final String id, final GTUCompatibility<LaneType> compatibility, final RoadNetwork network)
107 throws NullPointerException
108 {
109 super(id);
110 Throw.whenNull(compatibility, "compatibility collection cannot be null for LaneType with id = %s", id);
111 Throw.whenNull(network, "network cannot be null for LaneType with id = %s", id);
112 this.compatibility = new GTUCompatibility<>(compatibility);
113 this.network = network;
114 this.network.addLaneType(this);
115 }
116
117
118
119
120
121
122
123
124
125
126
127 public LaneType(final String id, final LaneType parent, final GTUCompatibility<LaneType> compatibility,
128 final RoadNetwork network) throws NullPointerException
129 {
130 super(id, parent);
131 Throw.whenNull(compatibility, "compatibility collection cannot be null for LaneType with id = %s", id);
132 Throw.whenNull(parent, "parent cannot be null for LaneType with id = %s", id);
133 this.compatibility = new GTUCompatibility<>(compatibility);
134 this.network = network;
135 this.network.addLaneType(this);
136 }
137
138
139
140
141
142
143
144 private LaneType(final String id, final boolean inverted, final RoadNetwork network)
145 {
146 super(id);
147 this.compatibility = null;
148 this.network = network;
149 this.network.addLaneType(this);
150 }
151
152
153
154
155
156
157 public boolean isOfType(final DEFAULTS type)
158 {
159 if (this.getId().equals(type.getId()))
160 {
161 return true;
162 }
163 if (getParent() != null)
164 {
165 return getParent().isOfType(type);
166 }
167 return false;
168 }
169
170
171
172
173
174 public final LaneType inv()
175 {
176 return new LaneType(getId(), true, getNetwork());
177 }
178
179
180
181
182 public GTUCompatibility<LaneType> getCompatibility()
183 {
184 return this.compatibility;
185 }
186
187
188
189
190 public RoadNetwork getNetwork()
191 {
192 return this.network;
193 }
194
195
196
197
198
199
200
201
202 @Override
203 public final Boolean isCompatible(final GTUType gtuType, final GTUDirectionality direction)
204 {
205
206
207 return getDirectionality(gtuType).permits(direction);
208 }
209
210
211
212
213
214
215 public final LongitudinalDirectionality getDirectionality(final GTUType gtuType)
216 {
217 LongitudinalDirectionality result = this.compatibility.getDirectionality(gtuType, true);
218 if (null == this.compatibility)
219 {
220 return result.invert();
221 }
222 return result;
223 }
224
225
226
227
228
229
230 public final void addGtuCompatability(final GTUType gtuType, final LongitudinalDirectionality direction)
231 {
232 if (null == this.compatibility)
233 {
234 getParent().addGtuCompatability(gtuType, direction.invert());
235 }
236 else
237 {
238 this.compatibility.addAllowedGTUType(gtuType, direction);
239 }
240 }
241
242
243 @Override
244 @SuppressWarnings("checkstyle:designforextension")
245 public String toString()
246 {
247 return "LaneType [id=" + this.getId() + ", compatibilitySet=" + this.compatibility + "]";
248 }
249
250
251 @Override
252 @SuppressWarnings("checkstyle:designforextension")
253 public int hashCode()
254 {
255 final int prime = 31;
256 int result = super.hashCode();
257 result = prime * result + ((this.compatibility == null) ? 0 : this.compatibility.hashCode());
258 return result;
259 }
260
261
262 @Override
263 @SuppressWarnings("checkstyle:designforextension")
264 public boolean equals(final Object obj)
265 {
266 if (this == obj)
267 {
268 return true;
269 }
270 if (!super.equals(obj))
271 {
272 return false;
273 }
274 if (getClass() != obj.getClass())
275 {
276 return false;
277 }
278 LaneType other = (LaneType) obj;
279 if (this.compatibility == null)
280 {
281 if (other.compatibility != null)
282 {
283 return false;
284 }
285 }
286 else if (!this.compatibility.equals(other.compatibility))
287 {
288 return false;
289 }
290 return true;
291 }
292
293
294 @Override
295 public final LongitudinalDirectionality getDirectionality(final GTUType gtuType, final boolean tryParentsOfGTUType)
296 {
297 LongitudinalDirectionality result = this.compatibility.getDirectionality(gtuType, tryParentsOfGTUType);
298 if (null == this.compatibility)
299 {
300 return result.invert();
301 }
302 return result;
303 }
304
305 }