AbstractHistoricalQueue.java

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

  2. import java.util.Queue;

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

  4. /**
  5.  * Queue-valued historical state. The current queue is always maintained, and past states of the queue are obtained by applying
  6.  * the events between now and the requested time in reverse.<br>
  7.  * <br>
  8.  * The {@code Iterator} returned by this class does not support the {@code remove()} method.
  9.  * <p>
  10.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  11.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  12.  * <p>
  13.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 4 feb. 2018 <br>
  14.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  15.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  16.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  17.  * @param <E> element type
  18.  * @param <Q> queue type
  19.  */
  20. public abstract class AbstractHistoricalQueue<E, Q extends Queue<E>> extends AbstractHistoricalCollection<E, Q>
  21.         implements HistoricalQueue<E>
  22. {

  23.     /**
  24.      * Constructor.
  25.      * @param historyManager HistoryManager; history manager
  26.      * @param queue Q; initial queue
  27.      */
  28.     protected AbstractHistoricalQueue(final HistoryManager historyManager, final Q queue)
  29.     {
  30.         super(historyManager, queue);
  31.     }

  32.     // Altering PriorityQueue methods

  33.     /** {@inheritDoc} */
  34.     @Override
  35.     public boolean offer(final E e)
  36.     {
  37.         boolean added = getCollection().offer(e);
  38.         if (added)
  39.         {
  40.             addEvent(new AddEvent<>(now().si, e));
  41.         }
  42.         return added;
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public E remove()
  47.     {
  48.         E e = getCollection().remove();
  49.         addEvent(new RemoveEvent<>(now().si, e));
  50.         return e;
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public E poll()
  55.     {
  56.         if (isEmpty())
  57.         {
  58.             return null;
  59.         }
  60.         return remove();
  61.     }

  62.     // Non-altering PriorityQueue methods

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public E peek()
  66.     {
  67.         return this.getCollection().peek();
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public E element()
  72.     {
  73.         return this.getCollection().element();
  74.     }

  75. }