Class QueueWithSpare<E>

  • Direct Known Subclasses:
    KeyPairQueue

    public class QueueWithSpare<E>
    extends Object
    A Queue with a "spare tire". That is to say, the last entry returned by this queue is always stored in the spare. It is assumed that the queue is populated by another thread which might be lagging for expensive operations (such as key pair generation), so this is a queue that always has an element.

    Note about this and a Java Queue

    The reason that this is a separate class from the java Queue is that the semantics are sufficiently different to invalidate its use: Since this queue always has an element, methods that return an exception when the queue is empty do not operate as expected. Moreover, having Queue inherit from Collection means there are many methods which must be over-ridden and do nothing but throw exceptions to make the semantics consistent with the contract of this class. Therefore, this is an independent class that inherits from nothing.

    Created by Jeff Gaynor
    on 2/20/12 at 9:56 AM

    • Constructor Detail

      • QueueWithSpare

        public QueueWithSpare()
    • Method Detail

      • getSpare

        public E getSpare()
      • setSpare

        public void setSpare​(E spare)
      • push

        public boolean push​(E e)
      • pop

        public E pop()
      • remove

        public E remove()
      • size

        public int size()
      • isEmpty

        public boolean isEmpty()
        Slightly different than expected: This return true if there are no elements in the queue proper AND the spare is null. Since the spare is not included in the calculation of the number of elements in the queue, use size() == 0 to check if there are no elements in this queue.
        Returns:
      • clear

        public void clear()