Class SuccessAsyncFuture<T>

  • All Implemented Interfaces:
    AsyncFuture<T>, AsyncPromise<T>, java.util.concurrent.CompletionStage<T>, java.util.concurrent.Future<T>

    public final class SuccessAsyncFuture<T>
    extends AbstractAsyncFuture<T>
    implements AsyncPromise<T>
    A simple implementation of AsyncFuture which behaves as if the Future has already completed successfully.

    note: The AsyncFuture classes would be replaced with the Promise classes available in Netty4.

    • Constructor Detail

      • SuccessAsyncFuture

        public SuccessAsyncFuture​(T value)
        Construct an AsyncFuture which is already in success state.
        Parameters:
        value - Value to be returned by get().
    • Method Detail

      • isSuccess

        public boolean isSuccess()
        Returns true if and only if the I/O operation was completed successfully.
        Specified by:
        isSuccess in interface AsyncFuture<T>
      • getCause

        public java.lang.Throwable getCause()
        Returns the cause of the failed I/O operation if the I/O operation has failed.
        Specified by:
        getCause in interface AsyncFuture<T>
        Returns:
        the cause of the failure. null if succeeded or this future is not completed yet.
      • setSuccess

        public boolean setSuccess​(T result)
        Marks this future as a success and notifies all listeners.
        Specified by:
        setSuccess in interface AsyncPromise<T>
        Returns:
        true if and only if successfully marked this future as a success. Otherwise false because this future is already marked as either a success or a failure.
      • setFailure

        public boolean setFailure​(@Nonnull
                                  java.lang.Throwable cause)
        Marks this future as a failure and notifies all listeners.
        Specified by:
        setFailure in interface AsyncPromise<T>
        Returns:
        true if and only if successfully marked this future as a failure. Otherwise false because this future is already marked as either a success or a failure.
      • addListener

        @Nonnull
        public AsyncPromise<T> addListener​(AsyncFutureListener<T> listener)
        Adds the specified listener to this future. The specified listener is notified when this future is done. If this future is already completed, the specified listener is notified immediately.
        Specified by:
        addListener in interface AsyncFuture<T>
        Specified by:
        addListener in interface AsyncPromise<T>
        Returns:
        this to permit chaining of operations.
      • addListener

        @Nonnull
        public AsyncPromise<T> addListener​(AsyncPromise<T> listener)
        Adds the specified future as a listener to this future. The specified future is notified when this future is done. If this future is already completed, the specified future is notified immediately.
        Specified by:
        addListener in interface AsyncFuture<T>
        Specified by:
        addListener in interface AsyncPromise<T>
        Parameters:
        listener -
        Returns:
        this to permit chaining of operations.
      • await

        @Nonnull
        public AsyncFuture<T> await()
                             throws java.lang.InterruptedException
        Waits for this future to be completed.
        Specified by:
        await in interface AsyncFuture<T>
        Throws:
        java.lang.InterruptedException - if the current thread was interrupted
      • awaitUninterruptibly

        @Nonnull
        public AsyncFuture<T> awaitUninterruptibly()
        Waits for this future to be completed without interruption. This method catches an InterruptedException and discards it silently.
        Specified by:
        awaitUninterruptibly in interface AsyncFuture<T>
      • await

        public boolean await​(long timeout,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.InterruptedException
        Waits for this future to be completed within the specified time limit.
        Specified by:
        await in interface AsyncFuture<T>
        Returns:
        true if and only if the future was completed within the specified time limit
        Throws:
        java.lang.InterruptedException - if the current thread was interrupted
      • awaitUninterruptibly

        public boolean awaitUninterruptibly​(long timeout,
                                            java.util.concurrent.TimeUnit unit)
        Waits for this future to be completed within the specified time limit without interruption. This method catches an InterruptedException and discards it silently.
        Specified by:
        awaitUninterruptibly in interface AsyncFuture<T>
        Returns:
        true if and only if the future was completed within the specified time limit
      • cancel

        public boolean cancel​(boolean mayInterruptIfRunning)
        Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

        After this method returns, subsequent calls to isDone() will always return true. Subsequent calls to isCancelled() will always return true if this method returned true.

        Specified by:
        cancel in interface java.util.concurrent.Future<T>
        Parameters:
        mayInterruptIfRunning - true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete
        Returns:
        false if the task could not be cancelled, typically because it has already completed normally; true otherwise
      • isCancelled

        public boolean isCancelled()
        Returns true if this task was cancelled before it completed normally.
        Specified by:
        isCancelled in interface java.util.concurrent.Future<T>
        Returns:
        true if this task was cancelled before it completed
      • isDone

        public boolean isDone()
        Returns true if this task completed.

        Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.

        Specified by:
        isDone in interface java.util.concurrent.Future<T>
        Returns:
        true if this task completed
      • get

        public T get()
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException
        Waits if necessary for the computation to complete, and then retrieves its result.
        Specified by:
        get in interface java.util.concurrent.Future<T>
        Returns:
        the computed result
        Throws:
        java.util.concurrent.CancellationException - if the computation was cancelled
        java.util.concurrent.ExecutionException - if the computation threw an exception
        java.lang.InterruptedException - if the current thread was interrupted while waiting
      • get

        public T get​(long timeout,
                     java.util.concurrent.TimeUnit unit)
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException,
                     java.util.concurrent.TimeoutException
        Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
        Specified by:
        get in interface java.util.concurrent.Future<T>
        Parameters:
        timeout - the maximum time to wait
        unit - the time unit of the timeout argument
        Returns:
        the computed result
        Throws:
        java.util.concurrent.CancellationException - if the computation was cancelled
        java.util.concurrent.ExecutionException - if the computation threw an exception
        java.lang.InterruptedException - if the current thread was interrupted while waiting
        java.util.concurrent.TimeoutException - if the wait timed out
      • getNow

        public T getNow()
        Description copied from interface: AsyncFuture
        Non-blocking variant of Future.get()
        Specified by:
        getNow in interface AsyncFuture<T>
        Returns:
        value or null
      • toCompletableFuture

        public java.util.concurrent.CompletableFuture<T> toCompletableFuture()
        Specified by:
        toCompletableFuture in interface java.util.concurrent.CompletionStage<T>