View Javadoc
1   package org.opentrafficsim.core.perception.collections;
2   
3   import java.util.Queue;
4   
5   import org.opentrafficsim.core.perception.HistoryManager;
6   
7   /**
8    * Queue-valued historical state. The current queue is always maintained, and past states of the queue are obtained by applying
9    * the events between now and the requested time in reverse.<br>
10   * <br>
11   * The {@code Iterator} returned by this class does not support the {@code remove()} method.
12   * <p>
13   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
15   * <p>
16   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 4 feb. 2018 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
20   * @param <E> element type
21   * @param <Q> queue type
22   */
23  public abstract class AbstractHistoricalQueue<E, Q extends Queue<E>> extends AbstractHistoricalCollection<E, Q>
24          implements HistoricalQueue<E>
25  {
26  
27      /**
28       * Constructor.
29       * @param historyManager HistoryManager; history manager
30       * @param queue Q; initial queue
31       */
32      protected AbstractHistoricalQueue(final HistoryManager historyManager, final Q queue)
33      {
34          super(historyManager, queue);
35      }
36  
37      // Altering PriorityQueue methods
38  
39      /** {@inheritDoc} */
40      @Override
41      public boolean offer(final E e)
42      {
43          boolean added = getCollection().offer(e);
44          if (added)
45          {
46              addEvent(new AddEvent<>(now().si, e));
47          }
48          return added;
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public E remove()
54      {
55          E e = getCollection().remove();
56          addEvent(new RemoveEvent<>(now().si, e));
57          return e;
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public E poll()
63      {
64          if (isEmpty())
65          {
66              return null;
67          }
68          return remove();
69      }
70  
71      // Non-altering PriorityQueue methods
72  
73      /** {@inheritDoc} */
74      @Override
75      public E peek()
76      {
77          return this.getCollection().peek();
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public E element()
83      {
84          return this.getCollection().element();
85      }
86  
87  }