1 package org.opentrafficsim.road.network.lane.object.trafficlight;
2
3 import java.util.Set;
4 import java.util.SortedMap;
5 import java.util.TreeMap;
6
7 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
8
9
10
11
12
13
14
15
16
17
18
19
20
21 public abstract class AbstractTrafficLightController implements TrafficLightController
22 {
23
24 private final String id;
25
26
27 @SuppressWarnings("checkstyle:visibilitymodifier")
28 protected final DEVSSimulatorInterface.TimeDoubleUnit simulator;
29
30
31 private final SortedMap<Integer, Set<TrafficLight>> phases = new TreeMap<>();
32
33
34 @SuppressWarnings("checkstyle:visibilitymodifier")
35 protected int currentPhase;
36
37
38 @SuppressWarnings("checkstyle:visibilitymodifier")
39 protected TrafficLightColor currentColor = TrafficLightColor.RED;
40
41
42
43
44
45
46 public AbstractTrafficLightController(final String id, final DEVSSimulatorInterface.TimeDoubleUnit simulator)
47 {
48 this.id = id;
49 this.simulator = simulator;
50 }
51
52
53 @Override
54 public final int getNumberOfPhases()
55 {
56 return this.phases.size();
57 }
58
59
60 @Override
61 public final int getCurrentPhase()
62 {
63 return this.currentPhase;
64 }
65
66
67 @Override
68 public final void addTrafficLightToPhase(final int phaseId, final TrafficLight trafficLight) throws TrafficLightException
69 {
70 if (!this.phases.containsKey(phaseId))
71 {
72 throw new TrafficLightException("Phase " + phaseId + " for traffic light " + this.id + " was not defined");
73 }
74 this.phases.get(phaseId).add(trafficLight);
75 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
76 }
77
78
79 @Override
80 public final String getId()
81 {
82 return this.id;
83 }
84
85 }