1 package org.opentrafficsim.core.network;
2
3 import java.io.Serializable;
4
5 import org.opentrafficsim.base.HierarchicalType;
6 import org.opentrafficsim.base.Identifiable;
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
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class LinkType extends HierarchicalType<LinkType> implements Serializable, Identifiable, Compatibility<GTUType, LinkType>
26 {
27
28 private static final long serialVersionUID = 20140821L;
29
30
31 private final GTUCompatibility<LinkType> compatibility;
32
33
34 private LinkType reversed = null;
35
36
37 private final Network network;
38
39
40 public enum DEFAULTS
41 {
42
43 NONE("NONE"),
44
45
46 ROAD("ROAD"),
47
48
49 FREEWAY("FREEWAY"),
50
51
52 WATERWAY("WATERWAY"),
53
54
55 RAILWAY("RAILWAY"),
56
57
58 CONNECTOR("CONNECTOR");
59
60
61 private final String id;
62
63
64
65
66
67 DEFAULTS(final String id)
68 {
69 this.id = id;
70 }
71
72
73 public String getId()
74 {
75 return this.id;
76 }
77 }
78
79
80
81
82
83
84
85
86
87 public LinkType(final String id, final LinkType parent, final GTUCompatibility<LinkType> compatibility,
88 final Network network)
89 {
90 super(id, parent);
91 this.compatibility = new GTUCompatibility<>(compatibility);
92 this.network = network;
93 this.network.addLinkType(this);
94 }
95
96
97
98
99
100
101 public boolean isOfType(final DEFAULTS type)
102 {
103 if (this.getId().equals(type.getId()))
104 {
105 return true;
106 }
107 if (getParent() != null)
108 {
109 return getParent().isOfType(type);
110 }
111 return false;
112 }
113
114
115 @Override
116 public Boolean isCompatible(final GTUType gtuType, final GTUDirectionality directionality)
117 {
118 return this.compatibility.isCompatible(gtuType, directionality);
119 }
120
121
122
123
124 public GTUCompatibility<LinkType> getCompatibility()
125 {
126 return this.compatibility;
127 }
128
129
130
131
132
133 public final LinkType reverse()
134 {
135 if (this.reversed == null)
136 {
137 this.reversed = new ReversedLinkType(this);
138 }
139 return this.reversed;
140 }
141
142
143
144
145 public final boolean isNone()
146 {
147 return this.getId().equals(DEFAULTS.NONE.getId());
148 }
149
150
151
152
153 public final boolean isRoad()
154 {
155 return this.getId().equals(DEFAULTS.ROAD.getId());
156 }
157
158
159
160
161 public final boolean isWaterWay()
162 {
163 return this.getId().equals(DEFAULTS.WATERWAY.getId());
164 }
165
166
167
168
169 public final boolean isRailWay()
170 {
171 return this.getId().equals(DEFAULTS.RAILWAY.getId());
172 }
173
174
175
176
177 public final boolean isConnector()
178 {
179 return this.getId().equals(DEFAULTS.CONNECTOR.getId());
180 }
181
182
183
184
185 public Network getNetwork()
186 {
187 return this.network;
188 }
189
190
191 @Override
192 @SuppressWarnings("checkstyle:designforextension")
193 public String toString()
194 {
195 return "LinkType [id=" + getId() + ", compatibilitySet=" + this.compatibility + "]";
196 }
197
198
199 @Override
200 public final LongitudinalDirectionality getDirectionality(final GTUType gtuType, final boolean tryParentsOfGTUType)
201 {
202 return this.compatibility.getDirectionality(gtuType, tryParentsOfGTUType);
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217 private class ReversedLinkType extends LinkType
218 {
219
220
221 private static final long serialVersionUID = 20180824L;
222
223
224 private final LinkType original;
225
226
227
228
229
230 ReversedLinkType(final LinkType original)
231 {
232 super(original.getId() + "_rev", original.getParent().reverse(), new GTUCompatibility<>((LinkType) null),
233 original.getNetwork());
234 this.original = original;
235 }
236
237
238 @Override
239 public Boolean isCompatible(final GTUType gtuType, final GTUDirectionality directionality)
240 {
241 return this.original.isCompatible(gtuType, directionality.flip());
242 }
243
244
245 @Override
246 public String toString()
247 {
248 return "ReversedLinkType [original=" + this.original + "]";
249 }
250
251 }
252
253 }