package org.opentrafficsim.road.gtu.lane.perception; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.function.Supplier; import org.djunits.value.vdouble.scalar.Length; import org.djutils.exceptions.Try; import org.opentrafficsim.base.parameters.ParameterException; import org.opentrafficsim.core.gtu.GtuException; import org.opentrafficsim.road.gtu.lane.InternalLaneBasedGtu; import org.opentrafficsim.road.gtu.lane.perception.headway.Headway; /** * 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 {@code 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-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* Copyright (c) 2013-2021 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 16 feb. 2018
* Copyright (c) 2013-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018
* Copyright (c) 2013-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version 16 feb. 2018
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class PerceptionIterator implements Iterator
* BSD-style license. See OpenTrafficSim License.
*
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
protected class PrimaryIteratorEntry implements Comparable
* BSD-style license. See OpenTrafficSim License.
*
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
private class SecondaryIteratorEntry
{
/** Value. */
private final U object;
/** Distance to object. */
private final Length distance;
/** Value. */
private H value;
/** Next entry. */
private SecondaryIteratorEntry next;
/**
* Constructor.
* @param object U; object
* @param distance Length; distance to object
*/
SecondaryIteratorEntry(final U object, final Length distance)
{
this.object = object;
this.distance = distance;
}
/**
* Returns the perceived version of the object.
* @return H; perceived version of the object
*/
H getValue()
{
if (this.value == null)
{
this.value = Try.assign(() -> perceive(AbstractPerceptionReiterable.this.getGtu(), this.object, this.distance),
"Exception during perception of object.");
}
return this.value;
}
}
}