SpeedSign.java

  1. package org.opentrafficsim.road.network.lane.object;

  2. import org.djunits.value.vdouble.scalar.Duration;
  3. import org.djunits.value.vdouble.scalar.Length;
  4. import org.djunits.value.vdouble.scalar.Speed;
  5. import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
  6. import org.opentrafficsim.core.gtu.GtuType;
  7. import org.opentrafficsim.core.network.NetworkException;
  8. import org.opentrafficsim.road.network.lane.Lane;

  9. /**
  10.  * Speed sign.
  11.  * <p>
  12.  * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  13.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  14.  * </p>
  15.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  16.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  17.  * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
  18.  */
  19. public class SpeedSign extends AbstractLaneBasedObject
  20. {

  21.     /** */
  22.     private static final long serialVersionUID = 20170420L;

  23.     /** Speed limit. */
  24.     private final Speed speed;

  25.     /** GTU type. */
  26.     private final GtuType gtuType;

  27.     /** Start time-of-day. */
  28.     private final Duration startTimeOfDay;

  29.     /** End time-of-day. */
  30.     private final Duration endTimeOfDay;

  31.     /**
  32.      * Construct a new SpeedSign.
  33.      * @param id String; the id of the new SpeedSign
  34.      * @param lane Lane; Lane on/over which the SpeedSign is positioned
  35.      * @param longitudinalPosition Length; the longitudinal position along the lane of the new SpeedSign
  36.      * @param simulator OtsSimulatorInterface; the simulator
  37.      * @param speed Speed; the speed limit shown by the new SpeedSign
  38.      * @param gtuType GtuType; GTU type that should obey the speed sign
  39.      * @param startTimeOfDay Duration; start time-of-day
  40.      * @param endTimeOfDay Duration; end time-of-day
  41.      * @throws NetworkException when the position on the lane is out of bounds
  42.      */
  43.     @SuppressWarnings("checkstyle:parameternumber")
  44.     public SpeedSign(final String id, final Lane lane, final Length longitudinalPosition, final OtsSimulatorInterface simulator,
  45.             final Speed speed, final GtuType gtuType, final Duration startTimeOfDay, final Duration endTimeOfDay)
  46.             throws NetworkException
  47.     {
  48.         super(id, lane, longitudinalPosition, LaneBasedObject.makeGeometry(lane, longitudinalPosition));
  49.         this.speed = speed;
  50.         this.gtuType = gtuType;
  51.         this.startTimeOfDay = startTimeOfDay;
  52.         this.endTimeOfDay = endTimeOfDay;

  53.         init();
  54.     }

  55.     /**
  56.      * Return whether this speed limit is currently active.
  57.      * @param gtuTypeIn GtuType; GTU type
  58.      * @param time Duration; current time-of-day
  59.      * @return whether this speed limit is currently active
  60.      */
  61.     public final boolean isActive(final GtuType gtuTypeIn, final Duration time)
  62.     {
  63.         return gtuTypeIn.isOfType(this.gtuType) && time.ge(this.startTimeOfDay) && time.le(this.endTimeOfDay);
  64.     }

  65.     /**
  66.      * Returns the speed.
  67.      * @return the speed
  68.      */
  69.     public final Speed getSpeed()
  70.     {
  71.         return this.speed;
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public int hashCode()
  76.     {
  77.         final int prime = 31;
  78.         int result = 1;
  79.         result = prime * result + ((this.endTimeOfDay == null) ? 0 : this.endTimeOfDay.hashCode());
  80.         result = prime * result + ((this.gtuType == null) ? 0 : this.gtuType.hashCode());
  81.         result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
  82.         result = prime * result + ((this.startTimeOfDay == null) ? 0 : this.startTimeOfDay.hashCode());
  83.         return result;
  84.     }

  85.     /** {@inheritDoc} */
  86.     @Override
  87.     public boolean equals(final Object obj)
  88.     {
  89.         if (this == obj)
  90.         {
  91.             return true;
  92.         }
  93.         if (obj == null)
  94.         {
  95.             return false;
  96.         }
  97.         if (getClass() != obj.getClass())
  98.         {
  99.             return false;
  100.         }
  101.         SpeedSign other = (SpeedSign) obj;
  102.         if (this.endTimeOfDay == null)
  103.         {
  104.             if (other.endTimeOfDay != null)
  105.             {
  106.                 return false;
  107.             }
  108.         }
  109.         else if (!this.endTimeOfDay.equals(other.endTimeOfDay))
  110.         {
  111.             return false;
  112.         }
  113.         if (this.gtuType == null)
  114.         {
  115.             if (other.gtuType != null)
  116.             {
  117.                 return false;
  118.             }
  119.         }
  120.         else if (!this.gtuType.equals(other.gtuType))
  121.         {
  122.             return false;
  123.         }
  124.         if (this.speed == null)
  125.         {
  126.             if (other.speed != null)
  127.             {
  128.                 return false;
  129.             }
  130.         }
  131.         else if (!this.speed.equals(other.speed))
  132.         {
  133.             return false;
  134.         }
  135.         if (this.startTimeOfDay == null)
  136.         {
  137.             if (other.startTimeOfDay != null)
  138.             {
  139.                 return false;
  140.             }
  141.         }
  142.         else if (!this.startTimeOfDay.equals(other.startTimeOfDay))
  143.         {
  144.             return false;
  145.         }
  146.         return true;
  147.     }

  148.     /** {@inheritDoc} */
  149.     @Override
  150.     public final String toString()
  151.     {
  152.         return "SpeedSign [speed=" + this.speed + ", gtuType=" + this.gtuType + ", startTime=" + this.startTimeOfDay
  153.                 + ", endTime=" + this.endTimeOfDay + "]";
  154.     }

  155. }