View Javadoc
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   * Standard implementation of {@link AbstractPerceptionReiterable} useful for most cases using an iterator over {@link Entry}
17   * from the {@link LaneStructure}.
18   * <p>
19   * Copyright (c) 2026-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * </p>
22   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
23   * @param <O> perceiving object type (an {@code O} is perceiving a {@code U} as a {@code P})
24   * @param <P> perceived object type (an {@code O} is perceiving a {@code U} as a {@code P})
25   * @param <U> underlying object type (an {@code O} is perceiving a {@code U} as a {@code P})
26   */
27  public class PerceptionReiterable<O extends LaneBasedObject, P extends PerceivedObject, U>
28          extends AbstractPerceptionReiterable<O, P, U>
29  {
30  
31      /** Iterator over entries of underlying objects. */
32      private final Iterator<Entry<U>> iterator;
33  
34      /** Function to translate object and the distance to it in to a perceived object. */
35      private final BiFunction<U, Length, P> perception;
36  
37      /**
38       * Constructor.
39       * @param perceivingObject perceiving object
40       * @param iterable iterable over entries of underlying objects
41       * @param perception function to translate an object, and the distance to it, in to a perceived object
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  }