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 public static final LinkType NONE;
38
39
40 public static final LinkType ROAD;
41
42
43 public static final LinkType FREEWAY;
44
45
46 public static final LinkType WATER_WAY;
47
48
49 public static final LinkType RAIL_WAY;
50
51
52 public static final LinkType CONNECTOR;
53
54 static
55 {
56 GTUCompatibility<LinkType> compatibility = new GTUCompatibility<>((LinkType) null);
57 NONE = new LinkType("NONE", null, compatibility);
58
59 compatibility = new GTUCompatibility<>((LinkType) null);
60 compatibility.addAllowedGTUType(GTUType.ROAD_USER, LongitudinalDirectionality.DIR_BOTH);
61 ROAD = new LinkType("ROAD", null, compatibility);
62
63 compatibility = new GTUCompatibility<>((LinkType) null);
64 compatibility.addAllowedGTUType(GTUType.ROAD_USER, LongitudinalDirectionality.DIR_PLUS);
65 compatibility.addAllowedGTUType(GTUType.PEDESTRIAN, LongitudinalDirectionality.DIR_NONE);
66 compatibility.addAllowedGTUType(GTUType.BICYCLE, LongitudinalDirectionality.DIR_NONE);
67 FREEWAY = new LinkType("FREEWAY", ROAD, compatibility);
68
69 compatibility = new GTUCompatibility<>((LinkType) null);
70 compatibility.addAllowedGTUType(GTUType.WATER_WAY_USER, LongitudinalDirectionality.DIR_BOTH);
71 WATER_WAY = new LinkType("WATER_WAY", null, compatibility);
72
73 compatibility = new GTUCompatibility<>((LinkType) null);
74 compatibility.addAllowedGTUType(GTUType.RAIL_WAY_USER, LongitudinalDirectionality.DIR_BOTH);
75 RAIL_WAY = new LinkType("WATER_WAY", null, compatibility);
76
77 compatibility = new GTUCompatibility<>((LinkType) null);
78 compatibility.addAllowedGTUType(GTUType.ROAD_USER, LongitudinalDirectionality.DIR_PLUS);
79 compatibility.addAllowedGTUType(GTUType.WATER_WAY_USER, LongitudinalDirectionality.DIR_PLUS);
80 compatibility.addAllowedGTUType(GTUType.RAIL_WAY_USER, LongitudinalDirectionality.DIR_PLUS);
81 CONNECTOR = new LinkType("CONNECTOR", null, compatibility);
82 }
83
84
85
86
87
88
89
90
91 public LinkType(final String id, final LinkType parent, final GTUCompatibility<LinkType> compatibility)
92 {
93 super(id, parent);
94 this.compatibility = new GTUCompatibility<>(compatibility);
95 }
96
97
98 @Override
99 public Boolean isCompatible(final GTUType gtuType, final GTUDirectionality directionality)
100 {
101 return this.compatibility.isCompatible(gtuType, directionality);
102 }
103
104
105
106
107
108 public final LinkType reverse()
109 {
110 if (this.reversed == null)
111 {
112 this.reversed = new ReversedLinkType(this);
113 }
114 return this.reversed;
115 }
116
117
118
119
120 public final boolean isNone()
121 {
122 return this.equals(NONE);
123 }
124
125
126
127
128 public final boolean isRoad()
129 {
130 return this.equals(ROAD);
131 }
132
133
134
135
136 public final boolean isWaterWay()
137 {
138 return this.equals(WATER_WAY);
139 }
140
141
142
143
144 public final boolean isRailWay()
145 {
146 return this.equals(RAIL_WAY);
147 }
148
149
150
151
152 public final boolean isConnector()
153 {
154 return this.equals(CONNECTOR);
155 }
156
157
158 @Override
159 @SuppressWarnings("checkstyle:designforextension")
160 public String toString()
161 {
162 return "LinkType [id=" + getId() + ", compatibilitySet=" + this.compatibility + "]";
163 }
164
165
166 @Override
167 public final LongitudinalDirectionality getDirectionality(final GTUType gtuType, final boolean tryParentsOfGTUType)
168 {
169 return this.compatibility.getDirectionality(gtuType, tryParentsOfGTUType);
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184 private class ReversedLinkType extends LinkType
185 {
186
187
188 private static final long serialVersionUID = 20180824L;
189
190
191 private final LinkType original;
192
193
194
195
196
197 ReversedLinkType(final LinkType original)
198 {
199 super(original.getId() + "_rev", original.getParent().reverse(), new GTUCompatibility<>((LinkType) null));
200 this.original = original;
201 }
202
203
204 @Override
205 public Boolean isCompatible(final GTUType gtuType, final GTUDirectionality directionality)
206 {
207 return this.original.isCompatible(gtuType, directionality.flip());
208 }
209
210
211 @Override
212 public String toString()
213 {
214 return "ReversedLinkType [original=" + this.original + "]";
215 }
216
217 }
218
219 }