View Javadoc
1   package org.opentrafficsim.road.network.lane.object;
2   
3   import org.djunits.value.vdouble.scalar.Duration;
4   import org.djunits.value.vdouble.scalar.Length;
5   import org.djunits.value.vdouble.scalar.Speed;
6   import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
7   import org.opentrafficsim.core.gtu.GtuType;
8   import org.opentrafficsim.core.network.NetworkException;
9   import org.opentrafficsim.road.network.lane.Lane;
10  
11  /**
12   * Speed sign.
13   * <p>
14   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * </p>
17   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
19   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
20   */
21  public class SpeedSign extends AbstractLaneBasedObject
22  {
23  
24      /** */
25      private static final long serialVersionUID = 20170420L;
26  
27      /** Speed limit. */
28      private final Speed speed;
29  
30      /** GTU type. */
31      private final GtuType gtuType;
32  
33      /** Start time-of-day. */
34      private final Duration startTimeOfDay;
35  
36      /** End time-of-day. */
37      private final Duration endTimeOfDay;
38  
39      /**
40       * Construct a new SpeedSign.
41       * @param id String; the id of the new SpeedSign
42       * @param lane Lane; Lane on/over which the SpeedSign is positioned
43       * @param longitudinalPosition Length; the longitudinal position along the lane of the new SpeedSign
44       * @param simulator OtsSimulatorInterface; the simulator
45       * @param speed Speed; the speed limit shown by the new SpeedSign
46       * @param gtuType GtuType; GTU type that should obey the speed sign
47       * @param startTimeOfDay Duration; start time-of-day
48       * @param endTimeOfDay Duration; end time-of-day
49       * @throws NetworkException when the position on the lane is out of bounds
50       */
51      @SuppressWarnings("checkstyle:parameternumber")
52      public SpeedSign(final String id, final Lane lane, final Length longitudinalPosition, final OtsSimulatorInterface simulator,
53              final Speed speed, final GtuType gtuType, final Duration startTimeOfDay, final Duration endTimeOfDay)
54              throws NetworkException
55      {
56          super(id, lane, longitudinalPosition, LaneBasedObject.makeGeometry(lane, longitudinalPosition));
57          this.speed = speed;
58          this.gtuType = gtuType;
59          this.startTimeOfDay = startTimeOfDay;
60          this.endTimeOfDay = endTimeOfDay;
61  
62          init();
63      }
64  
65      /**
66       * Return whether this speed limit is currently active.
67       * @param gtuTypeIn GtuType; GTU type
68       * @param time Duration; current time-of-day
69       * @return whether this speed limit is currently active
70       */
71      public final boolean isActive(final GtuType gtuTypeIn, final Duration time)
72      {
73          return gtuTypeIn.isOfType(this.gtuType) && time.ge(this.startTimeOfDay) && time.le(this.endTimeOfDay);
74      }
75  
76      /**
77       * Returns the speed.
78       * @return the speed
79       */
80      public final Speed getSpeed()
81      {
82          return this.speed;
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public int hashCode()
88      {
89          final int prime = 31;
90          int result = 1;
91          result = prime * result + ((this.endTimeOfDay == null) ? 0 : this.endTimeOfDay.hashCode());
92          result = prime * result + ((this.gtuType == null) ? 0 : this.gtuType.hashCode());
93          result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
94          result = prime * result + ((this.startTimeOfDay == null) ? 0 : this.startTimeOfDay.hashCode());
95          return result;
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public boolean equals(final Object obj)
101     {
102         if (this == obj)
103         {
104             return true;
105         }
106         if (obj == null)
107         {
108             return false;
109         }
110         if (getClass() != obj.getClass())
111         {
112             return false;
113         }
114         SpeedSign other = (SpeedSign) obj;
115         if (this.endTimeOfDay == null)
116         {
117             if (other.endTimeOfDay != null)
118             {
119                 return false;
120             }
121         }
122         else if (!this.endTimeOfDay.equals(other.endTimeOfDay))
123         {
124             return false;
125         }
126         if (this.gtuType == null)
127         {
128             if (other.gtuType != null)
129             {
130                 return false;
131             }
132         }
133         else if (!this.gtuType.equals(other.gtuType))
134         {
135             return false;
136         }
137         if (this.speed == null)
138         {
139             if (other.speed != null)
140             {
141                 return false;
142             }
143         }
144         else if (!this.speed.equals(other.speed))
145         {
146             return false;
147         }
148         if (this.startTimeOfDay == null)
149         {
150             if (other.startTimeOfDay != null)
151             {
152                 return false;
153             }
154         }
155         else if (!this.startTimeOfDay.equals(other.startTimeOfDay))
156         {
157             return false;
158         }
159         return true;
160     }
161 
162     /** {@inheritDoc} */
163     @Override
164     public final String toString()
165     {
166         return "SpeedSign [speed=" + this.speed + ", gtuType=" + this.gtuType + ", startTime=" + this.startTimeOfDay
167                 + ", endTime=" + this.endTimeOfDay + "]";
168     }
169 
170 }