View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import java.util.function.Supplier;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
7   
8   /**
9    * Iterable that additionally provides support for PerceptionCollectors. These gather raw data, to only 'perceive' the result.
10   * <p>
11   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
13   * <p>
14   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 mrt. 2018 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
18   * @param <H> headway type
19   * @param <U> underlying object type
20   */
21  public interface PerceptionCollectable<H extends Headway, U> extends PerceptionIterable<H>
22  {
23  
24      /**
25       * Collect the underlying objects in to a perceived result. This methodology is loosely based on Stream.collect().
26       * @param collector PerceptionCollector&lt;C, ? super U, I&gt;; collector
27       * @param <C> collection result type
28       * @param <I> intermediate type
29       * @return C; collection result
30       */
31      default <C, I> C collect(final PerceptionCollector<C, ? super U, I> collector)
32      {
33          return collect(collector.getIdentity(), collector.getAccumulator(), collector.getFinalizer());
34      }
35  
36      /**
37       * Collect the underlying objects in to a perceived result. This methodology is loosely based on Stream.collect().
38       * @param identity Supplier&lt;I&gt;; the initial intermediate result value
39       * @param accumulator PerceptionAccumulator&lt;? super U, I&gt;; accumulator
40       * @param finalizer PerceptionFinalizer&lt;C, I&gt;; finalizer
41       * @param <C> collection result type
42       * @param <I> intermediate type
43       * @return C; collection result
44       */
45      <C, I> C collect(Supplier<I> identity, PerceptionAccumulator<? super U, I> accumulator,
46              PerceptionFinalizer<C, I> finalizer);
47  
48      /**
49       * Combination of an accumulator and a finalizer.
50       * <p>
51       * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
52       * <br>
53       * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
54       * <p>
55       * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018 <br>
56       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
57       * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
58       * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
59       * @param <C> collection result type
60       * @param <U> underlying object type
61       * @param <I> intermediate result type
62       */
63      public interface PerceptionCollector<C, U, I>
64      {
65          /**
66           * Returns the identity value, the initial intermediate value.
67           * @return I; identity value, the initial intermediate value
68           */
69          Supplier<I> getIdentity();
70  
71          /**
72           * Returns the accumulator.
73           * @return PerceptionAccumulator; accumulator
74           */
75          PerceptionAccumulator<U, I> getAccumulator();
76  
77          /**
78           * Returns the finalizer.
79           * @return PerceptionFinalizer; finalizer
80           */
81          PerceptionFinalizer<C, I> getFinalizer();
82      }
83  
84      /**
85       * Accumulates an object one at a time in to an accumulating intermediate result.
86       * <p>
87       * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
88       * <br>
89       * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
90       * <p>
91       * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018 <br>
92       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
93       * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
94       * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
95       * @param <U> underlying object type
96       * @param <I> intermediate result type
97       */
98      public interface PerceptionAccumulator<U, I>
99      {
100         /**
101          * Accumulate the next object to intermediate result.
102          * @param intermediate Intermediate&lt;I&gt;; intermediate result before accumulation of object
103          * @param object U; next object to include
104          * @param distance Length; distance to the considered object
105          * @return I; intermediate result after accumulation of object
106          */
107         Intermediate<I> accumulate(Intermediate<I> intermediate, U object, Length distance);
108     }
109 
110     /**
111      * Translates the last intermediate result of an accumulator in to the collection output.
112      * <p>
113      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
114      * <br>
115      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
116      * <p>
117      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018 <br>
118      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
119      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
120      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
121      * @param <C> collection result type
122      * @param <I> intermediate result type
123      */
124     public interface PerceptionFinalizer<C, I>
125     {
126         /**
127          * Translate the last intermediate result in to a final result.
128          * @param intermediate I; last intermediate result
129          * @return C; final result
130          */
131         C collect(I intermediate);
132     }
133 
134     /**
135      * Wrapper of intermediate result with info for the iterator algorithm.
136      * <p>
137      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
138      * <br>
139      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
140      * <p>
141      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 apr. 2018 <br>
142      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
143      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
144      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>]
145      * @param <I> intermediate result type
146      */
147     class Intermediate<I>
148     {
149         /** Number of underlying object being iterated. */
150         private int number = 0;
151 
152         /** Intermediate object. */
153         private I object;
154 
155         /** Whether to stop accumulating. */
156         private boolean stop = false;
157 
158         /**
159          * Constructor.
160          * @param object I; identity value
161          */
162         public Intermediate(final I object)
163         {
164             this.object = object;
165         }
166 
167         /**
168          * Get intermediate object.
169          * @return I; intermediate object
170          */
171         public I getObject()
172         {
173             return this.object;
174         }
175 
176         /**
177          * Set intermediate object.
178          * @param object I; intermediate object
179          */
180         public void setObject(final I object)
181         {
182             this.object = object;
183         }
184 
185         /**
186          * Returns the number of the underlying object currently being accumulated, starts at 0 for the first.
187          * @return int; number of the underlying object currently being accumulated
188          */
189         public int getNumber()
190         {
191             return this.number;
192         }
193 
194         /**
195          * Method for the iterator to increase the underlying object number.
196          */
197         public void step()
198         {
199             this.number++;
200         }
201 
202         /**
203          * Method for the accumulator to indicate the iterator can stop.
204          */
205         public void stop()
206         {
207             this.stop = true;
208         }
209 
210         /**
211          * Method for the iterator to check if it can stop.
212          * @return boolean; whether the iterator can stop
213          */
214         public boolean isStop()
215         {
216             return this.stop;
217         }
218     }
219 
220 }