All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable Maven / Gradle / Ivy

package org.opentrafficsim.road.gtu.lane.perception;

import java.util.Iterator;
import java.util.function.Supplier;

import org.djunits.value.vdouble.scalar.Length;
import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;

/**
 * Iterable that additionally provides support for PerceptionCollectors. These gather raw data, to only 'perceive' the result.
 * 

* 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 * @author Peter Knoppers * @author Wouter Schakel * @param headway type * @param underlying object type */ public interface PerceptionCollectable extends PerceptionIterable { /** * Collect the underlying objects in to a perceived result. This methodology is loosely based on Stream.collect(). * @param collector PerceptionCollector<C, ? super U, I>; collector * @param collection result type * @param intermediate type * @return C; collection result */ default C collect(final PerceptionCollector collector) { return collect(collector.getIdentity(), collector.getAccumulator(), collector.getFinalizer()); } /** * Collect the underlying objects in to a perceived result. This methodology is loosely based on Stream.collect(). * @param identity Supplier<I>; the initial intermediate result value * @param accumulator PerceptionAccumulator<? super U, I>; accumulator * @param finalizer PerceptionFinalizer<C, I>; finalizer * @param collection result type * @param intermediate type * @return C; collection result */ C collect(Supplier identity, PerceptionAccumulator accumulator, PerceptionFinalizer finalizer); /** * Returns an iterator over the underlying objects. * @return Iterator<U>; iterator */ Iterator underlying(); /** * Returns an iterator over the underlying objects coupled with the distance. * @return Iterator<UnderlyingDistance<U>>; iterator */ Iterator> underlyingWithDistance(); /** * Combination of an accumulator and a finalizer. *

* 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 * @author Peter Knoppers * @author Wouter Schakel * @param collection result type * @param underlying object type * @param intermediate result type */ public interface PerceptionCollector { /** * Returns the identity value, the initial intermediate value. * @return I; identity value, the initial intermediate value */ Supplier getIdentity(); /** * Returns the accumulator. * @return PerceptionAccumulator; accumulator */ PerceptionAccumulator getAccumulator(); /** * Returns the finalizer. * @return PerceptionFinalizer; finalizer */ PerceptionFinalizer getFinalizer(); } /** * Accumulates an object one at a time in to an accumulating intermediate result. *

* 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 * @author Peter Knoppers * @author Wouter Schakel * @param underlying object type * @param intermediate result type */ public interface PerceptionAccumulator { /** * Accumulate the next object to intermediate result. * @param intermediate Intermediate<I>; intermediate result before accumulation of object * @param object U; next object to include * @param distance Length; distance to the considered object * @return I; intermediate result after accumulation of object */ Intermediate accumulate(Intermediate intermediate, U object, Length distance); } /** * Translates the last intermediate result of an accumulator in to the collection output. *

* 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 * @author Peter Knoppers * @author Wouter Schakel * @param collection result type * @param intermediate result type */ public interface PerceptionFinalizer { /** * Translate the last intermediate result in to a final result. * @param intermediate I; last intermediate result * @return C; final result */ C collect(I intermediate); } /** * Wrapper of intermediate result with info for the iterator algorithm. *

* 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 * @author Peter Knoppers * @author Wouter Schakel] * @param intermediate result type */ class Intermediate { /** Number of underlying object being iterated. */ private int number = 0; /** Intermediate object. */ private I object; /** Whether to stop accumulating. */ private boolean stop = false; /** * Constructor. * @param object I; identity value */ public Intermediate(final I object) { this.object = object; } /** * Get intermediate object. * @return I; intermediate object */ public I getObject() { return this.object; } /** * Set intermediate object. * @param object I; intermediate object */ public void setObject(final I object) { this.object = object; } /** * Returns the number of the underlying object currently being accumulated, starts at 0 for the first. * @return int; number of the underlying object currently being accumulated */ public int getNumber() { return this.number; } /** * Method for the iterator to increase the underlying object number. */ public void step() { this.number++; } /** * Method for the accumulator to indicate the iterator can stop. */ public void stop() { this.stop = true; } /** * Method for the iterator to check if it can stop. * @return boolean; whether the iterator can stop */ public boolean isStop() { return this.stop; } } /** * Wrapper for object and its distance. *

* 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 * @author Peter Knoppers * @author Wouter Schakel * @param underlying object type */ class UnderlyingDistance { /** Object. */ final U object; /** Distance. */ final Length distance; /** * @param object U; object * @param distance Length; distance */ public UnderlyingDistance(final U object, final Length distance) { this.object = object; this.distance = distance; } /** * @return U; object. */ public U getObject() { return this.object; } /** * @return Length; distance. */ public Length getDistance() { return this.distance; } } }