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 * Set of trajectories to be accepted or rejected for a query. All the trajectories pertain to one GTU. A {@code Query} may
14 * reject or accept all, or a specific subset, based on the specific needs of different {@code FilterDataType}s.
15 * <p>
16 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18 * </p>
19 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
20 * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
21 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
22 */
23 public class TrajectoryAcceptList
24 {
25
26 /** GTU id of the contained trajectories. */
27 private String gtuId;
28
29 /** List of trajectory's. */
30 private final List<Trajectory<?>> trajectoryList = new ArrayList<>();
31
32 /** List of trajectory groups. */
33 private final List<TrajectoryGroup<?>> trajectoryGroupList = new ArrayList<>();
34
35 /** Map of trajectory's and acceptance boolean. */
36 private final Map<Trajectory<?>, Boolean> trajectoryMap = new LinkedHashMap<>();
37
38 /**
39 * Adds a {@code Trajectory} with the {@code TrajectoryGroup} it is from to the accept list. By default it is registered to
40 * be not accepted for a query.
41 * @param trajectory {@code Trajectory} trajectory
42 * @param trajectoryGroup {@code TrajectoryGroup} trajectories
43 * @throws IllegalArgumentException if the {@code Trajectory} belongs to a different GTU than an earlier provided
44 * {@code Trajectory}
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 * Returns the number of trajectories.
60 * @return number of trajectories
61 */
62 public final int size()
63 {
64 return this.trajectoryList.size();
65 }
66
67 /**
68 * Returns trajectory by index.
69 * @param i number of {@code trajectory} to get
70 * @return i'th {@code trajectory}
71 * @throws IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= size()})
72 */
73 public final Trajectory<?> getTrajectory(final int i)
74 {
75 return this.trajectoryList.get(i);
76 }
77
78 /**
79 * Returns a trajectory group by index.
80 * @param i number of {@code TrajectoryGroup} to get
81 * @return i'th {@code TrajectoryGroup}
82 * @throws IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= size()})
83 */
84 public final TrajectoryGroup<?> getTrajectoryGroup(final int i)
85 {
86 return this.trajectoryGroupList.get(i);
87 }
88
89 /**
90 * Returns an iterator over the trajectories.
91 * @return iterator over {@code trajectory}'s, does not allow removal
92 */
93 public final Iterator<Trajectory<?>> getTrajectoryIterator()
94 {
95 return new ImmutableIterator<>(this.trajectoryList.iterator());
96 }
97
98 /**
99 * Returns an iterator over the trajectory groups.
100 * @return iterator over {@code TrajectoryGroup}'s, does not allow removal
101 */
102 public final Iterator<TrajectoryGroup<?>> getTrajectoryGroupIterator()
103 {
104 return new ImmutableIterator<>(this.trajectoryGroupList.iterator());
105 }
106
107 /**
108 * Accept given trajectory.
109 * @param trajectory trajectory to accept
110 * @throws IllegalArgumentException if the trajectory is not part of the trajectory accept list
111 */
112 public final void acceptTrajectory(final Trajectory<?> trajectory)
113 {
114 acceptTrajectory(trajectory, true);
115 }
116
117 /**
118 * Reject given trajectory.
119 * @param trajectory trajectory to reject
120 * @throws IllegalArgumentException if the trajectory is not part of the trajectory accept list
121 */
122 public final void rejectTrajectory(final Trajectory<?> trajectory)
123 {
124 acceptTrajectory(trajectory, false);
125 }
126
127 /**
128 * Accept or reject given trajectory.
129 * @param trajectory trajectory to accept or reject
130 * @param accept whether to accept the trajectory
131 * @throws IllegalArgumentException if the trajectory is not part of the trajectory accept list
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 * Accept all trajectories.
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 * Reject all trajectories.
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 * Returns whether the given trajectory is accepted or not.
164 * @param trajectory trajectory
165 * @return whether the given trajectory is accepted or not
166 * @throws IllegalArgumentException if the trajectory is not part of the trajectory accept list
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 }