TaskHeadwayCollector.java

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

  2. import java.util.function.Supplier;

  3. import org.djunits.value.vdouble.scalar.Duration;
  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.djunits.value.vdouble.scalar.Speed;
  6. import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
  7. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.Intermediate;
  8. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionAccumulator;
  9. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionCollector;
  10. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionFinalizer;

  11. /**
  12.  * Simple collector implementation to obtain time headway.
  13.  * <p>
  14.  * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  16.  * </p>
  17.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  18.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  19.  * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
  20.  */
  21. public class TaskHeadwayCollector implements PerceptionCollector<Duration, LaneBasedGtu, Duration>
  22. {

  23.     /** Own speed. */
  24.     private final Speed speed;

  25.     /**
  26.      * Constructor.
  27.      * @param speed Speed; speed
  28.      */
  29.     public TaskHeadwayCollector(final Speed speed)
  30.     {
  31.         this.speed = speed;
  32.     }

  33.     /** {@inheritDoc} */
  34.     @Override
  35.     public Supplier<Duration> getIdentity()
  36.     {
  37.         return new Supplier<Duration>()
  38.         {
  39.             @Override
  40.             public Duration get()
  41.             {
  42.                 return null; // if no leader
  43.             }
  44.         };
  45.     }

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public PerceptionAccumulator<LaneBasedGtu, Duration> getAccumulator()
  49.     {
  50.         return new PerceptionAccumulator<LaneBasedGtu, Duration>()
  51.         {
  52.             @SuppressWarnings("synthetic-access")
  53.             @Override
  54.             public Intermediate<Duration> accumulate(final Intermediate<Duration> intermediate, final LaneBasedGtu object,
  55.                     final Length distance)
  56.             {
  57.                 intermediate.setObject(distance.divide(TaskHeadwayCollector.this.speed));
  58.                 intermediate.stop(); // need only 1 leader
  59.                 return intermediate;
  60.             }
  61.         };
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public PerceptionFinalizer<Duration, Duration> getFinalizer()
  66.     {
  67.         return new PerceptionFinalizer<Duration, Duration>()
  68.         {
  69.             @Override
  70.             public Duration collect(final Duration intermediate)
  71.             {
  72.                 return intermediate == null ? intermediate : (intermediate.gt0() ? intermediate : Duration.ZERO);
  73.             }
  74.         };
  75.     }

  76. }