PerceptionIterableSet.java

  1. package org.opentrafficsim.road.gtu.lane.perception;

  2. import java.util.Iterator;
  3. import java.util.LinkedHashSet;
  4. import java.util.Set;
  5. import java.util.SortedSet;

  6. import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;

  7. /**
  8.  * Simple implementation of {@code PerceptionIterable} which wraps a set. Constructors are available for an empty set, a
  9.  * single-valued set, or a sorted set.
  10.  * <p>
  11.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  13.  * <p>
  14.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 26 feb. 2018 <br>
  15.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  16.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  17.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  18.  * @param <H> headway type
  19.  */
  20. public class PerceptionIterableSet<H extends Headway> implements PerceptionIterable<H>
  21. {

  22.     /** Internal set. */
  23.     private Set<H> set;

  24.     /**
  25.      * Creates an empty iterable.
  26.      */
  27.     public PerceptionIterableSet()
  28.     {
  29.         this.set = new LinkedHashSet<>();
  30.     }

  31.     /**
  32.      * Creates a single-value iterable.
  33.      * @param headway H; headway
  34.      */
  35.     public PerceptionIterableSet(final H headway)
  36.     {
  37.         this.set = new LinkedHashSet<>();
  38.         this.set.add(headway);
  39.     }

  40.     /**
  41.      * Creates an iterable from a sorted set.
  42.      * @param headways SortedSet&lt;H&gt;; set of headway
  43.      */
  44.     public PerceptionIterableSet(final SortedSet<H> headways)
  45.     {
  46.         this.set = headways;
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public Iterator<H> iterator()
  51.     {
  52.         return this.set.iterator();
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public H first()
  57.     {
  58.         return this.set.iterator().next();
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public boolean isEmpty()
  63.     {
  64.         return this.set.isEmpty();
  65.     }

  66. }