PerceptionCollectable.java

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

  2. import java.util.function.Supplier;

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

  5. /**
  6.  * Iterable that additionally provides support for PerceptionCollectors. These gather raw data, to only 'perceive' the result.
  7.  * <p>
  8.  * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  9.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  10.  * <p>
  11.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 mrt. 2018 <br>
  12.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  13.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  14.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  15.  * @param <H> headway type
  16.  * @param <U> underlying object type
  17.  */
  18. public interface PerceptionCollectable<H extends Headway, U> extends PerceptionIterable<H>
  19. {

  20.     /**
  21.      * Collect the underlying objects in to a perceived result. This methodology is loosely based on Stream.collect().
  22.      * @param collector PerceptionCollector&lt;C, ? super U, I&gt;; collector
  23.      * @param <C> collection result type
  24.      * @param <I> intermediate type
  25.      * @return C; collection result
  26.      */
  27.     default <C, I> C collect(final PerceptionCollector<C, ? super U, I> collector)
  28.     {
  29.         return collect(collector.getIdentity(), collector.getAccumulator(), collector.getFinalizer());
  30.     }

  31.     /**
  32.      * Collect the underlying objects in to a perceived result. This methodology is loosely based on Stream.collect().
  33.      * @param identity Supplier&lt;I&gt;; the initial intermediate result value
  34.      * @param accumulator PerceptionAccumulator&lt;? super U, I&gt;; accumulator
  35.      * @param finalizer PerceptionFinalizer&lt;C, I&gt;; finalizer
  36.      * @param <C> collection result type
  37.      * @param <I> intermediate type
  38.      * @return C; collection result
  39.      */
  40.     <C, I> C collect(Supplier<I> identity, PerceptionAccumulator<? super U, I> accumulator,
  41.             PerceptionFinalizer<C, I> finalizer);

  42.     /**
  43.      * Combination of an accumulator and a finalizer.
  44.      * <p>
  45.      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  46.      * <br>
  47.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  48.      * <p>
  49.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018 <br>
  50.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  51.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  52.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  53.      * @param <C> collection result type
  54.      * @param <U> underlying object type
  55.      * @param <I> intermediate result type
  56.      */
  57.     public interface PerceptionCollector<C, U, I>
  58.     {
  59.         /**
  60.          * Returns the identity value, the initial intermediate value.
  61.          * @return I; identity value, the initial intermediate value
  62.          */
  63.         Supplier<I> getIdentity();

  64.         /**
  65.          * Returns the accumulator.
  66.          * @return PerceptionAccumulator; accumulator
  67.          */
  68.         PerceptionAccumulator<U, I> getAccumulator();

  69.         /**
  70.          * Returns the finalizer.
  71.          * @return PerceptionFinalizer; finalizer
  72.          */
  73.         PerceptionFinalizer<C, I> getFinalizer();
  74.     }

  75.     /**
  76.      * Accumulates an object one at a time in to an accumulating intermediate result.
  77.      * <p>
  78.      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  79.      * <br>
  80.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  81.      * <p>
  82.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018 <br>
  83.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  84.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  85.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  86.      * @param <U> underlying object type
  87.      * @param <I> intermediate result type
  88.      */
  89.     public interface PerceptionAccumulator<U, I>
  90.     {
  91.         /**
  92.          * Accumulate the next object to intermediate result.
  93.          * @param intermediate Intermediate&lt;I&gt;; intermediate result before accumulation of object
  94.          * @param object U; next object to include
  95.          * @param distance Length; distance to the considered object
  96.          * @return I; intermediate result after accumulation of object
  97.          */
  98.         Intermediate<I> accumulate(Intermediate<I> intermediate, U object, Length distance);
  99.     }

  100.     /**
  101.      * Translates the last intermediate result of an accumulator in to the collection output.
  102.      * <p>
  103.      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  104.      * <br>
  105.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  106.      * <p>
  107.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018 <br>
  108.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  109.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  110.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  111.      * @param <C> collection result type
  112.      * @param <I> intermediate result type
  113.      */
  114.     public interface PerceptionFinalizer<C, I>
  115.     {
  116.         /**
  117.          * Translate the last intermediate result in to a final result.
  118.          * @param intermediate I; last intermediate result
  119.          * @return C; final result
  120.          */
  121.         C collect(I intermediate);
  122.     }

  123.     /**
  124.      * Wrapper of intermediate result with info for the iterator algorithm.
  125.      * <p>
  126.      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  127.      * <br>
  128.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  129.      * <p>
  130.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 apr. 2018 <br>
  131.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  132.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  133.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>]
  134.      * @param <I> intermediate result type
  135.      */
  136.     class Intermediate<I>
  137.     {
  138.         /** Number of underlying object being iterated. */
  139.         private int number = 0;

  140.         /** Intermediate object. */
  141.         private I object;

  142.         /** Whether to stop accumulating. */
  143.         private boolean stop = false;

  144.         /**
  145.          * Constructor.
  146.          * @param object I; identity value
  147.          */
  148.         public Intermediate(final I object)
  149.         {
  150.             this.object = object;
  151.         }

  152.         /**
  153.          * Get intermediate object.
  154.          * @return I; intermediate object
  155.          */
  156.         public I getObject()
  157.         {
  158.             return this.object;
  159.         }

  160.         /**
  161.          * Set intermediate object.
  162.          * @param object I; intermediate object
  163.          */
  164.         public void setObject(final I object)
  165.         {
  166.             this.object = object;
  167.         }

  168.         /**
  169.          * Returns the number of the underlying object currently being accumulated, starts at 0 for the first.
  170.          * @return int; number of the underlying object currently being accumulated
  171.          */
  172.         public int getNumber()
  173.         {
  174.             return this.number;
  175.         }

  176.         /**
  177.          * Method for the iterator to increase the underlying object number.
  178.          */
  179.         public void step()
  180.         {
  181.             this.number++;
  182.         }

  183.         /**
  184.          * Method for the accumulator to indicate the iterator can stop.
  185.          */
  186.         public void stop()
  187.         {
  188.             this.stop = true;
  189.         }

  190.         /**
  191.          * Method for the iterator to check if it can stop.
  192.          * @return boolean; whether the iterator can stop
  193.          */
  194.         public boolean isStop()
  195.         {
  196.             return this.stop;
  197.         }
  198.     }

  199. }