View Javadoc
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   * Set of trajectories to be accepted or rejected for a query.
14   * <p>
15   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
17   * <p>
18   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 30 sep. 2016 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @author <a href="http://www.transport.citg.tudelft.nl">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 HashMap<>();
37  
38      /**
39       * Adds a {@code Trajectory} with the {@code TrajectoryGroup} it is from to the accept list.
40       * @param trajectory Trajectory&lt;?&gt;; {@code Trajectory} trajectory
41       * @param trajectoryGroup TrajectoryGroup; {@code TrajectoryGroup} trajectories
42       * @throws IllegalArgumentException if the {@code Trajectory} is not within the {@code TrajectoryGroup}
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          // This is quite a costly check
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       * @return number of trajectories
63       */
64      public final int size()
65      {
66          return this.trajectoryList.size();
67      }
68  
69      /**
70       * @param i int; number of {@code trajectory} to get
71       * @return i'th {@code trajectory}
72       * @throws IndexOutOfBoundsException if the index is out of range (<tt>index &lt; 0 || index &gt;= size()</tt>)
73       */
74      public final Trajectory<?> getTrajectory(final int i)
75      {
76          return this.trajectoryList.get(i);
77      }
78  
79      /**
80       * @param i int; number of {@code TrajectoryGroup} to get
81       * @return i'th {@code TrajectoryGroup}
82       * @throws IndexOutOfBoundsException if the index is out of range (<tt>index &lt; 0 || index &gt;= size()</tt>)
83       */
84      public final TrajectoryGroup getTrajectoryGroup(final int i)
85      {
86          return this.trajectoryGroupList.get(i);
87      }
88  
89      /**
90       * @return iterator over {@code trajectory}'s, does not allow removal
91       */
92      public final Iterator<Trajectory<?>> getTrajectoryIterator()
93      {
94          return new ImmutableIterator<>(this.trajectoryList.iterator());
95      }
96  
97      /**
98       * @return iterator over {@code TrajectoryGroup}'s, does not allow removal
99       */
100     public final Iterator<TrajectoryGroup> getTrajectoryGroupIterator()
101     {
102         return new ImmutableIterator<>(this.trajectoryGroupList.iterator());
103     }
104 
105     /**
106      * Accept given trajectory.
107      * @param trajectory Trajectory&lt;?&gt;; trajectory to accept
108      * @throws IllegalArgumentException if the trajectory is not part of the trajectory accept list
109      */
110     public final void acceptTrajectory(final Trajectory<?> trajectory)
111     {
112         acceptTrajectory(trajectory, true);
113     }
114 
115     /**
116      * Reject given trajectory.
117      * @param trajectory Trajectory&lt;?&gt;; trajectory to reject
118      * @throws IllegalArgumentException if the trajectory is not part of the trajectory accept list
119      */
120     public final void rejectTrajectory(final Trajectory<?> trajectory)
121     {
122         acceptTrajectory(trajectory, false);
123     }
124 
125     /**
126      * Accept or reject given trajectory.
127      * @param trajectory Trajectory&lt;?&gt;; trajectory to accept or reject
128      * @param accept boolean; whether to accept the trajectory
129      * @throws IllegalArgumentException if the trajectory is not part of the trajectory accept list
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      * Accept all trajectories.
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      * Reject all trajectories.
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      * Returns whether the given trajectory is accepted or not. If this was not determined, it is {@code false} by default.
162      * @param trajectory Trajectory&lt;?&gt;; trajectory
163      * @return whether the given trajectory is accepted or not
164      * @throws IllegalArgumentException if the trajectory is not part of the trajectory accept list
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     /** {@inheritDoc} */
174     @Override
175     public final String toString()
176     {
177         return "TrajectoryAcceptList [gtuId=" + this.gtuId + ", " + this.trajectoryList.size() + " trajectories]";
178     }
179 
180 }