View Javadoc
1   package org.opentrafficsim.road.network.lane.object;
2   
3   import org.djunits.unit.DurationUnit;
4   import org.djunits.value.vdouble.scalar.Duration;
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djunits.value.vdouble.scalar.Speed;
7   import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
8   import org.opentrafficsim.core.gtu.GTUType;
9   import org.opentrafficsim.core.network.LongitudinalDirectionality;
10  import org.opentrafficsim.core.network.NetworkException;
11  import org.opentrafficsim.road.network.lane.CrossSectionElement;
12  import org.opentrafficsim.road.network.lane.Lane;
13  
14  /**
15   * Speed sign.
16   * <p>
17   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 20 apr. 2017 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public class SpeedSign extends AbstractLaneBasedObject
26  {
27  
28      /** */
29      private static final long serialVersionUID = 20170420L;
30  
31      /** End of day. */
32      private static final Duration ENDOFDAY = new Duration(24, DurationUnit.HOUR);
33  
34      /** Speed limit. */
35      private final Speed speed;
36  
37      /** GTU type. */
38      private final GTUType gtuType;
39  
40      /** Start time-of-day. */
41      private final Duration startTimeOfDay;
42  
43      /** End time-of-day. */
44      private final Duration endTimeOfDay;
45  
46      /**
47       * Construct a new SpeedSign.
48       * @param id String; the id of the new SpeedSign
49       * @param lane Lane; Lane on/over which the SpeedSign is positioned
50       * @param direction LongitudinalDirectionality; driving direction for which the new SpeedSign applies
51       * @param longitudinalPosition Length; the longitudinal position along the lane of the new SpeedSign
52       * @param simulator OTSSimulatorInterface; the simulator
53       * @param speed Speed; the speed limit shown by the new SpeedSign
54       * @param gtuType GTUType; GTU type that should obey the speed sign
55       * @param startTimeOfDay Duration; start time-of-day
56       * @param endTimeOfDay Duration; end time-of-day
57       * @throws NetworkException when the position on the lane is out of bounds
58       */
59      @SuppressWarnings("checkstyle:parameternumber")
60      public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
61              final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed,
62              final GTUType gtuType, final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
63      {
64          super(id, lane, direction, longitudinalPosition, LaneBasedObject.makeGeometry(lane, longitudinalPosition));
65          this.speed = speed;
66          this.gtuType = gtuType;
67          this.startTimeOfDay = startTimeOfDay;
68          this.endTimeOfDay = endTimeOfDay;
69          
70          init();
71      }
72  
73      /**
74       * Speed sign active all day.
75       * @param id String; id
76       * @param lane Lane; lane
77       * @param direction LongitudinalDirectionality; direction
78       * @param longitudinalPosition Length; longitudinal position
79       * @param simulator OTSSimulatorInterface; simulator
80       * @param speed Speed; speed
81       * @param gtuType GTUType; GTU type
82       * @throws NetworkException when the position on the lane is out of bounds
83       */
84      public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
85              final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed,
86              final GTUType gtuType) throws NetworkException
87      {
88          this(id, lane, direction, longitudinalPosition, simulator, speed, gtuType, Duration.ZERO, ENDOFDAY);
89      }
90  
91      /**
92       * Speed sign for all GTU types.
93       * @param id String; id
94       * @param lane Lane; lane
95       * @param direction LongitudinalDirectionality; direction
96       * @param longitudinalPosition Length; longitudinal position
97       * @param simulator OTSSimulatorInterface; simulator
98       * @param speed Speed; speed
99       * @param startTimeOfDay Duration; start time-of-day
100      * @param endTimeOfDay Duration; end time-of-day
101      * @throws NetworkException when the position on the lane is out of bounds
102      */
103     @SuppressWarnings("checkstyle:parameternumber")
104     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
105             final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed,
106             final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
107     {
108         this(id, lane, direction, longitudinalPosition, simulator, speed,
109                 lane.getNetwork().getGtuType(GTUType.DEFAULTS.VEHICLE), startTimeOfDay, endTimeOfDay);
110     }
111 
112     /**
113      * Speed sign active all day for all GTU types.
114      * @param id String; id
115      * @param lane Lane; lane
116      * @param direction LongitudinalDirectionality; direction
117      * @param longitudinalPosition Length; longitudinal position
118      * @param simulator OTSSimulatorInterface; simulator
119      * @param speed Speed; speed
120      * @throws NetworkException when the position on the lane is out of bounds
121      */
122     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
123             final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed)
124             throws NetworkException
125     {
126         this(id, lane, direction, longitudinalPosition, simulator, speed,
127                 lane.getNetwork().getGtuType(GTUType.DEFAULTS.VEHICLE), Duration.ZERO, ENDOFDAY);
128     }
129 
130     /**
131      * Return whether this speed limit is currently active.
132      * @param gtuTypeIn GTUType; GTU type
133      * @param time Duration; current time-of-day
134      * @return whether this speed limit is currently active
135      */
136     public final boolean isActive(final GTUType gtuTypeIn, final Duration time)
137     {
138         return gtuTypeIn.isOfType(this.gtuType) && time.ge(this.startTimeOfDay) && time.le(this.endTimeOfDay);
139     }
140 
141     /**
142      * Returns the speed.
143      * @return the speed
144      */
145     public final Speed getSpeed()
146     {
147         return this.speed;
148     }
149 
150     /** {@inheritDoc} */
151     @Override
152     public final AbstractLaneBasedObject clone(final CrossSectionElement newCSE,
153             final OTSSimulatorInterface newSimulator) throws NetworkException
154     {
155         return new SpeedSign(getId(), (Lane) newCSE, getDirection(), getLongitudinalPosition(), newSimulator, this.speed,
156                 this.gtuType, this.startTimeOfDay, this.endTimeOfDay);
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public int hashCode()
162     {
163         final int prime = 31;
164         int result = 1;
165         result = prime * result + ((this.endTimeOfDay == null) ? 0 : this.endTimeOfDay.hashCode());
166         result = prime * result + ((this.gtuType == null) ? 0 : this.gtuType.hashCode());
167         result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
168         result = prime * result + ((this.startTimeOfDay == null) ? 0 : this.startTimeOfDay.hashCode());
169         return result;
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public boolean equals(final Object obj)
175     {
176         if (this == obj)
177         {
178             return true;
179         }
180         if (obj == null)
181         {
182             return false;
183         }
184         if (getClass() != obj.getClass())
185         {
186             return false;
187         }
188         SpeedSign other = (SpeedSign) obj;
189         if (this.endTimeOfDay == null)
190         {
191             if (other.endTimeOfDay != null)
192             {
193                 return false;
194             }
195         }
196         else if (!this.endTimeOfDay.equals(other.endTimeOfDay))
197         {
198             return false;
199         }
200         if (this.gtuType == null)
201         {
202             if (other.gtuType != null)
203             {
204                 return false;
205             }
206         }
207         else if (!this.gtuType.equals(other.gtuType))
208         {
209             return false;
210         }
211         if (this.speed == null)
212         {
213             if (other.speed != null)
214             {
215                 return false;
216             }
217         }
218         else if (!this.speed.equals(other.speed))
219         {
220             return false;
221         }
222         if (this.startTimeOfDay == null)
223         {
224             if (other.startTimeOfDay != null)
225             {
226                 return false;
227             }
228         }
229         else if (!this.startTimeOfDay.equals(other.startTimeOfDay))
230         {
231             return false;
232         }
233         return true;
234     }
235 
236     /** {@inheritDoc} */
237     @Override
238     public final String toString()
239     {
240         return "SpeedSign [speed=" + this.speed + ", gtuType=" + this.gtuType + ", startTime=" + this.startTimeOfDay
241                 + ", endTime=" + this.endTimeOfDay + "]";
242     }
243 
244 }