View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.object;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djutils.exceptions.Throw;
5   import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
6   import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
7   
8   /**
9    * Container for a reference to information about a (lane based) traffic light and a headway to the traffic light.
10   * <p>
11   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
13   * </p>
14   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
16   */
17  public class PerceivedTrafficLight extends PerceivedLaneBasedObjectBase
18  {
19  
20      /** The traffic light object for further observation, can not be null. */
21      private final TrafficLight trafficLight;
22  
23      /** Whether we can turn on red. */
24      private final boolean turnOnRed;
25  
26      /**
27       * Construct a new Headway information object, for a traffic light ahead of us (or behind us, although that does not seem
28       * very useful).
29       * @param trafficLight the traffic light object for further observation, can not be null.
30       * @param distance the distance to the traffic light, distance cannot be null.
31       * @param turnOnRed whether the perceiving GTU may turn on red.
32       */
33      public PerceivedTrafficLight(final TrafficLight trafficLight, final Length distance, final boolean turnOnRed)
34      {
35          super(id(trafficLight), ObjectType.TRAFFICLIGHT, Length.ZERO, Kinematics.staticAhead(distance), trafficLight.getLane());
36          this.trafficLight = trafficLight;
37          this.turnOnRed = turnOnRed;
38      }
39  
40      /**
41       * Get the id of the traffic light; throw an exception if traffic light is null.
42       * @param trafficLight the traffic light object for further observation, can not be null.
43       * @return he id of the traffic light.
44       */
45      private static String id(final TrafficLight trafficLight)
46      {
47          Throw.whenNull(trafficLight, "trafficLight");
48          return trafficLight.getId();
49      }
50  
51      /**
52       * Returns the traffic light color.
53       * @return the traffic light color.
54       */
55      public TrafficLightColor getTrafficLightColor()
56      {
57          return this.trafficLight.getTrafficLightColor();
58      }
59  
60      /**
61       * Whether the perceiving GTU may turn on red.
62       * @return whether the perceiving GTU may turn on red.
63       */
64      public boolean canTurnOnRed()
65      {
66          return this.turnOnRed;
67      }
68  
69      /**
70       * Returns the traffic light for sub classes.
71       * @return traffic light
72       */
73      protected TrafficLight getTrafficLight()
74      {
75          return this.trafficLight;
76      }
77  
78      @Override
79      public String toString()
80      {
81          return "HeadwayTrafficLight [trafficLight=" + this.trafficLight + "]";
82      }
83  }