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