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://github.com/peter-knoppers">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 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 set of headway
49       */
50      public PerceptionIterableSet(final SortedSet<H> headways)
51      {
52          this.set = headways;
53      }
54  
55      @Override
56      public Iterator<H> iterator()
57      {
58          return this.set.iterator();
59      }
60  
61      @Override
62      public H first()
63      {
64          return this.set.iterator().next();
65      }
66  
67      @Override
68      public boolean isEmpty()
69      {
70          return this.set.isEmpty();
71      }
72  
73  }