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-2018 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; 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; the initial intermediate result value
39 * @param accumulator PerceptionAccumulator; accumulator
40 * @param finalizer PerceptionFinalizer; 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-2018 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-2018 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 I; 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-2018 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-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
138 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
139 * <p>
140 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 apr. 2018 <br>
141 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
142 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
143 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>]
144 * @param <I> intermediate result type
145 */
146 class Intermediate<I>
147 {
148 /** Number of underlying object being iterated. */
149 private int number = 0;
150
151 /** Intermediate object. */
152 private I object;
153
154 /** Whether to stop accumulating. */
155 private boolean stop = false;
156
157 /**
158 * Constructor.
159 * @param object I; identity value
160 */
161 public Intermediate(final I object)
162 {
163 this.object = object;
164 }
165
166 /**
167 * Get intermediate object.
168 * @return I; intermediate object
169 */
170 public I getObject()
171 {
172 return this.object;
173 }
174
175 /**
176 * Set intermediate object.
177 * @param object I; intermediate object
178 */
179 public void setObject(final I object)
180 {
181 this.object = object;
182 }
183
184 /**
185 * Returns the number of the underlying object currently being accumulated, starts at 0 for the first.
186 * @return int; number of the underlying object currently being accumulated
187 */
188 public int getNumber()
189 {
190 return this.number;
191 }
192
193 /**
194 * Method for the iterator the increase the underlying object number.
195 */
196 public void step()
197 {
198 this.number++;
199 }
200
201 /**
202 * Method for the accumulator to indicate the iterator can stop.
203 */
204 public void stop()
205 {
206 this.stop = true;
207 }
208
209 /**
210 * Method for the iterator to check if it can stop.
211 * @return boolean; whether the iterator can stop
212 */
213 public boolean isStop()
214 {
215 return this.stop;
216 }
217 }
218
219 }