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