HistoricalCollection.java

  1. package org.opentrafficsim.core.perception.collections;

  2. import java.util.Collection;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.Objects;
  6. import java.util.function.Predicate;

  7. import org.djunits.value.vdouble.scalar.Time;

  8. /**
  9.  * Interface for historical collections.
  10.  * <p>
  11.  * Copyright (c) 2013-2018 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 3 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 <E> element type
  19.  */
  20. public interface HistoricalCollection<E> extends Collection<E>
  21. {

  22.     /**
  23.      * Returns the current collection. This is not intended to be modified, and may be an unmodifiable.
  24.      * @return Collection; current collection
  25.      */
  26.     Collection<E> get();

  27.     /**
  28.      * Returns a past collection.
  29.      * @param time Time; time to obtain the collection at
  30.      * @return Collection; past collection
  31.      */
  32.     Collection<E> get(Time time);

  33.     /** {@inheritDoc} */
  34.     @Override
  35.     default boolean removeIf(Predicate<? super E> filter)
  36.     {
  37.         Objects.requireNonNull(filter);
  38.         boolean removed = false;
  39.         HashSet<E> removes = new HashSet<>();
  40.         final Iterator<E> each = iterator();
  41.         while (each.hasNext())
  42.         {
  43.             E next = each.next();
  44.             if (filter.test(next))
  45.             {
  46.                 // super uses Iterator.remove() which is not supported, can't use remove() due to concurrency
  47.                 removes.add(next);
  48.                 removed = true;
  49.             }
  50.         }
  51.         for (E e : removes)
  52.         {
  53.             remove(e);
  54.         }
  55.         return removed;
  56.     }

  57. }