View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import java.util.HashSet;
4   import java.util.Iterator;
5   import java.util.Set;
6   import java.util.SortedSet;
7   
8   import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
9   
10  /**
11   * Simple implementation of {@code PerceptionIterable} which wraps a set. Constructors are available for an empty set, a
12   * single-valued set, or a sorted set.
13   * <p>
14   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
16   * <p>
17   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 26 feb. 2018 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21   * @param <H> headway type
22   */
23  public class PerceptionIterableSet<H extends Headway> implements PerceptionIterable<H>
24  {
25  
26      /** Internal set. */
27      private Set<H> set;
28  
29      /**
30       * Creates an empty iterable.
31       */
32      public PerceptionIterableSet()
33      {
34          this.set = new HashSet<>();
35      }
36  
37      /**
38       * Creates a single-value iterable.
39       * @param headway H; headway
40       */
41      public PerceptionIterableSet(final H headway)
42      {
43          this.set = new HashSet<>();
44          this.set.add(headway);
45      }
46  
47      /**
48       * Creates an iterable from a sorted set.
49       * @param headways SortedSet; set of headway
50       */
51      public PerceptionIterableSet(final SortedSet<H> headways)
52      {
53          this.set = headways;
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public Iterator<H> iterator()
59      {
60          return this.set.iterator();
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public H first()
66      {
67          return this.set.iterator().next();
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public boolean isEmpty()
73      {
74          return this.set.isEmpty();
75      }
76  
77  }