View Javadoc
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   * Speed sign.
21   * <p>
22   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
24   * <p>
25   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 20 apr. 2017 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
29   */
30  public class SpeedSign extends AbstractLaneBasedObject
31  {
32  
33      /** */
34      private static final long serialVersionUID = 20170420L;
35  
36      /** End of day. */
37      private static final Duration ENDOFDAY = new Duration(24, DurationUnit.HOUR);
38  
39      /** Speed limit. */
40      private final Speed speed;
41  
42      /** GTU type. */
43      private final GTUType gtuType;
44  
45      /** Start time-of-day. */
46      private final Duration startTimeOfDay;
47  
48      /** End time-of-day. */
49      private final Duration endTimeOfDay;
50  
51      /**
52       * Speed sign.
53       * @param id id
54       * @param lane lane
55       * @param direction direction
56       * @param longitudinalPosition longitudinal position
57       * @param simulator simulator
58       * @param speed speed
59       * @param gtuType GTU type
60       * @param startTimeOfDay start time-of-day
61       * @param endTimeOfDay end time-of-day
62       * @throws NetworkException when the position on the lane is out of bounds
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       * Speed sign active all day.
87       * @param id id
88       * @param lane lane
89       * @param direction direction
90       * @param longitudinalPosition longitudinal position
91       * @param simulator simulator
92       * @param speed speed
93       * @param gtuType GTU type
94       * @throws NetworkException when the position on the lane is out of bounds
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      * Speed sign for all GTU types.
105      * @param id id
106      * @param lane lane
107      * @param direction direction
108      * @param longitudinalPosition longitudinal position
109      * @param simulator simulator
110      * @param speed speed
111      * @param startTimeOfDay start time-of-day
112      * @param endTimeOfDay end time-of-day
113      * @throws NetworkException when the position on the lane is out of bounds
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      * Speed sign active all day for all GTU types.
125      * @param id id
126      * @param lane lane
127      * @param direction direction
128      * @param longitudinalPosition longitudinal position
129      * @param simulator simulator
130      * @param speed speed
131      * @throws NetworkException when the position on the lane is out of bounds
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      * Return whether this speed limit is currently active.
141      * @param gtuTypeIn GTU type
142      * @param time current time-of-day
143      * @return whether this speed limit is currently active
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      * Returns the speed.
152      * @return the speed
153      */
154     public Speed getSpeed()
155     {
156         return this.speed;
157     }
158 
159     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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 }