1 package org.opentrafficsim.road.gtu.lane.perception.headway;
2
3 import org.djunits.value.vdouble.scalar.Length;
4 import org.djutils.exceptions.Throw;
5 import org.opentrafficsim.core.gtu.GtuException;
6 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
7 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
8
9 /**
10 * Container for a reference to information about a (lane based) traffic light and a headway to the traffic light.
11 * <p>
12 * Copyright (c) 2013-2024 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 */
18 public class HeadwayTrafficLight extends AbstractHeadwayLaneBasedObject
19 {
20 /** */
21 private static final long serialVersionUID = 20160410L;
22
23 /** the traffic light object for further observation, can not be null. */
24 private final TrafficLight trafficLight;
25
26 /** Whether we can turn on red. */
27 private final boolean turnOnRed;
28
29 /**
30 * Construct a new Headway information object, for a traffic light ahead of us (or behind us, although that does not seem
31 * very useful).
32 * @param trafficLight TrafficLight; the traffic light object for further observation, can not be null.
33 * @param distance Length; the distance to the traffic light, distance cannot be null.
34 * @param turnOnRed boolean; whether the perceiving GTU may turn on red.
35 * @throws GtuException when id is null, or parameters are inconsistent
36 */
37 public HeadwayTrafficLight(final TrafficLight trafficLight, final Length distance, final boolean turnOnRed)
38 throws GtuException
39 {
40 super(ObjectType.TRAFFICLIGHT, id(trafficLight), distance, trafficLight.getLane());
41 this.trafficLight = trafficLight;
42 this.turnOnRed = turnOnRed;
43 }
44
45 /**
46 * Get the id of the traffic light; throw an exception if traffic light is null.
47 * @param trafficLight TrafficLight; the traffic light object for further observation, can not be null.
48 * @return he id of the traffic light.
49 * @throws GtuException when the trafficLight object is null
50 */
51 private static String id(final TrafficLight trafficLight) throws GtuException
52 {
53 Throw.when(trafficLight == null, GtuException.class, "Headway constructor: trafficLight == null");
54 return trafficLight.getId();
55 }
56
57 /**
58 * @return the traffic light color.
59 */
60 public final TrafficLightColor getTrafficLightColor()
61 {
62 return this.trafficLight.getTrafficLightColor();
63 }
64
65 /**
66 * Whether the perceiving GTU may turn on red.
67 * @return boolean; whether the perceiving GTU may turn on red.
68 */
69 public final boolean canTurnOnRed()
70 {
71 return this.turnOnRed;
72 }
73
74 /** {@inheritDoc} */
75 @Override
76 public final String toString()
77 {
78 return "HeadwayTrafficLight [trafficLight=" + this.trafficLight + "]";
79 }
80 }