HistoricalLinkedHashSet.java

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

  2. import java.util.Collection;
  3. import java.util.LinkedHashSet;

  4. import org.djunits.value.vdouble.scalar.Time;
  5. import org.opentrafficsim.core.perception.HistoryManager;

  6. /**
  7.  * LinkedHashSet-valued historical state. The current linked hash set is always maintained, and past states of the linked hash
  8.  * set are obtained by applying the events between now and the requested time in reverse.<br>
  9.  * <br>
  10.  * The {@code Iterator} returned by this class does not support the {@code remove()} method. Any returned subset is
  11.  * unmodifiable.
  12.  * <p>
  13.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  14.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  15.  * </p>
  16.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  17.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  18.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  19.  * @param <E> element type
  20.  */
  21. public class HistoricalLinkedHashSet<E> extends AbstractHistoricalCollection<E, LinkedHashSet<E>> implements HistoricalSet<E>
  22. {

  23.     /**
  24.      * Constructor.
  25.      * @param historyManager history manager
  26.      * @param owner object that owns the historical value
  27.      */
  28.     public HistoricalLinkedHashSet(final HistoryManager historyManager, final Object owner)
  29.     {
  30.         super(historyManager, owner, new LinkedHashSet<>());
  31.     }

  32.     /**
  33.      * Constructor.
  34.      * @param historyManager history manager
  35.      * @param owner object that owns the historical value
  36.      * @param c initial collection
  37.      */
  38.     public HistoricalLinkedHashSet(final HistoryManager historyManager, final Object owner, final Collection<? extends E> c)
  39.     {
  40.         super(historyManager, owner, new LinkedHashSet<>(c));
  41.     }

  42.     @Override
  43.     public LinkedHashSet<E> get()
  44.     {
  45.         return getCollection();
  46.     }

  47.     @Override
  48.     public LinkedHashSet<E> get(final Time time)
  49.     {
  50.         if (isLastState(time))
  51.         {
  52.             return getCollection();
  53.         }
  54.         return fill(time, new LinkedHashSet<>());
  55.     }

  56.     @Override
  57.     public String toString()
  58.     {
  59.         return "HistoricalLinkedHashSet [current=" + getCollection() + "]";
  60.     }

  61. }