View Javadoc
1   package org.opentrafficsim.core.perception.collections;
2   
3   import java.util.Collections;
4   import java.util.Iterator;
5   import java.util.NavigableSet;
6   
7   import org.opentrafficsim.core.perception.HistoryManager;
8   
9   /**
10   * NavigableSet-valued historical state. The current navigable set is always maintained, and past states of the navigable set
11   * are obtained by applying the events between now and the requested time in reverse.<br>
12   * <br>
13   * The {@code Iterator} returned by this class does not support the {@code remove()} method. Any returned subset is
14   * unmodifiable.
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
18   * <p>
19   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 2 feb. 2018 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   * @param <E> element type
24   * @param <S> navigable set type
25   */
26  public abstract class AbstractHistoricalNavigableSet<E, S extends NavigableSet<E>> extends AbstractHistoricalSortedSet<E, S>
27          implements HistoricalNavigableSet<E>
28  {
29  
30      /**
31       * Constructor.
32       * @param historyManager HistoryManager; history manager
33       * @param set S; initial set
34       */
35      protected AbstractHistoricalNavigableSet(final HistoryManager historyManager, final S set)
36      {
37          super(historyManager, set);
38      }
39  
40      /** {@inheritDoc} */
41      @Override
42      public String toString()
43      {
44          return "HistoricalNavigableSet [current=" + getCollection() + "]";
45      }
46  
47      // Altering NavigableSet methods
48  
49      /** {@inheritDoc} */
50      @Override
51      public E pollFirst()
52      {
53          if (getCollection().isEmpty())
54          {
55              return null;
56          }
57          E element = getCollection().first();
58          remove(element);
59          return element;
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public E pollLast()
65      {
66          if (getCollection().isEmpty())
67          {
68              return null;
69          }
70          E element = getCollection().last();
71          remove(element);
72          return element;
73      }
74  
75      // Non-altering NavigableSet methods
76  
77      /** {@inheritDoc} */
78      @Override
79      public E lower(final E e)
80      {
81          return getCollection().lower(e);
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public E floor(final E e)
87      {
88          return getCollection().floor(e);
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public E ceiling(final E e)
94      {
95          return getCollection().ceiling(e);
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public E higher(final E e)
101     {
102         return getCollection().higher(e);
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public NavigableSet<E> descendingSet()
108     {
109         return Collections.unmodifiableNavigableSet(getCollection().descendingSet());
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public Iterator<E> descendingIterator()
115     {
116         return Collections.unmodifiableNavigableSet(getCollection()).descendingIterator();
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
122             final boolean toInclusive)
123     {
124         return Collections.unmodifiableNavigableSet(getCollection().subSet(fromElement, fromInclusive, toElement, toInclusive));
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public NavigableSet<E> headSet(final E toElement, final boolean inclusive)
130     {
131         return Collections.unmodifiableNavigableSet(getCollection().headSet(toElement, inclusive));
132     }
133 
134     /** {@inheritDoc} */
135     @Override
136     public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive)
137     {
138         return Collections.unmodifiableNavigableSet(getCollection().tailSet(fromElement, inclusive));
139     }
140 
141 }