Class SuccessAsyncFuture<T>
java.lang.Object
com.linkedin.alpini.base.concurrency.impl.AbstractAsyncFuture<T>
com.linkedin.alpini.base.concurrency.impl.SuccessAsyncFuture<T>
- All Implemented Interfaces:
AsyncFuture<T>,AsyncPromise<T>,CompletionStage<T>,Future<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.-
Nested Class Summary
Nested classes/interfaces inherited from interface com.linkedin.alpini.base.concurrency.AsyncFuture
AsyncFuture.Status -
Field Summary
Fields inherited from interface com.linkedin.alpini.base.concurrency.AsyncFuture
NULL_SUCCESS -
Constructor Summary
ConstructorsConstructorDescriptionSuccessAsyncFuture(T value) Construct an AsyncFuture which is already in success state. -
Method Summary
Modifier and TypeMethodDescriptionaddListener(AsyncFutureListener<T> listener) Adds the specified listener to this future.addListener(AsyncPromise<T> listener) Adds the specified future as a listener to this future.await()Waits for this future to be completed.booleanWaits for this future to be completed within the specified time limit.Waits for this future to be completed without interruption.booleanawaitUninterruptibly(long timeout, TimeUnit unit) Waits for this future to be completed within the specified time limit without interruption.booleancancel(boolean mayInterruptIfRunning) Attempts to cancel execution of this task.get()Waits if necessary for the computation to complete, and then retrieves its result.Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.getCause()Returns the cause of the failed I/O operation if the I/O operation has failed.getNow()Non-blocking variant ofFuture.get()booleanReturns true if this task was cancelled before it completed normally.booleanisDone()Returns true if this task completed.booleanReturnstrueif and only if the I/O operation was completed successfully.booleansetFailure(Throwable cause) Marks this future as a failure and notifies all listeners.booleansetSuccess(T result) Marks this future as a success and notifies all listeners.Methods inherited from class com.linkedin.alpini.base.concurrency.impl.AbstractAsyncFuture
acceptEither, acceptEitherAsync, acceptEitherAsync, applyToEither, applyToEitherAsync, applyToEitherAsync, exceptionally, handle, handleAsync, handleAsync, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, whenComplete, whenCompleteAsync, whenCompleteAsyncMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.linkedin.alpini.base.concurrency.AsyncPromise
setCompleteMethods inherited from interface java.util.concurrent.CompletionStage
acceptEither, acceptEitherAsync, acceptEitherAsync, applyToEither, applyToEitherAsync, applyToEitherAsync, exceptionally, exceptionallyAsync, exceptionallyAsync, exceptionallyCompose, exceptionallyComposeAsync, exceptionallyComposeAsync, handle, handleAsync, handleAsync, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, whenComplete, whenCompleteAsync, whenCompleteAsync
-
Constructor Details
-
SuccessAsyncFuture
Construct an AsyncFuture which is already in success state.- Parameters:
value- Value to be returned by get().
-
-
Method Details
-
isSuccess
public boolean isSuccess()Returnstrueif and only if the I/O operation was completed successfully.- Specified by:
isSuccessin interfaceAsyncFuture<T>
-
getCause
Returns the cause of the failed I/O operation if the I/O operation has failed.- Specified by:
getCausein interfaceAsyncFuture<T>- Returns:
- the cause of the failure.
nullif succeeded or this future is not completed yet.
-
setSuccess
Marks this future as a success and notifies all listeners.- Specified by:
setSuccessin interfaceAsyncPromise<T>- Returns:
trueif and only if successfully marked this future as a success. Otherwisefalsebecause this future is already marked as either a success or a failure.
-
setFailure
Marks this future as a failure and notifies all listeners.- Specified by:
setFailurein interfaceAsyncPromise<T>- Returns:
trueif and only if successfully marked this future as a failure. Otherwisefalsebecause this future is already marked as either a success or a failure.
-
addListener
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:
addListenerin interfaceAsyncFuture<T>- Specified by:
addListenerin interfaceAsyncPromise<T>- Returns:
thisto permit chaining of operations.
-
addListener
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:
addListenerin interfaceAsyncFuture<T>- Specified by:
addListenerin interfaceAsyncPromise<T>- Parameters:
listener-- Returns:
thisto permit chaining of operations.
-
await
Waits for this future to be completed.- Specified by:
awaitin interfaceAsyncFuture<T>- Throws:
InterruptedException- if the current thread was interrupted
-
awaitUninterruptibly
Waits for this future to be completed without interruption. This method catches anInterruptedExceptionand discards it silently.- Specified by:
awaitUninterruptiblyin interfaceAsyncFuture<T>
-
await
Waits for this future to be completed within the specified time limit.- Specified by:
awaitin interfaceAsyncFuture<T>- Returns:
trueif and only if the future was completed within the specified time limit- Throws:
InterruptedException- if the current thread was interrupted
-
awaitUninterruptibly
Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedExceptionand discards it silently.- Specified by:
awaitUninterruptiblyin interfaceAsyncFuture<T>- Returns:
trueif 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 toisCancelled()will always return true if this method returned true.- Specified by:
cancelin interfaceFuture<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:
isCancelledin interfaceFuture<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. -
get
Waits if necessary for the computation to complete, and then retrieves its result.- Specified by:
getin interfaceFuture<T>- Returns:
- the computed result
- Throws:
CancellationException- if the computation was cancelledExecutionException- if the computation threw an exceptionInterruptedException- if the current thread was interrupted while waiting
-
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.- Specified by:
getin interfaceFuture<T>- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the computed result
- Throws:
CancellationException- if the computation was cancelledExecutionException- if the computation threw an exceptionInterruptedException- if the current thread was interrupted while waitingTimeoutException- if the wait timed out
-
getNow
Description copied from interface:AsyncFutureNon-blocking variant ofFuture.get()- Specified by:
getNowin interfaceAsyncFuture<T>- Returns:
- value or
null
-
toCompletableFuture
- Specified by:
toCompletableFuturein interfaceCompletionStage<T>
-