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.opentrafficsim.base.immutablecollections.ImmutableIterator;
10
11 import nl.tudelft.simulation.language.Throw;
12
13
14
15
16
17
18
19
20
21
22
23
24 public class TrajectoryAcceptList
25 {
26
27
28 private String gtuId;
29
30
31 private final List<Trajectory> trajectoryList = new ArrayList<>();
32
33
34 private final List<TrajectoryGroup> trajectoryGroupList = new ArrayList<>();
35
36
37 private final Map<Trajectory, Boolean> trajectoryMap = new HashMap<>();
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 Throw.when(!trajectoryGroup.contains(trajectory), IllegalArgumentException.class,
52 "The trajectory should be contained within the trajectory group.");
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 Throw.when(!this.trajectoryList.contains(trajectory), IllegalArgumentException.class,
169 "The trajectory is not part of the trajectory accept list.");
170 return this.trajectoryMap.get(trajectory);
171 }
172
173
174 @Override
175 public final String toString()
176 {
177 return "TrajectoryAcceptList [gtuId=" + this.gtuId + ", " + this.trajectoryList.size() + " trajectories]";
178 }
179
180 }