1 package org.opentrafficsim.road.network.lane.object;
2
3 import java.rmi.RemoteException;
4
5 import javax.naming.NamingException;
6
7 import org.djunits.unit.DurationUnit;
8 import org.djunits.value.vdouble.scalar.Duration;
9 import org.djunits.value.vdouble.scalar.Length;
10 import org.djunits.value.vdouble.scalar.Speed;
11 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
12 import org.opentrafficsim.core.gtu.GTUType;
13 import org.opentrafficsim.core.network.LongitudinalDirectionality;
14 import org.opentrafficsim.core.network.NetworkException;
15 import org.opentrafficsim.road.network.animation.SpeedSignAnimation;
16 import org.opentrafficsim.road.network.lane.CrossSectionElement;
17 import org.opentrafficsim.road.network.lane.Lane;
18
19
20
21
22
23
24
25
26
27
28
29
30 public class SpeedSign extends AbstractLaneBasedObject
31 {
32
33
34 private static final long serialVersionUID = 20170420L;
35
36
37 private static final Duration ENDOFDAY = new Duration(24, DurationUnit.HOUR);
38
39
40 private final Speed speed;
41
42
43 private final GTUType gtuType;
44
45
46 private final Duration startTimeOfDay;
47
48
49 private final Duration endTimeOfDay;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 @SuppressWarnings("checkstyle:parameternumber")
65 public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
66 final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed, final GTUType gtuType,
67 final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
68 {
69 super(id, lane, direction, longitudinalPosition, LaneBasedObject.makeGeometry(lane, longitudinalPosition));
70 this.speed = speed;
71 this.gtuType = gtuType;
72 this.startTimeOfDay = startTimeOfDay;
73 this.endTimeOfDay = endTimeOfDay;
74
75 try
76 {
77 new SpeedSignAnimation(this, simulator);
78 }
79 catch (RemoteException | NamingException exception)
80 {
81 throw new NetworkException(exception);
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96 public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
97 final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed, final GTUType gtuType)
98 throws NetworkException
99 {
100 this(id, lane, direction, longitudinalPosition, simulator, speed, gtuType, Duration.ZERO, ENDOFDAY);
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115 @SuppressWarnings("checkstyle:parameternumber")
116 public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
117 final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed,
118 final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
119 {
120 this(id, lane, direction, longitudinalPosition, simulator, speed, GTUType.ALL, startTimeOfDay, endTimeOfDay);
121 }
122
123
124
125
126
127
128
129
130
131
132
133 public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
134 final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed) throws NetworkException
135 {
136 this(id, lane, direction, longitudinalPosition, simulator, speed, GTUType.ALL, Duration.ZERO, ENDOFDAY);
137 }
138
139
140
141
142
143
144
145 public boolean isActive(final GTUType gtuTypeIn, final Duration time)
146 {
147 return gtuTypeIn.isOfType(this.gtuType) && time.ge(this.startTimeOfDay) && time.le(this.endTimeOfDay);
148 }
149
150
151
152
153
154 public Speed getSpeed()
155 {
156 return this.speed;
157 }
158
159
160 @Override
161 public AbstractLaneBasedObject clone(final CrossSectionElement newCSE, final OTSSimulatorInterface newSimulator,
162 final boolean animation) throws NetworkException
163 {
164 return new SpeedSign(getId(), (Lane) newCSE, getDirection(), getLongitudinalPosition(), newSimulator, this.speed,
165 this.gtuType, this.startTimeOfDay, this.endTimeOfDay);
166 }
167
168
169 @Override
170 public int hashCode()
171 {
172 final int prime = 31;
173 int result = 1;
174 result = prime * result + ((this.endTimeOfDay == null) ? 0 : this.endTimeOfDay.hashCode());
175 result = prime * result + ((this.gtuType == null) ? 0 : this.gtuType.hashCode());
176 result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
177 result = prime * result + ((this.startTimeOfDay == null) ? 0 : this.startTimeOfDay.hashCode());
178 return result;
179 }
180
181
182 @Override
183 public boolean equals(final Object obj)
184 {
185 if (this == obj)
186 {
187 return true;
188 }
189 if (obj == null)
190 {
191 return false;
192 }
193 if (getClass() != obj.getClass())
194 {
195 return false;
196 }
197 SpeedSign other = (SpeedSign) obj;
198 if (this.endTimeOfDay == null)
199 {
200 if (other.endTimeOfDay != null)
201 {
202 return false;
203 }
204 }
205 else if (!this.endTimeOfDay.equals(other.endTimeOfDay))
206 {
207 return false;
208 }
209 if (this.gtuType == null)
210 {
211 if (other.gtuType != null)
212 {
213 return false;
214 }
215 }
216 else if (!this.gtuType.equals(other.gtuType))
217 {
218 return false;
219 }
220 if (this.speed == null)
221 {
222 if (other.speed != null)
223 {
224 return false;
225 }
226 }
227 else if (!this.speed.equals(other.speed))
228 {
229 return false;
230 }
231 if (this.startTimeOfDay == null)
232 {
233 if (other.startTimeOfDay != null)
234 {
235 return false;
236 }
237 }
238 else if (!this.startTimeOfDay.equals(other.startTimeOfDay))
239 {
240 return false;
241 }
242 return true;
243 }
244
245
246 @Override
247 public String toString()
248 {
249 return "SpeedSign [speed=" + this.speed + ", gtuType=" + this.gtuType + ", startTime=" + this.startTimeOfDay + ", endTime="
250 + this.endTimeOfDay + "]";
251 }
252
253 }