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      /** Speed limit. */
24      private final Speed speed;
25  
26      /** GTU type. */
27      private final GtuType gtuType;
28  
29      /** Start time-of-day. */
30      private final Duration startTimeOfDay;
31  
32      /** End time-of-day. */
33      private final Duration endTimeOfDay;
34  
35      /**
36       * Construct a new SpeedSign.
37       * @param id the id of the new SpeedSign
38       * @param lane Lane on/over which the SpeedSign is positioned
39       * @param longitudinalPosition the longitudinal position along the lane of the new SpeedSign
40       * @param speed the speed limit shown by the new SpeedSign
41       * @param gtuType GTU type that should obey the speed sign
42       * @param startTimeOfDay start time-of-day
43       * @param endTimeOfDay end time-of-day
44       * @throws NetworkException when the position on the lane is out of bounds
45       */
46      @SuppressWarnings("checkstyle:parameternumber")
47      public SpeedSign(final String id, final Lane lane, final Length longitudinalPosition, final Speed speed,
48              final GtuType gtuType, final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
49      {
50          super(id, lane, longitudinalPosition, LaneBasedObject.makeLine(lane, longitudinalPosition));
51          this.speed = speed;
52          this.gtuType = gtuType;
53          this.startTimeOfDay = startTimeOfDay;
54          this.endTimeOfDay = endTimeOfDay;
55  
56          init();
57      }
58  
59      /**
60       * Return whether this speed limit is currently active.
61       * @param gtuTypeIn GTU type
62       * @param time current time-of-day
63       * @return whether this speed limit is currently active
64       */
65      public final boolean isActive(final GtuType gtuTypeIn, final Duration time)
66      {
67          return gtuTypeIn.isOfType(this.gtuType) && time.ge(this.startTimeOfDay) && time.le(this.endTimeOfDay);
68      }
69  
70      /**
71       * Returns the speed.
72       * @return the speed
73       */
74      public final Speed getSpeed()
75      {
76          return this.speed;
77      }
78  
79      @Override
80      public int hashCode()
81      {
82          final int prime = 31;
83          int result = 1;
84          result = prime * result + ((this.endTimeOfDay == null) ? 0 : this.endTimeOfDay.hashCode());
85          result = prime * result + ((this.gtuType == null) ? 0 : this.gtuType.hashCode());
86          result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
87          result = prime * result + ((this.startTimeOfDay == null) ? 0 : this.startTimeOfDay.hashCode());
88          return result;
89      }
90  
91      @Override
92      public boolean equals(final Object obj)
93      {
94          if (this == obj)
95          {
96              return true;
97          }
98          if (obj == null)
99          {
100             return false;
101         }
102         if (getClass() != obj.getClass())
103         {
104             return false;
105         }
106         SpeedSign other = (SpeedSign) obj;
107         if (this.endTimeOfDay == null)
108         {
109             if (other.endTimeOfDay != null)
110             {
111                 return false;
112             }
113         }
114         else if (!this.endTimeOfDay.equals(other.endTimeOfDay))
115         {
116             return false;
117         }
118         if (this.gtuType == null)
119         {
120             if (other.gtuType != null)
121             {
122                 return false;
123             }
124         }
125         else if (!this.gtuType.equals(other.gtuType))
126         {
127             return false;
128         }
129         if (this.speed == null)
130         {
131             if (other.speed != null)
132             {
133                 return false;
134             }
135         }
136         else if (!this.speed.equals(other.speed))
137         {
138             return false;
139         }
140         if (this.startTimeOfDay == null)
141         {
142             if (other.startTimeOfDay != null)
143             {
144                 return false;
145             }
146         }
147         else if (!this.startTimeOfDay.equals(other.startTimeOfDay))
148         {
149             return false;
150         }
151         return true;
152     }
153 
154     @Override
155     public final String toString()
156     {
157         return "SpeedSign [speed=" + this.speed + ", gtuType=" + this.gtuType + ", startTime=" + this.startTimeOfDay
158                 + ", endTime=" + this.endTimeOfDay + "]";
159     }
160 
161 }