View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.categories.neighbors;
2   
3   import java.util.function.Supplier;
4   
5   import org.djunits.value.vdouble.scalar.Duration;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
9   import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.Intermediate;
10  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionAccumulator;
11  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionCollector;
12  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionFinalizer;
13  
14  /**
15   * Simple collector implementation to obtain time headway.
16   * <p>
17   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 apr. 2018 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public class TaskHeadwayCollector implements PerceptionCollector<Duration, LaneBasedGTU, Duration>
26  {
27  
28      /** Own speed. */
29      private final Speed speed;
30  
31      /**
32       * Constructor.
33       * @param speed Speed; speed
34       */
35      public TaskHeadwayCollector(final Speed speed)
36      {
37          this.speed = speed;
38      }
39  
40      /** {@inheritDoc} */
41      @Override
42      public Supplier<Duration> getIdentity()
43      {
44          return new Supplier<Duration>()
45          {
46              @Override
47              public Duration get()
48              {
49                  return null; // if no leader
50              }
51          };
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public PerceptionAccumulator<LaneBasedGTU, Duration> getAccumulator()
57      {
58          return new PerceptionAccumulator<LaneBasedGTU, Duration>()
59          {
60              @SuppressWarnings("synthetic-access")
61              @Override
62              public Intermediate<Duration> accumulate(final Intermediate<Duration> intermediate, final LaneBasedGTU object,
63                      final Length distance)
64              {
65                  intermediate.setObject(distance.divideBy(TaskHeadwayCollector.this.speed));
66                  intermediate.stop(); // need only 1 leader
67                  return intermediate;
68              }
69          };
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public PerceptionFinalizer<Duration, Duration> getFinalizer()
75      {
76          return new PerceptionFinalizer<Duration, Duration>()
77          {
78              @Override
79              public Duration collect(final Duration intermediate)
80              {
81                  return intermediate == null ? intermediate : (intermediate.gt0() ? intermediate : Duration.ZERO);
82              }
83          };
84      }
85  
86  }