SpeedSign.java

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

  2. import org.djunits.unit.DurationUnit;
  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.LongitudinalDirectionality;
  9. import org.opentrafficsim.core.network.NetworkException;
  10. import org.opentrafficsim.road.network.lane.CrossSectionElement;
  11. import org.opentrafficsim.road.network.lane.Lane;

  12. /**
  13.  * Speed sign.
  14.  * <p>
  15.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  16.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  17.  * <p>
  18.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 20 apr. 2017 <br>
  19.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  20.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  21.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  22.  */
  23. public class SpeedSign extends AbstractLaneBasedObject
  24. {

  25.     /** */
  26.     private static final long serialVersionUID = 20170420L;

  27.     /** End of day. */
  28.     private static final Duration ENDOFDAY = new Duration(24, DurationUnit.HOUR);

  29.     /** Speed limit. */
  30.     private final Speed speed;

  31.     /** GTU type. */
  32.     private final GTUType gtuType;

  33.     /** Start time-of-day. */
  34.     private final Duration startTimeOfDay;

  35.     /** End time-of-day. */
  36.     private final Duration endTimeOfDay;

  37.     /**
  38.      * Construct a new SpeedSign.
  39.      * @param id String; the id of the new SpeedSign
  40.      * @param lane Lane; Lane on/over which the SpeedSign is positioned
  41.      * @param direction LongitudinalDirectionality; driving direction for which the new SpeedSign applies
  42.      * @param longitudinalPosition Length; the longitudinal position along the lane of the new SpeedSign
  43.      * @param simulator OTSSimulatorInterface; the simulator
  44.      * @param speed Speed; the speed limit shown by the new SpeedSign
  45.      * @param gtuType GTUType; GTU type that should obey the speed sign
  46.      * @param startTimeOfDay Duration; start time-of-day
  47.      * @param endTimeOfDay Duration; end time-of-day
  48.      * @throws NetworkException when the position on the lane is out of bounds
  49.      */
  50.     @SuppressWarnings("checkstyle:parameternumber")
  51.     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
  52.             final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed,
  53.             final GTUType gtuType, final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
  54.     {
  55.         super(id, lane, direction, longitudinalPosition, LaneBasedObject.makeGeometry(lane, longitudinalPosition));
  56.         this.speed = speed;
  57.         this.gtuType = gtuType;
  58.         this.startTimeOfDay = startTimeOfDay;
  59.         this.endTimeOfDay = endTimeOfDay;
  60.        
  61.         init();
  62.     }

  63.     /**
  64.      * Speed sign active all day.
  65.      * @param id String; id
  66.      * @param lane Lane; lane
  67.      * @param direction LongitudinalDirectionality; direction
  68.      * @param longitudinalPosition Length; longitudinal position
  69.      * @param simulator OTSSimulatorInterface; simulator
  70.      * @param speed Speed; speed
  71.      * @param gtuType GTUType; GTU type
  72.      * @throws NetworkException when the position on the lane is out of bounds
  73.      */
  74.     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
  75.             final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed,
  76.             final GTUType gtuType) throws NetworkException
  77.     {
  78.         this(id, lane, direction, longitudinalPosition, simulator, speed, gtuType, Duration.ZERO, ENDOFDAY);
  79.     }

  80.     /**
  81.      * Speed sign for all GTU types.
  82.      * @param id String; id
  83.      * @param lane Lane; lane
  84.      * @param direction LongitudinalDirectionality; direction
  85.      * @param longitudinalPosition Length; longitudinal position
  86.      * @param simulator OTSSimulatorInterface; simulator
  87.      * @param speed Speed; speed
  88.      * @param startTimeOfDay Duration; start time-of-day
  89.      * @param endTimeOfDay Duration; end time-of-day
  90.      * @throws NetworkException when the position on the lane is out of bounds
  91.      */
  92.     @SuppressWarnings("checkstyle:parameternumber")
  93.     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
  94.             final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed,
  95.             final Duration startTimeOfDay, final Duration endTimeOfDay) throws NetworkException
  96.     {
  97.         this(id, lane, direction, longitudinalPosition, simulator, speed,
  98.                 lane.getNetwork().getGtuType(GTUType.DEFAULTS.VEHICLE), startTimeOfDay, endTimeOfDay);
  99.     }

  100.     /**
  101.      * Speed sign active all day for all GTU types.
  102.      * @param id String; id
  103.      * @param lane Lane; lane
  104.      * @param direction LongitudinalDirectionality; direction
  105.      * @param longitudinalPosition Length; longitudinal position
  106.      * @param simulator OTSSimulatorInterface; simulator
  107.      * @param speed Speed; speed
  108.      * @throws NetworkException when the position on the lane is out of bounds
  109.      */
  110.     public SpeedSign(final String id, final Lane lane, final LongitudinalDirectionality direction,
  111.             final Length longitudinalPosition, final OTSSimulatorInterface simulator, final Speed speed)
  112.             throws NetworkException
  113.     {
  114.         this(id, lane, direction, longitudinalPosition, simulator, speed,
  115.                 lane.getNetwork().getGtuType(GTUType.DEFAULTS.VEHICLE), Duration.ZERO, ENDOFDAY);
  116.     }

  117.     /**
  118.      * Return whether this speed limit is currently active.
  119.      * @param gtuTypeIn GTUType; GTU type
  120.      * @param time Duration; current time-of-day
  121.      * @return whether this speed limit is currently active
  122.      */
  123.     public final boolean isActive(final GTUType gtuTypeIn, final Duration time)
  124.     {
  125.         return gtuTypeIn.isOfType(this.gtuType) && time.ge(this.startTimeOfDay) && time.le(this.endTimeOfDay);
  126.     }

  127.     /**
  128.      * Returns the speed.
  129.      * @return the speed
  130.      */
  131.     public final Speed getSpeed()
  132.     {
  133.         return this.speed;
  134.     }

  135.     /** {@inheritDoc} */
  136.     @Override
  137.     public final AbstractLaneBasedObject clone(final CrossSectionElement newCSE,
  138.             final OTSSimulatorInterface newSimulator) throws NetworkException
  139.     {
  140.         return new SpeedSign(getId(), (Lane) newCSE, getDirection(), getLongitudinalPosition(), newSimulator, this.speed,
  141.                 this.gtuType, this.startTimeOfDay, this.endTimeOfDay);
  142.     }

  143.     /** {@inheritDoc} */
  144.     @Override
  145.     public int hashCode()
  146.     {
  147.         final int prime = 31;
  148.         int result = 1;
  149.         result = prime * result + ((this.endTimeOfDay == null) ? 0 : this.endTimeOfDay.hashCode());
  150.         result = prime * result + ((this.gtuType == null) ? 0 : this.gtuType.hashCode());
  151.         result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode());
  152.         result = prime * result + ((this.startTimeOfDay == null) ? 0 : this.startTimeOfDay.hashCode());
  153.         return result;
  154.     }

  155.     /** {@inheritDoc} */
  156.     @Override
  157.     public boolean equals(final Object obj)
  158.     {
  159.         if (this == obj)
  160.         {
  161.             return true;
  162.         }
  163.         if (obj == null)
  164.         {
  165.             return false;
  166.         }
  167.         if (getClass() != obj.getClass())
  168.         {
  169.             return false;
  170.         }
  171.         SpeedSign other = (SpeedSign) obj;
  172.         if (this.endTimeOfDay == null)
  173.         {
  174.             if (other.endTimeOfDay != null)
  175.             {
  176.                 return false;
  177.             }
  178.         }
  179.         else if (!this.endTimeOfDay.equals(other.endTimeOfDay))
  180.         {
  181.             return false;
  182.         }
  183.         if (this.gtuType == null)
  184.         {
  185.             if (other.gtuType != null)
  186.             {
  187.                 return false;
  188.             }
  189.         }
  190.         else if (!this.gtuType.equals(other.gtuType))
  191.         {
  192.             return false;
  193.         }
  194.         if (this.speed == null)
  195.         {
  196.             if (other.speed != null)
  197.             {
  198.                 return false;
  199.             }
  200.         }
  201.         else if (!this.speed.equals(other.speed))
  202.         {
  203.             return false;
  204.         }
  205.         if (this.startTimeOfDay == null)
  206.         {
  207.             if (other.startTimeOfDay != null)
  208.             {
  209.                 return false;
  210.             }
  211.         }
  212.         else if (!this.startTimeOfDay.equals(other.startTimeOfDay))
  213.         {
  214.             return false;
  215.         }
  216.         return true;
  217.     }

  218.     /** {@inheritDoc} */
  219.     @Override
  220.     public final String toString()
  221.     {
  222.         return "SpeedSign [speed=" + this.speed + ", gtuType=" + this.gtuType + ", startTime=" + this.startTimeOfDay
  223.                 + ", endTime=" + this.endTimeOfDay + "]";
  224.     }

  225. }