Class IndexedBlockingQueue<E extends IDIndexedAccessible>

  • Direct Known Subclasses:
    L1PriorityQueue, L2PriorityQueue

    public abstract class IndexedBlockingQueue<E extends IDIndexedAccessible>
    extends java.lang.Object
    The base class of a special kind of blocking queue, which has these characters:

    1. Thread-safe.

    2. Can poll from queue head. When the queue is empty, the poll() will be blocked until an element is inserted.

    3. Can push a non-null element to queue. When the queue is beyond the max size, an exception will be thrown.

    4. Can remove an element by a type of ID.

    5. Each element has the different ID.

    • Constructor Summary

      Constructors 
      Constructor Description
      IndexedBlockingQueue​(int maxCapacity, E queryHolder)
      Init the queue with a max capacity.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clear all the elements in the queue.
      protected abstract void clearAllElements()
      Clear all elements in this queue.
      protected abstract boolean contains​(E element)
      Check whether an element with the same ID exists.
      protected abstract E get​(E element)
      Return the element with the same id of the input, null if it doesn't exist.
      E get​(ID id)
      Get the element by id.
      protected abstract boolean isEmpty()
      Whether the queue is empty.
      E poll()
      Get and remove the first element of the queue.
      protected abstract E pollFirst()
      Get and remove the first element.
      void push​(E element)
      Push an element to the queue.
      protected abstract void pushToQueue​(E element)
      Push the element into the queue.
      protected abstract E remove​(E element)
      Remove and return the element by its ID.
      E remove​(ID id)
      Remove and return the element by id.
      int size()
      Get the current queue size.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IndexedBlockingQueue

        public IndexedBlockingQueue​(int maxCapacity,
                                    E queryHolder)
        Init the queue with a max capacity. The queryHolder is just a simple reused object in query to avoid small objects allocation. It should be not used in any other places out of the queue as the id may be mutated.
        Parameters:
        maxCapacity - the max capacity of the queue.
        queryHolder - the query holder instance.
        Throws:
        java.lang.IllegalArgumentException - if maxCapacity <= 0.
    • Method Detail

      • poll

        public E poll()
               throws java.lang.InterruptedException
        Get and remove the first element of the queue. If the queue is empty, this call will be blocked until an element has been pushed.
        Returns:
        the queue head element.
        Throws:
        java.lang.InterruptedException
      • push

        public void push​(E element)
        Push an element to the queue. The new element position is determined by the implementation. If the queue size has been reached the maxCapacity, or the queue has already contained an element with the same ID, an IllegalStateException will be thrown. If the element is null, an NullPointerException will be thrown.
        Parameters:
        element - the element to be pushed.
        Throws:
        java.lang.NullPointerException - the pushed element is null.
        java.lang.IllegalStateException - the queue size has been reached the maxCapacity, or the queue has already contained the same ID element .
      • remove

        public E remove​(ID id)
        Remove and return the element by id. It returns null if it doesn't exist.
        Parameters:
        id - the id of the element to be removed.
        Returns:
        the removed element.
      • get

        public E get​(ID id)
        Get the element by id. It returns null if it doesn't exist.
        Parameters:
        id - the id of the element.
        Returns:
        the removed element.
      • clear

        public void clear()
        Clear all the elements in the queue.
      • size

        public final int size()
        Get the current queue size.
        Returns:
        the current queue size.
      • isEmpty

        protected abstract boolean isEmpty()
        Whether the queue is empty.

        This implementation needn't be thread-safe.

        Returns:
        true if the queue is empty, otherwise false.
      • pollFirst

        protected abstract E pollFirst()
        Get and remove the first element.

        This implementation needn't be thread-safe.

        Returns:
        The first element.
      • pushToQueue

        protected abstract void pushToQueue​(E element)
        Push the element into the queue.

        This implementation needn't be thread-safe.

        Parameters:
        element - the element to be pushed.
      • remove

        protected abstract E remove​(E element)
        Remove and return the element by its ID. It returns null if it doesn't exist.

        This implementation needn't be thread-safe.

        Parameters:
        element - the element to be removed.
        Returns:
        the removed element.
      • contains

        protected abstract boolean contains​(E element)
        Check whether an element with the same ID exists.

        This implementation needn't be thread-safe.

        Parameters:
        element - the element to be checked.
        Returns:
        true if an element with the same ID exists, otherwise false.
      • get

        protected abstract E get​(E element)
        Return the element with the same id of the input, null if it doesn't exist.

        This implementation needn't be thread-safe.

        Parameters:
        element - the element to be queried.
        Returns:
        the element with the same id in the queue. Null if it doesn't exist.
      • clearAllElements

        protected abstract void clearAllElements()
        Clear all elements in this queue.

        This implementation needn't be thread-safe.