AbstractHistoricalNavigableMap.java

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

  2. import java.util.Collections;
  3. import java.util.NavigableMap;
  4. import java.util.NavigableSet;

  5. import org.opentrafficsim.core.perception.HistoryManager;

  6. /**
  7.  * NavigableMap-valued historical state. The current navigable map is always maintained, and past states of the navigable map
  8.  * are obtained by applying the events between now and the requested time in reverse.<br>
  9.  * <br>
  10.  * The set views and sub-maps returned by this class are unmodifiable.
  11.  * <p>
  12.  * Copyright (c) 2013-2019 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 2 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.  * @param <M> navigable map type
  22.  */
  23. public abstract class AbstractHistoricalNavigableMap<K, V, M extends NavigableMap<K, V>>
  24.         extends AbstractHistoricalSortedMap<K, V, M> implements HistoricalNavigableMap<K, V>
  25. {

  26.     /**
  27.      * Constructor.
  28.      * @param historyManager HistoryManager; history manager
  29.      * @param map M; initial map
  30.      */
  31.     protected AbstractHistoricalNavigableMap(final HistoryManager historyManager, final M map)
  32.     {
  33.         super(historyManager, map);
  34.     }

  35.     // Altering NavigableMap methods

  36.     /** {@inheritDoc} */
  37.     @Override
  38.     public Entry<K, V> pollFirstEntry()
  39.     {
  40.         if (getMap().isEmpty())
  41.         {
  42.             return null;
  43.         }
  44.         Entry<K, V> entry = Collections.unmodifiableNavigableMap(getMap()).firstEntry();
  45.         remove(entry.getKey());
  46.         return entry;
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public Entry<K, V> pollLastEntry()
  51.     {
  52.         if (getMap().isEmpty())
  53.         {
  54.             return null;
  55.         }
  56.         Entry<K, V> entry = Collections.unmodifiableNavigableMap(getMap()).lastEntry();
  57.         remove(entry.getKey());
  58.         return entry;
  59.     }

  60.     // Non-altering NavigableMap methods

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public Entry<K, V> lowerEntry(final K key)
  64.     {
  65.         return Collections.unmodifiableNavigableMap(getMap()).lowerEntry(key);
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public K lowerKey(final K key)
  70.     {
  71.         return getMap().lowerKey(key);
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public Entry<K, V> floorEntry(final K key)
  76.     {
  77.         return Collections.unmodifiableNavigableMap(getMap()).floorEntry(key);
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public K floorKey(final K key)
  82.     {
  83.         return getMap().floorKey(key);
  84.     }

  85.     /** {@inheritDoc} */
  86.     @Override
  87.     public Entry<K, V> ceilingEntry(final K key)
  88.     {
  89.         return Collections.unmodifiableNavigableMap(getMap()).ceilingEntry(key);
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public K ceilingKey(final K key)
  94.     {
  95.         return getMap().ceilingKey(key);
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     public Entry<K, V> higherEntry(final K key)
  100.     {
  101.         return Collections.unmodifiableNavigableMap(getMap()).higherEntry(key);
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     public K higherKey(final K key)
  106.     {
  107.         return getMap().higherKey(key);
  108.     }

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     public Entry<K, V> firstEntry()
  112.     {
  113.         return Collections.unmodifiableNavigableMap(getMap()).firstEntry();
  114.     }

  115.     /** {@inheritDoc} */
  116.     @Override
  117.     public Entry<K, V> lastEntry()
  118.     {
  119.         return Collections.unmodifiableNavigableMap(getMap()).lastEntry();
  120.     }

  121.     /** {@inheritDoc} */
  122.     @Override
  123.     public NavigableMap<K, V> descendingMap()
  124.     {
  125.         return Collections.unmodifiableNavigableMap(getMap().descendingMap());
  126.     }

  127.     /** {@inheritDoc} */
  128.     @Override
  129.     public NavigableSet<K> navigableKeySet()
  130.     {
  131.         return Collections.unmodifiableNavigableSet(getMap().navigableKeySet());
  132.     }

  133.     /** {@inheritDoc} */
  134.     @Override
  135.     public NavigableSet<K> descendingKeySet()
  136.     {
  137.         return Collections.unmodifiableNavigableSet(getMap().descendingKeySet());
  138.     }

  139.     /** {@inheritDoc} */
  140.     @Override
  141.     public NavigableMap<K, V> subMap(final K fromKey, final boolean fromInclusive, final K toKey, final boolean toInclusive)
  142.     {
  143.         return Collections.unmodifiableNavigableMap(getMap().subMap(fromKey, fromInclusive, toKey, toInclusive));
  144.     }

  145.     /** {@inheritDoc} */
  146.     @Override
  147.     public NavigableMap<K, V> headMap(final K toKey, final boolean inclusive)
  148.     {
  149.         return Collections.unmodifiableNavigableMap(getMap().headMap(toKey, inclusive));
  150.     }

  151.     /** {@inheritDoc} */
  152.     @Override
  153.     public NavigableMap<K, V> tailMap(final K fromKey, final boolean inclusive)
  154.     {
  155.         return Collections.unmodifiableNavigableMap(getMap().tailMap(fromKey, inclusive));
  156.     }

  157. }