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