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.opentrafficsim.base.immutablecollections.ImmutableIterator;
10  
11  import nl.tudelft.simulation.language.Throw;
12  
13  /**
14   * Set of trajectories to be accepted or rejected for a query.
15   * <p>
16   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
18   * <p>
19   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 30 sep. 2016 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   */
24  public class TrajectoryAcceptList
25  {
26  
27      /** GTU id of the contained trajectories. */
28      private String gtuId;
29  
30      /** List of trajectory's. */
31      private final List<Trajectory> trajectoryList = new ArrayList<>();
32  
33      /** List of trajectory groups. */
34      private final List<TrajectoryGroup> trajectoryGroupList = new ArrayList<>();
35  
36      /** Map of trajectory's and acceptance boolean. */
37      private final Map<Trajectory, Boolean> trajectoryMap = new HashMap<>();
38  
39      /**
40       * Adds a {@code Trajectory} with the {@code TrajectoryGroup} it is from to the accept list.
41       * @param trajectory {@code Trajectory} trajectory
42       * @param trajectoryGroup {@code TrajectoryGroup} trajectories
43       * @throws IllegalArgumentException if the {@code Trajectory} is not within the {@code TrajectoryGroup}
44       * @throws IllegalArgumentException if the {@code Trajectory} belongs to a different GTU than an earlier provided
45       *             {@code Trajectory}
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       * @return number of trajectories
63       */
64      public final int size()
65      {
66          return this.trajectoryList.size();
67      }
68  
69      /**
70       * @param i 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 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 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 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 to accept or reject
128      * @param accept 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
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         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     /** {@inheritDoc} */
174     @Override
175     public final String toString()
176     {
177         return "TrajectoryAcceptList [gtuId=" + this.gtuId + ", " + this.trajectoryList.size() + " trajectories]";
178     }
179 
180 }