Class AbstractDrainingQueue<T>

  • Type Parameters:
    T - The type of the items in the queue.
    Direct Known Subclasses:
    BlockingDrainingQueue, PriorityBlockingDrainingQueue

    public abstract class AbstractDrainingQueue<T>
    extends java.lang.Object
    Thread-safe queue that dequeues one or more elements at once. Provides asynchronous methods for waiting for items to be added (if empty) and enables instantaneous dequeuing of all elements.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(T item)
      Adds a new item to the queue.
      protected abstract void addInternal​(T item)
      Updates the internal data structure to include the given item.
      void cancelPendingTake()
      Cancels any pending Future from a take() operation.
      java.util.Queue<T> close()
      Closes the queue and prevents any other access to it.
      protected abstract java.util.Queue<T> fetch​(int maxCount)
      Extracts a number of items from the queue.
      protected java.util.concurrent.CompletableFuture<java.util.Queue<T>> newTakeResult()  
      T peek()
      Returns (without removing) the first item in the Queue.
      protected abstract T peekInternal()
      Returns the first item in the Queue.
      java.util.Queue<T> poll​(int maxCount)
      Returns the next items from the queue, if any.
      int size()
      Gets a value indicating the size of this queue.
      protected abstract int sizeInternal()
      Returns the size of the queue.
      java.util.concurrent.CompletableFuture<java.util.Queue<T>> take​(int maxCount)
      Returns the next items from the queue.
      java.util.concurrent.CompletableFuture<java.util.Queue<T>> take​(int maxCount, java.time.Duration timeout, java.util.concurrent.ScheduledExecutorService timeoutExecutor)
      Returns the next items from the queue.
      • Methods inherited from class java.lang.Object

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

      • AbstractDrainingQueue

        public AbstractDrainingQueue()
    • Method Detail

      • close

        public java.util.Queue<T> close()
        Closes the queue and prevents any other access to it. Any blocked call to takeAllItems() will fail with InterruptedException.
        Returns:
        If the queue has any more items in it, these will be returned here in the order in which they were inserted. The items are guaranteed not to be returned both here and via take()/poll().
      • cancelPendingTake

        public void cancelPendingTake()
        Cancels any pending Future from a take() operation.
      • add

        public void add​(T item)
        Adds a new item to the queue.
        Parameters:
        item - The item to add.
        Throws:
        ObjectClosedException - If the Queue is closed.
      • poll

        public java.util.Queue<T> poll​(int maxCount)
        Returns the next items from the queue, if any.
        Parameters:
        maxCount - The maximum number of items to return.
        Returns:
        A Queue containing at most maxCount items, or empty if there is nothing in the queue.
        Throws:
        java.lang.IllegalStateException - If there is a pending take() operation which hasn't completed yet.
      • take

        public java.util.concurrent.CompletableFuture<java.util.Queue<T>> take​(int maxCount)
        Returns the next items from the queue. If the queue is empty, it blocks the call until at least one item is added.
        Parameters:
        maxCount - The maximum number of items to return. This argument will be ignored if the queue is currently empty, but in that case the result will always be completed with exactly one element.
        Returns:
        A CompletableFuture that, when completed, will contain the requested result. If the queue is not currently empty, this Future will already be completed, otherwise it will be completed the next time the add() method is called. If the queue is closed and this Future is not yet completed, it will be cancelled.
        Throws:
        ObjectClosedException - If the Queue is closed.
        java.lang.IllegalStateException - If another call to take() is in progress.
      • take

        public java.util.concurrent.CompletableFuture<java.util.Queue<T>> take​(int maxCount,
                                                                               java.time.Duration timeout,
                                                                               java.util.concurrent.ScheduledExecutorService timeoutExecutor)
        Returns the next items from the queue. If the queue is empty, it blocks the call until at least one item is added, or until the given timeout expires.
        Parameters:
        maxCount - The maximum number of items to return. This argument will be ignored if the queue is currently empty, but in that case the result will always be completed with exactly one element.
        timeout - Timeout.
        timeoutExecutor - An Executor to use for timing out.
        Returns:
        A CompletableFuture that, when completed, will contain the requested result. If the queue is not currently empty, this Future will already be completed, otherwise it will be completed the next time the add() method is called. If the queue is closed and this Future is not yet completed, it will be cancelled. If the timeout expires prior to adding a new element, this future will be completed with a TimeoutException.
        Throws:
        ObjectClosedException - If the Queue is closed.
        java.lang.IllegalStateException - If another call to take() is in progress.
      • peek

        public T peek()
        Returns (without removing) the first item in the Queue.
        Returns:
        The first item, or null if size() is 0.
      • size

        public int size()
        Gets a value indicating the size of this queue.
        Returns:
        The size.
      • addInternal

        protected abstract void addInternal​(T item)
        Updates the internal data structure to include the given item. NOTE: this is invoked while holding the lock. There is no need for additional synchronization in the implementation.
        Parameters:
        item - The item to include.
      • sizeInternal

        protected abstract int sizeInternal()
        Returns the size of the queue. NOTE: this is invoked while holding the lock. There is no need for additional synchronization in the implementation.
        Returns:
        The size of the queue.
      • peekInternal

        protected abstract T peekInternal()
        Returns the first item in the Queue. NOTE: this is invoked while holding the lock. There is no need for additional synchronization in the implementation.
        Returns:
        The first item, or null if size() is 0.
      • fetch

        protected abstract java.util.Queue<T> fetch​(int maxCount)
        Extracts a number of items from the queue. NOTE: this is invoked while holding the lock. There is no need for additional synchronization in the implementation.
        Parameters:
        maxCount - The maximum number of items to extract.
        Returns:
        The extracted items, in order.
      • newTakeResult

        protected java.util.concurrent.CompletableFuture<java.util.Queue<T>> newTakeResult()