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