View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import java.util.Iterator;
4   import java.util.LinkedHashSet;
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-2024 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://github.com/wjschakel">Wouter Schakel</a>
20   * @param <H> headway type
21   */
22  public class PerceptionIterableSet<H extends Headway> implements PerceptionIterable<H>
23  {
24  
25      /** Internal set. */
26      private Set<H> set;
27  
28      /**
29       * Creates an empty iterable.
30       */
31      public PerceptionIterableSet()
32      {
33          this.set = new LinkedHashSet<>();
34      }
35  
36      /**
37       * Creates a single-value iterable.
38       * @param headway H; headway
39       */
40      public PerceptionIterableSet(final H headway)
41      {
42          this.set = new LinkedHashSet<>();
43          this.set.add(headway);
44      }
45  
46      /**
47       * Creates an iterable from a sorted set.
48       * @param headways SortedSet&lt;H&gt;; set of headway
49       */
50      public PerceptionIterableSet(final SortedSet<H> headways)
51      {
52          this.set = headways;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public Iterator<H> iterator()
58      {
59          return this.set.iterator();
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public H first()
65      {
66          return this.set.iterator().next();
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public boolean isEmpty()
72      {
73          return this.set.isEmpty();
74      }
75  
76  }