View Javadoc
1   package org.opentrafficsim.core.perception.collections;
2   
3   import java.util.Collections;
4   import java.util.NavigableMap;
5   import java.util.NavigableSet;
6   
7   import org.opentrafficsim.core.perception.HistoryManager;
8   
9   /**
10   * NavigableMap-valued historical state. The current navigable map is always maintained, and past states of the navigable map
11   * are obtained by applying the events between now and the requested time in reverse.<br>
12   * <br>
13   * The set views and sub-maps 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 2 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   * @param <M> navigable map type
25   */
26  public abstract class AbstractHistoricalNavigableMap<K, V, M extends NavigableMap<K, V>>
27          extends AbstractHistoricalSortedMap<K, V, M> implements HistoricalNavigableMap<K, V>
28  {
29  
30      /**
31       * Constructor.
32       * @param historyManager HistoryManager; history manager
33       * @param map M; initial map
34       */
35      protected AbstractHistoricalNavigableMap(final HistoryManager historyManager, final M map)
36      {
37          super(historyManager, map);
38      }
39  
40      // Altering NavigableMap methods
41  
42      /** {@inheritDoc} */
43      @Override
44      public Entry<K, V> pollFirstEntry()
45      {
46          if (getMap().isEmpty())
47          {
48              return null;
49          }
50          Entry<K, V> entry = Collections.unmodifiableNavigableMap(getMap()).firstEntry();
51          remove(entry.getKey());
52          return entry;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public Entry<K, V> pollLastEntry()
58      {
59          if (getMap().isEmpty())
60          {
61              return null;
62          }
63          Entry<K, V> entry = Collections.unmodifiableNavigableMap(getMap()).lastEntry();
64          remove(entry.getKey());
65          return entry;
66      }
67  
68      // Non-altering NavigableMap methods
69  
70      /** {@inheritDoc} */
71      @Override
72      public Entry<K, V> lowerEntry(final K key)
73      {
74          return Collections.unmodifiableNavigableMap(getMap()).lowerEntry(key);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public K lowerKey(final K key)
80      {
81          return getMap().lowerKey(key);
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public Entry<K, V> floorEntry(final K key)
87      {
88          return Collections.unmodifiableNavigableMap(getMap()).floorEntry(key);
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public K floorKey(final K key)
94      {
95          return getMap().floorKey(key);
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public Entry<K, V> ceilingEntry(final K key)
101     {
102         return Collections.unmodifiableNavigableMap(getMap()).ceilingEntry(key);
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public K ceilingKey(final K key)
108     {
109         return getMap().ceilingKey(key);
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public Entry<K, V> higherEntry(final K key)
115     {
116         return Collections.unmodifiableNavigableMap(getMap()).higherEntry(key);
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public K higherKey(final K key)
122     {
123         return getMap().higherKey(key);
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public Entry<K, V> firstEntry()
129     {
130         return Collections.unmodifiableNavigableMap(getMap()).firstEntry();
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public Entry<K, V> lastEntry()
136     {
137         return Collections.unmodifiableNavigableMap(getMap()).lastEntry();
138     }
139 
140     /** {@inheritDoc} */
141     @Override
142     public NavigableMap<K, V> descendingMap()
143     {
144         return Collections.unmodifiableNavigableMap(getMap().descendingMap());
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public NavigableSet<K> navigableKeySet()
150     {
151         return Collections.unmodifiableNavigableSet(getMap().navigableKeySet());
152     }
153 
154     /** {@inheritDoc} */
155     @Override
156     public NavigableSet<K> descendingKeySet()
157     {
158         return Collections.unmodifiableNavigableSet(getMap().descendingKeySet());
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public NavigableMap<K, V> subMap(final K fromKey, final boolean fromInclusive, final K toKey, final boolean toInclusive)
164     {
165         return Collections.unmodifiableNavigableMap(getMap().subMap(fromKey, fromInclusive, toKey, toInclusive));
166     }
167 
168     /** {@inheritDoc} */
169     @Override
170     public NavigableMap<K, V> headMap(final K toKey, final boolean inclusive)
171     {
172         return Collections.unmodifiableNavigableMap(getMap().headMap(toKey, inclusive));
173     }
174 
175     /** {@inheritDoc} */
176     @Override
177     public NavigableMap<K, V> tailMap(final K fromKey, final boolean inclusive)
178     {
179         return Collections.unmodifiableNavigableMap(getMap().tailMap(fromKey, inclusive));
180     }
181 
182 }