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-2020 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          init();
72      }
73  
74      /**
75       * Speed sign active all day.
76       * @param id String; id
77       * @param lane Lane; lane
78       * @param direction LongitudinalDirectionality; direction
79       * @param longitudinalPosition Length; longitudinal position
80       * @param simulator SimulatorInterface.TimeDoubleUnit; simulator
81       * @param speed Speed; speed
82       * @param gtuType GTUType; GTU type
83       * @throws NetworkException when the position on the lane is out of bounds
84       */
85      public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
86              final Length longitudinalPosition, final SimulatorInterface.TimeDoubleUnit simulator, final Speed speed,
87              final GTUType gtuType) throws NetworkException
88      {
89          this(id, lane, direction, longitudinalPosition, simulator, speed, gtuType, Duration.ZERO, ENDOFDAY);
90      }
91  
92      /**
93       * Speed sign for all GTU types.
94       * @param id String; id
95       * @param lane Lane; lane
96       * @param direction LongitudinalDirectionality; direction
97       * @param longitudinalPosition Length; longitudinal position
98       * @param simulator SimulatorInterface.TimeDoubleUnit; simulator
99       * @param speed Speed; speed
100      * @param startTimeOfDay Duration; start time-of-day
101      * @param endTimeOfDay Duration; end time-of-day
102      * @throws NetworkException when the position on the lane is out of bounds
103      */
104     @SuppressWarnings("checkstyle:parameternumber")
105     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
106             final Length longitudinalPosition, final SimulatorInterface.TimeDoubleUnit simulator, final Speed speed,
107             final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
108     {
109         this(id, lane, direction, longitudinalPosition, simulator, speed,
110                 lane.getNetwork().getGtuType(GTUType.DEFAULTS.VEHICLE), startTimeOfDay, endTimeOfDay);
111     }
112 
113     /**
114      * Speed sign active all day for all GTU types.
115      * @param id String; id
116      * @param lane Lane; lane
117      * @param direction LongitudinalDirectionality; direction
118      * @param longitudinalPosition Length; longitudinal position
119      * @param simulator SimulatorInterface.TimeDoubleUnit; simulator
120      * @param speed Speed; speed
121      * @throws NetworkException when the position on the lane is out of bounds
122      */
123     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
124             final Length longitudinalPosition, final SimulatorInterface.TimeDoubleUnit simulator, final Speed speed)
125             throws NetworkException
126     {
127         this(id, lane, direction, longitudinalPosition, simulator, speed,
128                 lane.getNetwork().getGtuType(GTUType.DEFAULTS.VEHICLE), Duration.ZERO, ENDOFDAY);
129     }
130 
131     /**
132      * Return whether this speed limit is currently active.
133      * @param gtuTypeIn GTUType; GTU type
134      * @param time Duration; current time-of-day
135      * @return whether this speed limit is currently active
136      */
137     public final boolean isActive(final GTUType gtuTypeIn, final Duration time)
138     {
139         return gtuTypeIn.isOfType(this.gtuType) && time.ge(this.startTimeOfDay) && time.le(this.endTimeOfDay);
140     }
141 
142     /**
143      * Returns the speed.
144      * @return the speed
145      */
146     public final Speed getSpeed()
147     {
148         return this.speed;
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public final AbstractLaneBasedObject clone(final CrossSectionElement newCSE,
154             final SimulatorInterface.TimeDoubleUnit newSimulator) throws NetworkException
155     {
156         return new SpeedSign(getId(), (Lane) newCSE, getDirection(), getLongitudinalPosition(), newSimulator, this.speed,
157                 this.gtuType, this.startTimeOfDay, this.endTimeOfDay);
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     public int hashCode()
163     {
164         final int prime = 31;
165         int result = 1;
166         result = prime * result + ((this.endTimeOfDay == null) ? 0 : this.endTimeOfDay.hashCode());
167         result = prime * result + ((this.gtuType == null) ? 0 : this.gtuType.hashCode());
168         result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
169         result = prime * result + ((this.startTimeOfDay == null) ? 0 : this.startTimeOfDay.hashCode());
170         return result;
171     }
172 
173     /** {@inheritDoc} */
174     @Override
175     public boolean equals(final Object obj)
176     {
177         if (this == obj)
178         {
179             return true;
180         }
181         if (obj == null)
182         {
183             return false;
184         }
185         if (getClass() != obj.getClass())
186         {
187             return false;
188         }
189         SpeedSign/../../../../../org/opentrafficsim/road/network/lane/object/SpeedSign.html#SpeedSign">SpeedSign other = (SpeedSign) obj;
190         if (this.endTimeOfDay == null)
191         {
192             if (other.endTimeOfDay != null)
193             {
194                 return false;
195             }
196         }
197         else if (!this.endTimeOfDay.equals(other.endTimeOfDay))
198         {
199             return false;
200         }
201         if (this.gtuType == null)
202         {
203             if (other.gtuType != null)
204             {
205                 return false;
206             }
207         }
208         else if (!this.gtuType.equals(other.gtuType))
209         {
210             return false;
211         }
212         if (this.speed == null)
213         {
214             if (other.speed != null)
215             {
216                 return false;
217             }
218         }
219         else if (!this.speed.equals(other.speed))
220         {
221             return false;
222         }
223         if (this.startTimeOfDay == null)
224         {
225             if (other.startTimeOfDay != null)
226             {
227                 return false;
228             }
229         }
230         else if (!this.startTimeOfDay.equals(other.startTimeOfDay))
231         {
232             return false;
233         }
234         return true;
235     }
236 
237     /** {@inheritDoc} */
238     @Override
239     public final String toString()
240     {
241         return "SpeedSign [speed=" + this.speed + ", gtuType=" + this.gtuType + ", startTime=" + this.startTimeOfDay
242                 + ", endTime=" + this.endTimeOfDay + "]";
243     }
244 
245 }