View Javadoc
1   package org.opentrafficsim.road.gtu.lane;
2   
3   import java.util.function.Supplier;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djutils.exceptions.Throw;
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  /**
13   * Collision detector that detects a negative headway when used on a {@code PerceptionCollectable<HeadwayGTU, LaneBasedGTU>}.
14   * This is done by {@code AbstractLaneBasedGTU.getCarFollowingAcceleration()}.
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
18   * <p>
19   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 feb. 2019 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   */
24  public class CollisionDetector implements PerceptionCollector<Void, LaneBasedGTU, Void>
25  {
26  
27      /** GTU id. */
28      private final String id;
29  
30      /**
31       * Constructor.
32       * @param id String; GTU id
33       */
34      public CollisionDetector(final String id)
35      {
36          this.id = id;
37      }
38  
39      /** {@inheritDoc} */
40      @Override
41      public Supplier<Void> getIdentity()
42      {
43          return new Supplier<Void>()
44          {
45              /** {@inheritDoc} */
46              @Override
47              public Void get()
48              {
49                  return null;
50              }
51          };
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public PerceptionAccumulator<LaneBasedGTU, Void> getAccumulator()
57      {
58          return new PerceptionAccumulator<LaneBasedGTU, Void>()
59          {
60              /** {@inheritDoc} */
61              @SuppressWarnings("synthetic-access")
62              @Override
63              public Intermediate<Void> accumulate(final Intermediate<Void> intermediate, final LaneBasedGTU object,
64                      final Length distance)
65              {
66                  Throw.when(distance.lt0(), CollisionException.class, "GTU %s collided with GTU %s", CollisionDetector.this.id,
67                          object.getId());
68                  intermediate.stop();
69                  return intermediate;
70              }
71          };
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      public PerceptionFinalizer<Void, Void> getFinalizer()
77      {
78          return new PerceptionFinalizer<Void, Void>()
79          {
80              @Override
81              public Void collect(final Void intermediate)
82              {
83                  return null;
84              }
85          };
86      }
87  
88  }