Class AbstractPerceptionReiterable<H extends Headway,U>

java.lang.Object
org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable<H,U>
Type Parameters:
H - headway type
U - underlying object type
All Implemented Interfaces:
Iterable<H>, PerceptionCollectable<H,U>, PerceptionIterable<H>
Direct Known Subclasses:
AbstractPerceptionIterable, MultiLanePerceptionIterable

public abstract class AbstractPerceptionReiterable<H extends Headway,U> extends Object implements PerceptionCollectable<H,U>
This class uses a single primary iterator which a subclass defines, and makes sure that all elements are only looked up and created once. It does so by storing the elements in a linked list. All calls to iterator() return an iterator which iterates over the linked list. If an iterator runs to the end of the linked list, the primary iterator is requested to add an element if it has one.

Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Author:
Alexander Verbraeck, Peter Knoppers, Wouter Schakel
  • Constructor Details

    • AbstractPerceptionReiterable

      protected AbstractPerceptionReiterable(LaneBasedGtu perceivingGtu)
      Constructor.
      Parameters:
      perceivingGtu - LaneBasedGtu; perceiving GTU
  • Method Details

    • getGtu

      protected LaneBasedGtu getGtu()
      Returns the GTU.
      Returns:
      LaneBasedGtu; GTU
    • getPrimaryIterator

      Returns the primary iterator.
      Returns:
      Iterator; primary iterator
    • primaryIterator

      protected abstract Iterator<AbstractPerceptionReiterable<H,U>.PrimaryIteratorEntry> primaryIterator()
      Returns the primary iterator. This method is called once by AbstractPerceptionReiterable.
      Returns:
      Iterator; primary iterator
    • perceive

      protected abstract H perceive(LaneBasedGtu perceivingGtu, U object, org.djunits.value.vdouble.scalar.Length distance) throws GtuException, org.opentrafficsim.base.parameters.ParameterException
      Returns a perceived version of the underlying object.
      Parameters:
      perceivingGtu - LaneBasedGtu; perceiving GTU
      object - U; underlying object
      distance - Length; distance to the object
      Returns:
      H; perceived version of the underlying object
      Throws:
      GtuException - on exception
      org.opentrafficsim.base.parameters.ParameterException - on invalid parameter value or missing parameter
    • first

      public final H first()
      Returns the first element.
      Specified by:
      first in interface PerceptionIterable<H extends Headway>
      Returns:
      H; first element
    • addNext

      Adds an iterator entry to the internal linked list.
      Parameters:
      next - PrimaryIteratorEntry; next object
    • isEmpty

      public final boolean isEmpty()
      Returns whether this iterable is empty.
      Specified by:
      isEmpty in interface PerceptionIterable<H extends Headway>
      Returns:
      whether this iterable is empty
    • iterator

      public final Iterator<H> iterator()
      Specified by:
      iterator in interface Iterable<H extends Headway>
    • collect

      public final <C, I> C collect(Supplier<I> identity, PerceptionCollectable.PerceptionAccumulator<? super U,I> accumulator, PerceptionCollectable.PerceptionFinalizer<C,I> finalizer)
      Collect the underlying objects in to a perceived result. This methodology is loosely based on Stream.collect().
      Specified by:
      collect in interface PerceptionCollectable<H extends Headway,U>
      Type Parameters:
      C - collection result type
      I - intermediate type
      Parameters:
      identity - Supplier<I>; the initial intermediate result value
      accumulator - PerceptionAccumulator<? super U, I>; accumulator
      finalizer - PerceptionFinalizer<C, I>; finalizer
      Returns:
      C; collection result
    • underlying

      public Iterator<U> underlying()
      Returns an iterator over the underlying objects.
      Specified by:
      underlying in interface PerceptionCollectable<H extends Headway,U>
      Returns:
      Iterator<U>; iterator
    • underlyingWithDistance

      public Iterator<PerceptionCollectable.UnderlyingDistance<U>> underlyingWithDistance()
      Returns an iterator over the underlying objects coupled with the distance.
      Specified by:
      underlyingWithDistance in interface PerceptionCollectable<H extends Headway,U>
      Returns:
      Iterator<UnderlyingDistance<U>>; iterator
    • assureNext

      AbstractPerceptionReiterable<H,U>.org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable.SecondaryIteratorEntry assureNext(AbstractPerceptionReiterable<H,U>.org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable.SecondaryIteratorEntry next, AbstractPerceptionReiterable<H,U>.org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable.SecondaryIteratorEntry lastReturned)
      Helper method that assures that a next entry is available, if the primary iterator has a next value. This method may be used by any process that derives from the primary iterator.
      Parameters:
      next - SecondaryIteratorEntry; currently known next entry
      lastReturned - SecondaryIteratorEntry; entry of last returned object or value
      Returns:
      IteratorEntry; next entry