Class Async.Queue<T>

  • Type Parameters:
    T - async result type
    Enclosing class:
    Async<T>

    public static final class Async.Queue<T>
    extends Object
    A queue for handling Async instances. The queue can be polled for added Async instances that have completed, or a handler can be attached to run on completion.

    An Async may only be in one queue at a time. Adding to the queue will automatically remove from any previous queue.

    A queue cannot be constructed directly. Use the Inject annotation on a field. Queues will have handlers and limits automatically removed on reset, and will be cleared on disposal.

    • Method Detail

      • poll

        public Async<T> poll()
        Retrieves and removes the next completed Async, if available. Returns null if no completed Async is available. The caller should check whether any returned Async has failed before extracting the result or error.
        Returns:
        next completed Async or null
      • add

        public Async<T> add​(Async<T> async)
        Add an Async to the queue. If the Async is already completed, and an onDone handler has been attached, this will be executed before this method returns.

        If the queue size limit has been reached, the least recently added Async will be evicted and returned to make space.

        Parameters:
        async - Async to add to queue
        Returns:
        evicted Async or null
      • remove

        public boolean remove​(Async<T> async)
        Remove the provided Async from the queue. If the provided Async is not in this queue, this is a no-op.
        Parameters:
        async - Async to remove
        Returns:
        true if an element was removed
      • limit

        public List<Async<T>> limit​(int size)
        Limit the queue to the provided size. If the current queue size is above the limit, the least recently added Async will be evicted and returned to make space.
        Parameters:
        size - requested size limit - at least 1.
        Returns:
        evicted Async or empty list
        Throws:
        IllegalArgumentException - if size is less than 1.
      • size

        public int size()
        Query current number of Async in the queue.
        Returns:
        current number of Async
      • clear

        public List<Async<T>> clear()
        Clear all Async from the queue. Removed Async will be returned.
        Returns:
        list of removed Async or empty list
      • onDone

        public void onDone​(Consumer<Async<T>> handler)
        Attach a handler for completed Async. Only one handler may be attached at a time. The handler will be run when any Async in the queue completes. The handler should check for failure before extracting any result or error. Any already completed Async in the queue will be passed to the handler before this method returns. A null value may be passed to remove the current handler.
        Parameters:
        handler - handler for completed Async, or null to remove
      • onDone

        public void onDone​(Consumer<T> resultHandler,
                           Consumer<PError> errorHandler)
        Convenience method to link separate result and error handlers to onDone(java.util.function.Consumer). This method does not accept null values.
        Parameters:
        resultHandler - handler for succesful Async result
        errorHandler - handler for failed Async errors