1 package org.opentrafficsim.kpi.sampling;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.LinkedHashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.djutils.exceptions.Throw;
10 import org.djutils.immutablecollections.ImmutableIterator;
11
12
13
14
15
16
17
18
19
20
21
22
23 public class TrajectoryAcceptList
24 {
25
26
27 private String gtuId;
28
29
30 private final List<Trajectory<?>> trajectoryList = new ArrayList<>();
31
32
33 private final List<TrajectoryGroup<?>> trajectoryGroupList = new ArrayList<>();
34
35
36 private final Map<Trajectory<?>, Boolean> trajectoryMap = new LinkedHashMap<>();
37
38
39
40
41
42
43
44
45
46 public final void addTrajectory(final Trajectory<?> trajectory, final TrajectoryGroup<?> trajectoryGroup)
47 {
48 Throw.whenNull(trajectory, "Trajectory may not be null.");
49 Throw.whenNull(trajectoryGroup, "Trajectory group may not be null.");
50 Throw.when(this.gtuId != null && !this.gtuId.equals(trajectory.getGtuId()), IllegalArgumentException.class,
51 "Trajectories of different GTU's may not be in a single trajectory accept list.");
52 this.gtuId = trajectory.getGtuId();
53 this.trajectoryList.add(trajectory);
54 this.trajectoryGroupList.add(trajectoryGroup);
55 this.trajectoryMap.put(trajectory, false);
56 }
57
58
59
60
61
62 public final int size()
63 {
64 return this.trajectoryList.size();
65 }
66
67
68
69
70
71
72
73 public final Trajectory<?> getTrajectory(final int i)
74 {
75 return this.trajectoryList.get(i);
76 }
77
78
79
80
81
82
83
84 public final TrajectoryGroup<?> getTrajectoryGroup(final int i)
85 {
86 return this.trajectoryGroupList.get(i);
87 }
88
89
90
91
92
93 public final Iterator<Trajectory<?>> getTrajectoryIterator()
94 {
95 return new ImmutableIterator<>(this.trajectoryList.iterator());
96 }
97
98
99
100
101
102 public final Iterator<TrajectoryGroup<?>> getTrajectoryGroupIterator()
103 {
104 return new ImmutableIterator<>(this.trajectoryGroupList.iterator());
105 }
106
107
108
109
110
111
112 public final void acceptTrajectory(final Trajectory<?> trajectory)
113 {
114 acceptTrajectory(trajectory, true);
115 }
116
117
118
119
120
121
122 public final void rejectTrajectory(final Trajectory<?> trajectory)
123 {
124 acceptTrajectory(trajectory, false);
125 }
126
127
128
129
130
131
132
133 public final void acceptTrajectory(final Trajectory<?> trajectory, final boolean accept)
134 {
135 Throw.when(!this.trajectoryList.contains(trajectory), IllegalArgumentException.class,
136 "The trajectory is not part of the trajectory accept list.");
137 this.trajectoryMap.put(trajectory, accept);
138 }
139
140
141
142
143 public final void acceptAll()
144 {
145 for (Trajectory<?> trajectory : this.trajectoryList)
146 {
147 this.trajectoryMap.put(trajectory, true);
148 }
149 }
150
151
152
153
154 public final void rejectAll()
155 {
156 for (Trajectory<?> trajectory : this.trajectoryList)
157 {
158 this.trajectoryMap.put(trajectory, false);
159 }
160 }
161
162
163
164
165
166
167
168 public final boolean isAccepted(final Trajectory<?> trajectory)
169 {
170 Boolean out = this.trajectoryMap.get(trajectory);
171 Throw.when(out == null, IllegalArgumentException.class, "The trajectory is not part of the trajectory accept list.");
172 return out;
173 }
174
175 @Override
176 public final String toString()
177 {
178 return "TrajectoryAcceptList [gtuId=" + this.gtuId + ", " + this.trajectoryList.size() + " trajectories]";
179 }
180
181 }