1 package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
7
8
9
10
11
12
13
14
15
16
17
18
19 public final class LmrsData
20 {
21
22
23 private final Synchronization synchronization;
24
25
26 private final Set<String> leaders = new HashSet<>();
27
28
29 private final Set<String> tempLeaders = new HashSet<>();
30
31
32 private String syncVehicle;
33
34
35
36
37 public LmrsData(final Synchronization synchronization)
38 {
39 this.synchronization = synchronization;
40 }
41
42
43
44
45
46
47 boolean isNewLeader(final HeadwayGTU gtu)
48 {
49 this.tempLeaders.add(gtu.getId());
50 return !this.leaders.contains(gtu.getId());
51 }
52
53
54
55
56 void finalizeStep()
57 {
58 this.leaders.clear();
59 this.leaders.addAll(this.tempLeaders);
60 this.tempLeaders.clear();
61 }
62
63
64
65
66
67 void setSyncVehicle(final HeadwayGTU gtu)
68 {
69 this.syncVehicle = gtu == null ? null : gtu.getId();
70 }
71
72
73
74
75
76
77 boolean isSyncVehicle(final HeadwayGTU gtu)
78 {
79 return this.syncVehicle == null ? false : gtu.getId().equals(this.syncVehicle);
80 }
81
82
83
84
85
86
87
88 HeadwayGTU getSyncVehicle(final Set<HeadwayGTU> adjLeaders)
89 {
90 if (this.syncVehicle == null)
91 {
92 return null;
93 }
94 for (HeadwayGTU leader : adjLeaders)
95 {
96 if (leader.getId().equals(this.syncVehicle))
97 {
98 return leader;
99 }
100 }
101 return null;
102 }
103
104
105
106
107
108 Synchronization getSynchronization()
109 {
110 return this.synchronization;
111 }
112
113 }