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