View Javadoc
1   package org.opentrafficsim.core.perception.collections;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.djunits.value.vdouble.scalar.Time;
7   import org.opentrafficsim.core.perception.HistoryManager;
8   
9   /**
10   * HashMap-valued historical state. The current hash map is always maintained, and past states of the hash map are obtained by
11   * applying the events between now and the requested time in reverse.<br>
12   * <br>
13   * The set views returned by this class are unmodifiable.
14   * <p>
15   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
17   * <p>
18   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 feb. 2018 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   * @param <K> key type
23   * @param <V> value type
24   */
25  public class HistoricalHashMap<K, V> extends AbstractHistoricalMap<K, V, HashMap<K, V>>
26  {
27  
28      /**
29       * Constructor.
30       * @param historyManager HistoryManager; history manager
31       */
32      public HistoricalHashMap(final HistoryManager historyManager)
33      {
34          super(historyManager, new HashMap<>());
35      }
36  
37      /**
38       * Constructor.
39       * @param historyManager HistoryManager; history manager
40       * @param m Map&lt;? extends K, ? extends V&gt;; initial map
41       */
42      public HistoricalHashMap(final HistoryManager historyManager, final Map<? extends K, ? extends V> m)
43      {
44          super(historyManager, new HashMap<>(m));
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      public HashMap<K, V> get()
50      {
51          return getMap();
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public HashMap<K, V> get(final Time time)
57      {
58          if (isLastState(time))
59          {
60              return getMap();
61          }
62          return fill(time, new HashMap<>());
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public String toString()
68      {
69          return "HistoricalHashMap [current=" + getMap() + "]";
70      }
71  
72  }