View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import java.util.Iterator;
4   import java.util.TreeSet;
5   import java.util.function.Supplier;
6   
7   import org.opentrafficsim.core.network.OTSNetwork;
8   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
9   import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
10  
11  /**
12   * Simple class implementing a SortedSet. This is mainly for backwards compatibility. Methods that determine the elements 1-by-1
13   * are much preferred for efficiency.
14   * <p>
15   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
17   * <p>
18   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 feb. 2018 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   * @param <H> headway type
23   */
24  @Deprecated
25  public class SortedSetPerceptionIterable<H extends Headway> extends TreeSet<H> implements PerceptionCollectable<H, LaneBasedGTU>
26  {
27  
28      /** */
29      private static final long serialVersionUID = 20180219L;
30  
31      /** Network to obtain LaneBasedGTU. */
32      private final OTSNetwork network;
33  
34      /**
35       * Constructor.
36       * @param otsNetwork OTSNetwork; network to obtain LaneBasedGTU
37       */
38      public SortedSetPerceptionIterable(final OTSNetwork otsNetwork)
39      {
40          this.network = otsNetwork;
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      public <C, I> C collect(final Supplier<I> identity, final PerceptionAccumulator<? super LaneBasedGTU, I> accumulator,
46              final PerceptionFinalizer<C, I> finalizer)
47      {
48          Intermediate<I> intermediate = new Intermediate<>(identity.get());
49          Iterator<H> it = iterator();
50          while (it.hasNext() && !intermediate.isStop())
51          {
52              H next = it.next();
53              intermediate =
54                      accumulator.accumulate(intermediate, (LaneBasedGTU) this.network.getGTU(next.getId()), next.getDistance());
55              intermediate.step();
56          }
57          return finalizer.collect(intermediate.getObject());
58      }
59  
60  }