HistoricalHashMap.java

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

  2. import java.util.LinkedHashMap;
  3. import java.util.Map;

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

  6. /**
  7.  * LinkedHashMap-valued historical state. The current hash map is always maintained, and past states of the hash map are obtained by
  8.  * applying the events between now and the requested time in reverse.<br>
  9.  * <br>
  10.  * The set views returned by this class are unmodifiable.
  11.  * <p>
  12.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  13.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  14.  * <p>
  15.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 feb. 2018 <br>
  16.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  17.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  18.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  19.  * @param <K> key type
  20.  * @param <V> value type
  21.  */
  22. public class HistoricalHashMap<K, V> extends AbstractHistoricalMap<K, V, LinkedHashMap<K, V>>
  23. {

  24.     /**
  25.      * Constructor.
  26.      * @param historyManager HistoryManager; history manager
  27.      */
  28.     public HistoricalHashMap(final HistoryManager historyManager)
  29.     {
  30.         super(historyManager, new LinkedHashMap<>());
  31.     }

  32.     /**
  33.      * Constructor.
  34.      * @param historyManager HistoryManager; history manager
  35.      * @param m Map&lt;? extends K, ? extends V&gt;; initial map
  36.      */
  37.     public HistoricalHashMap(final HistoryManager historyManager, final Map<? extends K, ? extends V> m)
  38.     {
  39.         super(historyManager, new LinkedHashMap<>(m));
  40.     }

  41.     /** {@inheritDoc} */
  42.     @Override
  43.     public LinkedHashMap<K, V> get()
  44.     {
  45.         return getMap();
  46.     }

  47.     /** {@inheritDoc} */
  48.     @Override
  49.     public LinkedHashMap<K, V> get(final Time time)
  50.     {
  51.         if (isLastState(time))
  52.         {
  53.             return getMap();
  54.         }
  55.         return fill(time, new LinkedHashMap<>());
  56.     }

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     public String toString()
  60.     {
  61.         return "HistoricalHashMap [current=" + getMap() + "]";
  62.     }

  63. }