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
47 public final void addTrajectory(final Trajectory<?> trajectory, final TrajectoryGroup<?> trajectoryGroup)
48 {
49 Throw.whenNull(trajectory, "Trajectory may not be null.");
50 Throw.whenNull(trajectoryGroup, "Trajectory group may not be null.");
51
52
53
54 Throw.when(this.gtuId != null && !this.gtuId.equals(trajectory.getGtuId()), IllegalArgumentException.class,
55 "Trajectories of different GTU's may not be in a single trajectory accept list.");
56 this.gtuId = trajectory.getGtuId();
57 this.trajectoryList.add(trajectory);
58 this.trajectoryGroupList.add(trajectoryGroup);
59 this.trajectoryMap.put(trajectory, false);
60 }
61
62
63
64
65
66 public final int size()
67 {
68 return this.trajectoryList.size();
69 }
70
71
72
73
74
75
76
77 public final Trajectory<?> getTrajectory(final int i)
78 {
79 return this.trajectoryList.get(i);
80 }
81
82
83
84
85
86
87
88 public final TrajectoryGroup<?> getTrajectoryGroup(final int i)
89 {
90 return this.trajectoryGroupList.get(i);
91 }
92
93
94
95
96
97 public final Iterator<Trajectory<?>> getTrajectoryIterator()
98 {
99 return new ImmutableIterator<>(this.trajectoryList.iterator());
100 }
101
102
103
104
105
106 public final Iterator<TrajectoryGroup<?>> getTrajectoryGroupIterator()
107 {
108 return new ImmutableIterator<>(this.trajectoryGroupList.iterator());
109 }
110
111
112
113
114
115
116 public final void acceptTrajectory(final Trajectory<?> trajectory)
117 {
118 acceptTrajectory(trajectory, true);
119 }
120
121
122
123
124
125
126 public final void rejectTrajectory(final Trajectory<?> trajectory)
127 {
128 acceptTrajectory(trajectory, false);
129 }
130
131
132
133
134
135
136
137 public final void acceptTrajectory(final Trajectory<?> trajectory, final boolean accept)
138 {
139 Throw.when(!this.trajectoryList.contains(trajectory), IllegalArgumentException.class,
140 "The trajectory is not part of the trajectory accept list.");
141 this.trajectoryMap.put(trajectory, accept);
142 }
143
144
145
146
147 public final void acceptAll()
148 {
149 for (Trajectory<?> trajectory : this.trajectoryList)
150 {
151 this.trajectoryMap.put(trajectory, true);
152 }
153 }
154
155
156
157
158 public final void rejectAll()
159 {
160 for (Trajectory<?> trajectory : this.trajectoryList)
161 {
162 this.trajectoryMap.put(trajectory, false);
163 }
164 }
165
166
167
168
169
170
171
172 public final boolean isAccepted(final Trajectory<?> trajectory)
173 {
174 Boolean out = this.trajectoryMap.get(trajectory);
175 Throw.when(out == null, IllegalArgumentException.class, "The trajectory is not part of the trajectory accept list.");
176 return out;
177 }
178
179
180 @Override
181 public final String toString()
182 {
183 return "TrajectoryAcceptList [gtuId=" + this.gtuId + ", " + this.trajectoryList.size() + " trajectories]";
184 }
185
186 }