HeadwayTrafficLight.java

  1. package org.opentrafficsim.road.gtu.lane.perception.headway;

  2. import org.djunits.value.vdouble.scalar.Length;
  3. import org.djutils.exceptions.Throw;
  4. import org.opentrafficsim.core.gtu.GtuException;
  5. import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
  6. import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;

  7. /**
  8.  * Container for a reference to information about a (lane based) traffic light and a headway to the traffic light.
  9.  * <p>
  10.  * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  11.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  12.  * </p>
  13.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  14.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  15.  */
  16. public class HeadwayTrafficLight extends AbstractHeadwayLaneBasedObject
  17. {
  18.     /** */
  19.     private static final long serialVersionUID = 20160410L;

  20.     /** the traffic light object for further observation, can not be null. */
  21.     private final TrafficLight trafficLight;

  22.     /**
  23.      * Construct a new Headway information object, for a traffic light ahead of us (or behind us, although that does not seem
  24.      * very useful).
  25.      * @param trafficLight TrafficLight; the traffic light object for further observation, can not be null.
  26.      * @param distance Length; the distance to the traffic light, distance cannot be null.
  27.      * @throws GtuException when id is null, or parameters are inconsistent
  28.      */
  29.     public HeadwayTrafficLight(final TrafficLight trafficLight, final Length distance) throws GtuException
  30.     {
  31.         super(ObjectType.TRAFFICLIGHT, id(trafficLight), distance, trafficLight.getLane());
  32.         this.trafficLight = trafficLight;
  33.     }

  34.     /**
  35.      * Get the id of the traffic light; throw an exception if traffic light is null.
  36.      * @param trafficLight TrafficLight; the traffic light object for further observation, can not be null.
  37.      * @return he id of the traffic light.
  38.      * @throws GtuException when the trafficLight object is null
  39.      */
  40.     private static String id(final TrafficLight trafficLight) throws GtuException
  41.     {
  42.         Throw.when(trafficLight == null, GtuException.class, "Headway constructor: trafficLight == null");
  43.         return trafficLight.getId();
  44.     }

  45.     /**
  46.      * @return the traffic light color.
  47.      */
  48.     public final TrafficLightColor getTrafficLightColor()
  49.     {
  50.         return this.trafficLight.getTrafficLightColor();
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public final String toString()
  55.     {
  56.         return "HeadwayTrafficLight [trafficLight=" + this.trafficLight + "]";
  57.     }
  58. }