1 package org.opentrafficsim.road.gtu.lane.perception;
2
3 import java.util.Iterator;
4 import java.util.function.BiFunction;
5
6 import org.djunits.value.vdouble.scalar.Length;
7 import org.djutils.exceptions.Throw;
8 import org.opentrafficsim.base.parameters.ParameterException;
9 import org.opentrafficsim.core.gtu.GtuException;
10 import org.opentrafficsim.road.gtu.lane.perception.object.PerceivedObject;
11 import org.opentrafficsim.road.gtu.lane.perception.structure.LaneStructure;
12 import org.opentrafficsim.road.gtu.lane.perception.structure.NavigatingIterable.Entry;
13 import org.opentrafficsim.road.network.lane.object.LaneBasedObject;
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class PerceptionReiterable<O extends LaneBasedObject, P extends PerceivedObject, U>
28 extends AbstractPerceptionReiterable<O, P, U>
29 {
30
31
32 private final Iterator<Entry<U>> iterator;
33
34
35 private final BiFunction<U, Length, P> perception;
36
37
38
39
40
41
42
43 public PerceptionReiterable(final O perceivingObject, final Iterable<Entry<U>> iterable,
44 final BiFunction<U, Length, P> perception)
45 {
46 super(perceivingObject);
47 Throw.whenNull(iterable, "iterator");
48 Throw.whenNull(perception, "perception");
49 this.iterator = iterable.iterator();
50 this.perception = perception;
51 }
52
53 @Override
54 protected Iterator<UnderlyingDistance<U>> primaryIterator()
55 {
56 return new Iterator<>()
57 {
58 @Override
59 public boolean hasNext()
60 {
61 return PerceptionReiterable.this.iterator.hasNext();
62 }
63
64 @Override
65 public UnderlyingDistance<U> next()
66 {
67 Entry<U> entry = PerceptionReiterable.this.iterator.next();
68 return new UnderlyingDistance<>(entry.object(), entry.distance());
69 }
70 };
71 }
72
73 @Override
74 protected P perceive(final U object, final Length distance) throws GtuException, ParameterException
75 {
76 return this.perception.apply(object, distance);
77 }
78
79 }