1 package org.opentrafficsim.road.gtu.lane.perception;
2
3 import java.util.Iterator;
4 import java.util.NoSuchElementException;
5 import java.util.function.Supplier;
6
7 import org.djunits.value.vdouble.scalar.Length;
8 import org.djutils.exceptions.Try;
9 import org.opentrafficsim.base.parameters.ParameterException;
10 import org.opentrafficsim.core.gtu.GTUException;
11 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
12 import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public abstract class AbstractPerceptionReiterable<H extends Headway, U> implements PerceptionCollectable<H, U>
31 {
32
33
34 private SecondaryIteratorEntry first;
35
36
37 private SecondaryIteratorEntry last;
38
39
40 private Iterator<PrimaryIteratorEntry> primaryIterator;
41
42
43 private final LaneBasedGTU gtu;
44
45
46
47
48
49 protected AbstractPerceptionReiterable(final LaneBasedGTU perceivingGtu)
50 {
51 this.gtu = perceivingGtu;
52 }
53
54
55
56
57
58 protected LaneBasedGTU getGtu()
59 {
60 return this.gtu;
61 }
62
63
64
65
66
67 final Iterator<PrimaryIteratorEntry> getPrimaryIterator()
68 {
69 if (this.primaryIterator == null)
70 {
71 this.primaryIterator = primaryIterator();
72 }
73 return this.primaryIterator;
74 }
75
76
77
78
79
80 protected abstract Iterator<PrimaryIteratorEntry> primaryIterator();
81
82
83
84
85
86
87
88
89
90
91 protected abstract H perceive(LaneBasedGTU perceivingGtu, U object, Length distance)
92 throws GTUException, ParameterException;
93
94
95 @Override
96 public final synchronized H first()
97 {
98 assureFirst();
99 if (this.first == null)
100 {
101 return null;
102 }
103 return this.first.getValue();
104 }
105
106
107
108
109 private synchronized void assureFirst()
110 {
111 if (this.first == null && getPrimaryIterator().hasNext())
112 {
113 addNext(getPrimaryIterator().next());
114 }
115 }
116
117
118
119
120
121 @SuppressWarnings("synthetic-access")
122 final void addNext(final PrimaryIteratorEntry next)
123 {
124 SecondaryIteratorEntry entry = new SecondaryIteratorEntry(next.object, next.distance);
125 if (AbstractPerceptionReiterable.this.last == null)
126 {
127 AbstractPerceptionReiterable.this.first = entry;
128 AbstractPerceptionReiterable.this.last = entry;
129 }
130 else
131 {
132 AbstractPerceptionReiterable.this.last.next = entry;
133 AbstractPerceptionReiterable.this.last = entry;
134 }
135 }
136
137
138 @Override
139 public final boolean isEmpty()
140 {
141 return first() == null;
142 }
143
144
145 @Override
146 public final Iterator<H> iterator()
147 {
148 return new PerceptionIterator();
149 }
150
151
152 @SuppressWarnings("synthetic-access")
153 @Override
154 public final <C, I> C collect(final Supplier<I> identity, final PerceptionAccumulator<? super U, I> accumulator,
155 final PerceptionFinalizer<C, I> finalizer)
156 {
157 Intermediate<I> intermediate = new Intermediate<>(identity.get());
158 assureFirst();
159 if (this.first != null)
160 {
161 SecondaryIteratorEntry lastReturned = null;
162 SecondaryIteratorEntry next = this.first;
163 next = assureNext(next, lastReturned);
164 while (next != null && !intermediate.isStop())
165 {
166 intermediate = accumulator.accumulate(intermediate, next.object, next.distance);
167 intermediate.step();
168 lastReturned = next;
169 next = lastReturned.next;
170 next = assureNext(next, lastReturned);
171 }
172 }
173 return finalizer.collect(intermediate.getObject());
174 }
175
176
177 @Override
178 public Iterator<U> underlying()
179 {
180 assureFirst();
181 SecondaryIteratorEntry firstInContext = this.first;
182 return new Iterator<U>()
183 {
184
185 private SecondaryIteratorEntry lastReturned = null;
186
187
188 private SecondaryIteratorEntry next = firstInContext;
189
190
191 @Override
192 public boolean hasNext()
193 {
194 this.next = assureNext(this.next, this.lastReturned);
195 return this.next != null;
196 }
197
198
199 @SuppressWarnings("synthetic-access")
200 @Override
201 public U next()
202 {
203 this.lastReturned = this.next;
204 this.next = this.lastReturned.next;
205 this.next = assureNext(this.next, this.lastReturned);
206 return this.lastReturned.object;
207 }
208 };
209 }
210
211
212 @Override
213 public Iterator<UnderlyingDistance<U>> underlyingWithDistance()
214 {
215 assureFirst();
216 SecondaryIteratorEntry firstInContext = this.first;
217 return new Iterator<UnderlyingDistance<U>>()
218 {
219
220 private SecondaryIteratorEntry lastReturned = null;
221
222
223 private SecondaryIteratorEntry next = firstInContext;
224
225
226 @Override
227 public boolean hasNext()
228 {
229 this.next = assureNext(this.next, this.lastReturned);
230 return this.next != null;
231 }
232
233
234 @SuppressWarnings("synthetic-access")
235 @Override
236 public UnderlyingDistance<U> next()
237 {
238 this.lastReturned = this.next;
239 this.next = this.lastReturned.next;
240 this.next = assureNext(this.next, this.lastReturned);
241 return new UnderlyingDistance<>(this.lastReturned.object, this.lastReturned.distance);
242 }
243 };
244 }
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260 public class PerceptionIterator implements Iterator<H>
261 {
262
263
264 private SecondaryIteratorEntry lastReturned;
265
266
267 private SecondaryIteratorEntry next;
268
269
270 @SuppressWarnings("synthetic-access")
271 PerceptionIterator()
272 {
273 this.next = AbstractPerceptionReiterable.this.first;
274 }
275
276
277 @Override
278 public boolean hasNext()
279 {
280 this.next = assureNext(this.next, this.lastReturned);
281 return this.next != null;
282 }
283
284
285 @SuppressWarnings("synthetic-access")
286 @Override
287 public H next()
288 {
289 this.next = assureNext(this.next, this.lastReturned);
290 if (this.next == null)
291 {
292 throw new NoSuchElementException();
293 }
294 this.lastReturned = this.next;
295 this.next = this.lastReturned.next;
296 return this.lastReturned.getValue();
297 }
298
299 }
300
301
302
303
304
305
306
307
308 @SuppressWarnings("synthetic-access")
309 synchronized SecondaryIteratorEntry assureNext(final SecondaryIteratorEntry next, final SecondaryIteratorEntry lastReturned)
310 {
311 if (next == null && getPrimaryIterator().hasNext())
312 {
313 addNext(getPrimaryIterator().next());
314 if (lastReturned == null)
315 {
316 return AbstractPerceptionReiterable.this.first;
317 }
318 else
319 {
320 return lastReturned.next;
321 }
322 }
323 return next;
324 }
325
326
327
328
329
330
331
332
333
334
335
336
337
338 protected class PrimaryIteratorEntry implements Comparable<PrimaryIteratorEntry>
339 {
340
341 private final U object;
342
343
344 private final Length distance;
345
346
347
348
349
350
351 public PrimaryIteratorEntry(final U object, final Length distance)
352 {
353 this.object = object;
354 this.distance = distance;
355 }
356
357
358 @Override
359 public int compareTo(final PrimaryIteratorEntry o)
360 {
361 return this.distance.compareTo(o.distance);
362 }
363
364
365
366
367
368 protected U getObject()
369 {
370 return this.object;
371 }
372 }
373
374
375
376
377
378
379
380
381
382
383
384
385
386 private class SecondaryIteratorEntry
387 {
388
389 private final U object;
390
391
392 private final Length distance;
393
394
395 private H value;
396
397
398 private SecondaryIteratorEntry next;
399
400
401
402
403
404
405 SecondaryIteratorEntry(final U object, final Length distance)
406 {
407 this.object = object;
408 this.distance = distance;
409 }
410
411
412
413
414
415 H getValue()
416 {
417 if (this.value == null)
418 {
419 this.value = Try.assign(() -> perceive(AbstractPerceptionReiterable.this.getGtu(), this.object, this.distance),
420 "Exception during perception of object.");
421 }
422 return this.value;
423 }
424 }
425
426 }