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
  8.  * obtained by 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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  13.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  14.  * </p>
  15.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  16.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  17.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  18.  * @param <K> key type
  19.  * @param <V> value type
  20.  */
  21. public class HistoricalHashMap<K, V> extends AbstractHistoricalMap<K, V, LinkedHashMap<K, V>>
  22. {

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

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

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

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

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

  62. }