Class AbstractPerceptionReiterable<H extends Headway,U>
- java.lang.Object
-
- org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable<H,U>
-
- Type Parameters:
H
- headway typeU
- 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 toiterator()
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-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.- Version:
- $Revision$, $LastChangedDate$, by $Author$, initial version 20 feb. 2018
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
AbstractPerceptionReiterable.PerceptionIterator
This iterator is returned to callers of theiterator()
method.protected class
AbstractPerceptionReiterable.PrimaryIteratorEntry
Class forprimaryIterator()
to return, implemented in subclasses.-
Nested classes/interfaces inherited from interface org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable
PerceptionCollectable.Intermediate<I>, PerceptionCollectable.PerceptionAccumulator<U,I>, PerceptionCollectable.PerceptionCollector<C,U,I>, PerceptionCollectable.PerceptionFinalizer<C,I>, PerceptionCollectable.UnderlyingDistance<U>
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractPerceptionReiterable(LaneBasedGTU perceivingGtu)
Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) void
addNext(AbstractPerceptionReiterable.PrimaryIteratorEntry next)
Adds an iterator entry to the internal linked list.(package private) org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable.SecondaryIteratorEntry
assureNext(org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable.SecondaryIteratorEntry next, 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.<C,I>
Ccollect(Supplier<I> identity, PerceptionCollectable.PerceptionAccumulator<? super U,I> accumulator, PerceptionCollectable.PerceptionFinalizer<C,I> finalizer)
Collect the underlying objects in to a perceived result.H
first()
Returns the first element.protected LaneBasedGTU
getGtu()
Returns the GTU.(package private) Iterator<AbstractPerceptionReiterable.PrimaryIteratorEntry>
getPrimaryIterator()
Returns the primary iterator.boolean
isEmpty()
Returns whether this iterable is empty.Iterator<H>
iterator()
protected abstract H
perceive(LaneBasedGTU perceivingGtu, U object, Length distance)
Returns a perceived version of the underlying object.protected abstract Iterator<AbstractPerceptionReiterable.PrimaryIteratorEntry>
primaryIterator()
Returns the primary iterator.Iterator<U>
underlying()
Returns an iterator over the underlying objects.Iterator<PerceptionCollectable.UnderlyingDistance<U>>
underlyingWithDistance()
Returns an iterator over the underlying objects coupled with the distance.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable
collect
-
-
-
-
Constructor Detail
-
AbstractPerceptionReiterable
protected AbstractPerceptionReiterable(LaneBasedGTU perceivingGtu)
Constructor.- Parameters:
perceivingGtu
- LaneBasedGTU; perceiving GTU
-
-
Method Detail
-
getGtu
protected LaneBasedGTU getGtu()
Returns the GTU.- Returns:
- LaneBasedGTU; GTU
-
getPrimaryIterator
final Iterator<AbstractPerceptionReiterable.PrimaryIteratorEntry> getPrimaryIterator()
Returns the primary iterator.- Returns:
- Iterator; primary iterator
-
primaryIterator
protected abstract Iterator<AbstractPerceptionReiterable.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, Length distance) throws GTUException, org.opentrafficsim.base.parameters.ParameterException
Returns a perceived version of the underlying object.- Parameters:
perceivingGtu
- LaneBasedGTU; perceiving GTUobject
- U; underlying objectdistance
- Length; distance to the object- Returns:
- H; perceived version of the underlying object
- Throws:
GTUException
- on exceptionorg.opentrafficsim.base.parameters.ParameterException
- on invalid parameter value or missing parameter
-
first
public final H first()
Returns the first element.- Specified by:
first
in interfacePerceptionIterable<H extends Headway>
- Returns:
- H; first element
-
addNext
final void addNext(AbstractPerceptionReiterable.PrimaryIteratorEntry next)
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 interfacePerceptionIterable<H extends Headway>
- Returns:
- whether this iterable is empty
-
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 interfacePerceptionCollectable<H extends Headway,U>
- Type Parameters:
C
- collection result typeI
- intermediate type- Parameters:
identity
- Supplier<I>; the initial intermediate result valueaccumulator
- PerceptionAccumulator<? super U, I>; accumulatorfinalizer
- PerceptionFinalizer<C, I>; finalizer- Returns:
- C; collection result
-
underlying
public Iterator<U> underlying()
Returns an iterator over the underlying objects.- Specified by:
underlying
in interfacePerceptionCollectable<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 interfacePerceptionCollectable<H extends Headway,U>
- Returns:
- Iterator<UnderlyingDistance<U>>; iterator
-
assureNext
org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable.SecondaryIteratorEntry assureNext(org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionReiterable.SecondaryIteratorEntry next, 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 entrylastReturned
- SecondaryIteratorEntry; entry of last returned object or value- Returns:
- IteratorEntry; next entry
-
-