T - the type of the future valuepublic interface FluentFuture<T> extends com.google.common.util.concurrent.ListenableFuture<T>, Cancellable, Awaitable
Future in
several categories:
Awaitable, giving you more API
choices for blocking until the future completes (that do not require catching ExecutionException, CancellationException, or TimeoutException).try/catch block (isSuccessful(),
getResult(), isFailed(), getFailure(), and
visit(FutureVisitor)). All of these new methods are non-blocking and are intended to
assist with implementing listeners and with writing asynchronous code. Many will throw an
IllegalStateException if invoked before the future is done.| Modifier and Type | Method and Description |
|---|---|
void |
addListener(FutureListener<? super T> listener,
Executor executor)
Adds a listener that will be called when the future completes.
|
default void |
addListener(Runnable r,
Executor executor) |
default CompletionStage<T> |
asCompletionStage()
Returns a view of this future as a
CompletionStage. |
static <T> FluentFuture<T> |
cancelledFuture()
Returns a future that has already been cancelled.
|
static <T,U extends T> |
cast(FluentFuture<U> future)
Casts a future to a super-type.
|
default <U> FluentFuture<U> |
chainTo(Callable<U> task,
Executor executor)
Chains the specified task to the completion of this future.
|
default <U> FluentFuture<U> |
chainTo(Function<? super T,? extends U> task,
Executor executor)
Chains the specified task to the completion of this future.
|
default FluentFuture<Void> |
chainTo(Runnable task,
Executor executor)
Chains the specified task to the completion of this future.
|
default <U> FluentFuture<U> |
chainTo(Runnable task,
U result,
Executor executor)
Chains the specified task to the completion of this future.
|
default <U,V> FluentFuture<V> |
combineWith(FluentFuture<U> other,
BiFunction<? super T,? super U,? extends V> function)
Combines this future with another by applying a function.
|
default <U,V,W> FluentFuture<W> |
combineWith(FluentFuture<U> other1,
FluentFuture<V> other2,
TriFunction<? super T,? super U,? super V,? extends W> function)
Combines this future with two other futures by applying a function.
|
static <T> FluentFuture<T> |
completedFuture(T value)
Returns a future that has already successfully completed with the specified value.
|
static <T> FluentFuture<T> |
deferredCancelledFuture(long delay,
TimeUnit unit)
Returns a future that will be cancelled after the given delay.
|
static <T> FluentFuture<T> |
deferredFailedFuture(Throwable failure,
long delay,
TimeUnit unit)
Returns a future that will fail with the given cause after the given delay.
|
static <T> FluentFuture<T> |
deferredFuture(T value,
long delay,
TimeUnit unit)
Returns a future that will complete successfully with the given value after the given delay.
|
static <T> FluentFuture<T> |
dereference(FluentFuture<? extends FluentFuture<? extends T>> future)
Dereferences a future future.
|
static <T> FluentFuture<T> |
failedFuture(Throwable failure)
Returns a future that has already failed due to the specified cause.
|
static <T> FluentFuture<T> |
firstOf(FluentFuture<? extends T>... futures)
Returns a future that represents the first future to complete out of the given futures.
|
static <T> FluentFuture<T> |
firstOf(Iterable<FluentFuture<? extends T>> futures)
Returns a future that represents the first future to complete out of the given collection of
futures.
|
static <T> FluentFuture<T> |
firstSuccessfulOf(FluentFuture<? extends T>... futures)
Returns a future that represents the first future to successfully complete out of the given
futures.
|
static <T> FluentFuture<T> |
firstSuccessfulOf(Iterable<FluentFuture<? extends T>> futures)
Returns a future that represents the first future to successfully complete out of the given
collection of futures.
|
default <U> FluentFuture<U> |
flatMap(Function<? super T,? extends FluentFuture<? extends U>> function)
Transforms the result of this future using the specified function.
|
static <T> FluentFuture<T> |
fromCompletionStage(CompletionStage<T> stage)
Converts the given completion stage into a
FluentFuture. |
Throwable |
getFailure()
Gets the cause of failure for a future.
|
default T |
getNow(T valueIfIncomplete)
Gets the value of the future or a given default value if the future is not yet complete.
|
T |
getResult()
Gets the result of a successful future.
|
boolean |
isFailed()
Returns true if the future is done and failed.
|
boolean |
isSuccessful()
Returns true if the future is done and was successful.
|
static <T> FluentFuture<List<T>> |
join(FluentFuture<? extends T>... futures)
Joins multiple futures into one future list.
|
static <T> FluentFuture<List<T>> |
join(Iterable<? extends FluentFuture<? extends T>> futures)
Joins a list of futures into one future list.
|
static <T> FluentFuture<T> |
makeFluent(Future<T> future)
Converts the specified future into a
FluentFuture. |
static <T> FluentScheduledFuture<T> |
makeFluent(ScheduledFuture<T> future)
Converts the specified future into a
FluentScheduledFuture. |
default <U> FluentFuture<U> |
map(Function<? super T,? extends U> function)
Transforms the result of this future using the specified function.
|
default FluentFuture<T> |
mapException(Function<Throwable,? extends Throwable> function)
Transforms any exception from this future using the specified function.
|
default <U> FluentFuture<U> |
mapFuture(Function<? super FluentFuture<T>,? extends U> function)
Transforms this future using the specified function.
|
default FluentFuture<T> |
recover(Function<Throwable,? extends T> function)
Recovers a future value by applying the specified function to any exception from this future.
|
static <T> FluentFuture<T> |
unfinishableFuture()
Returns a future that will never finish.
|
void |
visit(FutureVisitor<? super T> visitor)
Invokes applicable methods on the specified visitor, depending on the disposition of this
future.
|
default void |
visitWhenDone(FutureVisitor<? super T> visitor)
Adds a listener that visits this future using a
SameThreadExecutor when it completes. |
cancel, fromFuture, fromFutures, fromFutures, group, groupawait, await, awaitUninterruptibly, awaitUninterruptibly, fromCondition, fromFuture, fromIntrinsic, fromLatch, fromTerminatingExecutor, isDoneboolean isSuccessful()
T getResult()
IllegalStateException - if the future is not complete or was not successfulboolean isFailed()
Throwable getFailure()
IllegalArgumentException - if the future is not complete, was successful, or was
cancelledvoid addListener(FutureListener<? super T> listener, Executor executor)
SameThreadExecutor.listener - the listenerexecutor - the executor used when calling the listenerdefault void addListener(Runnable r, Executor executor)
addListener in interface com.google.common.util.concurrent.ListenableFuture<T>void visit(FutureVisitor<? super T> visitor)
visitor - the visitorIllegalStateException - if the future is not completestatic <T,U extends T> FluentFuture<T> cast(FluentFuture<U> future)
future - a futuredefault T getNow(T valueIfIncomplete)
valueIfIncomplete - the value to return if the future is not yet completeCancellationException - if the future was cancelledCompletionException - if the future finished with a failure (the cause of the thrown
exception will be the cause of the failure)default void visitWhenDone(FutureVisitor<? super T> visitor)
SameThreadExecutor when it completes.visitor - the visitor that will be called once this future completesdefault <U> FluentFuture<U> chainTo(Callable<U> task, Executor executor)
If this future is cancelled then the returned future will also be cancelled. But not vice versa, so canceling the returned future will not cause this future to be cancelled.
task - a task that should execute when this future completesexecutor - the executor used to run the taskdefault <U> FluentFuture<U> chainTo(Runnable task, U result, Executor executor)
If this future is cancelled then the returned future will also be cancelled. But not vice versa, so canceling the returned future will not cause this future to be cancelled.
task - a task that should execute when the future completesresult - the result of the task when it completesexecutor - the executor used to run the taskdefault FluentFuture<Void> chainTo(Runnable task, Executor executor)
If this future is cancelled then the returned future will also be cancelled. But not vice versa, so canceling the returned future will not cause this future to be cancelled.
task - a task that should execute when the future completesexecutor - the executor used to run the tasknull value when the specified task
completesdefault <U> FluentFuture<U> chainTo(Function<? super T,? extends U> task, Executor executor)
If this future is cancelled then the returned future will also be cancelled. But not vice versa, so canceling the returned future will not cause this future to be cancelled.
task - a task whose input is the result of the specified futureexecutor - the executor used to run the taskdefault <U> FluentFuture<U> map(Function<? super T,? extends U> function)
chainTo(Function, Executor) except that no executor is specified. The function
will be executed in the same thread that completes the future, so it should run quickly and be
safe to run from any thread. The function cannot be interrupted. The returned future
will fail if this future fails (in which case the given function is never invoked) or if the
function throws an exception.
Also unlike using chainTo(...), the returned future's cancellation status is kept
in sync with this future. So if the returned future is cancelled, this will also be cancelled
(if it is not yet done).
function - a function used to compute the transformed valuedefault <U> FluentFuture<U> flatMap(Function<? super T,? extends FluentFuture<? extends U>> function)
chainTo(Function, Executor) except that no executor is specified. The function
will be executed in the same thread that completes the future, so it should run quickly and be
safe to run from any thread. The function cannot be interrupted. The returned future
will fail if this future fails (in which case the given function is never invoked) or if the
function throws an exception.
Also unlike using chainTo(...), the returned future's cancellation status is kept
in sync with this future. So if the returned future is cancelled, this will also be cancelled
(if it is not yet done).
function - a function used to compute the transformed valuedefault <U> FluentFuture<U> mapFuture(Function<? super FluentFuture<T>,? extends U> function)
getResult(), getFailure(), or visit(FutureVisitor).
The returned future's cancellation status is kept in sync with the given future. So if the returned future is cancelled, this future is also cancelled (if it is not yet done). Similarly, if this future gets cancelled then the returned future will also be cancelled. The function will not be invoked if this future is cancelled.
function - a function used to compute the transformed valuedefault FluentFuture<T> mapException(Function<Throwable,? extends Throwable> function)
map(Function), except that the function is applied to this future's
exception if it fails, not to the future's value if it succeeds. So if this future
completes successfully then the given function is never invoked.
The returned future's cancellation status is kept in sync with the given future. So if the returned future is cancelled, this future is also cancelled (if it is not yet done). Similarly, if this future gets cancelled then the returned future will also be cancelled. The function will not be invoked if this future is cancelled.
function - a function used to compute a new value from an exceptiondefault FluentFuture<T> recover(Function<Throwable,? extends T> function)
map(Function), except that the function is applied to the future's
exception if it fails, instead of being applied to the future's value if it succeeds. If this
future completes successfully then the given function is never invoked and the returned future
completes with the same value. If this future fails, the function is invoked to compute a
value for the returned future. The returned future only fails if that function throws an
exception.
The returned future's cancellation status is kept in sync with the given future. So if the returned future is cancelled, this future is also cancelled (if it is not yet done). Similarly, if this future gets cancelled then the returned future will also be cancelled. The function will not be invoked if this future is cancelled.
function - a function used to compute the transformed exceptiondefault <U,V> FluentFuture<V> combineWith(FluentFuture<U> other, BiFunction<? super T,? super U,? extends V> function)
The returned future's cancellation status will be kept in sync with the both input futures. So if the returned future is cancelled, any unfinished input futures will also be cancelled. If either of the input futures is cancelled, the returned future will also be cancelled.
other - another futurefunction - a function that combines two input values into one resultdefault <U,V,W> FluentFuture<W> combineWith(FluentFuture<U> other1, FluentFuture<V> other2, TriFunction<? super T,? super U,? super V,? extends W> function)
The returned future's cancellation status will be kept in sync with the three input futures. So if the returned future is cancelled, any unfinished input futures will also be cancelled. If any of the input futures is cancelled, the returned future will also be cancelled.
other1 - another futureother2 - yet another futurefunction - a function that combines three input values into one resultdefault CompletionStage<T> asCompletionStage()
CompletionStage.static <T> FluentFuture<T> fromCompletionStage(CompletionStage<T> stage)
FluentFuture.stage - a completion stagestatic <T> FluentFuture<T> makeFluent(Future<T> future)
FluentFuture. If the specified future
is a FluentFuture, it is returned without any conversion. Note that if
the specified future is not yet done and is not a CompletableFuture, conversion
requires creating a new thread that simply blocks until the specified future completes,
at which time the returned fluent future is also completed (asynchronously).
The returned future's cancellation status will be kept in sync with the specified future. So if the returned future is cancelled, so too will the underlying future be cancelled, and vice versa.
future - the futurestatic <T> FluentScheduledFuture<T> makeFluent(ScheduledFuture<T> future)
FluentScheduledFuture. If the specified
future is a FluentScheduledFuture, it is returned without any conversion.
Note that if the specified future is not yet done, conversion requires creating a new thread.
The thread simply blocks until the specified future completes, at which time the returned
fluent future is also completed (asynchronously).
The returned future's cancellation status will be kept in sync with the specified future. So if the returned future is cancelled, so too will the underlying future be cancelled, and vice versa.
future - the scheduled futurestatic <T> FluentFuture<T> completedFuture(T value)
value - the future resultstatic <T> FluentFuture<T> failedFuture(Throwable failure)
failure - the cause of future failurestatic <T> FluentFuture<T> cancelledFuture()
static <T> FluentFuture<T> deferredFuture(T value, long delay, TimeUnit unit)
value - the future valuedelay - the time after which the future completesunit - the unit for the given delaystatic <T> FluentFuture<T> deferredFailedFuture(Throwable failure, long delay, TimeUnit unit)
failure - the cause of future failuredelay - the time after which the future completesunit - the unit for the given delaystatic <T> FluentFuture<T> deferredCancelledFuture(long delay, TimeUnit unit)
delay - the time after which the future is cancelledunit - the unit for the given delaystatic <T> FluentFuture<T> unfinishableFuture()
The returned future cannot be used with blocking calls, since they would never return or
always timeout. So calls to both forms of Future.get() and both forms of
Awaitable.await() all throw UnsupportedOperationException.
Additionally, the future cannot be cancelled (as that would implicitly finish it). So calls
to Future.cancel(boolean) always return false but don't actually cause the future to become
cancelled.
@SafeVarargs static <T> FluentFuture<List<T>> join(FluentFuture<? extends T>... futures)
The returned future's cancellation status will be kept in sync with the constituent futures. So if the returned future is cancelled, any unfinished constituent futures will also be cancelled. If any of the constituent futures is cancelled, the returned future will also be cancelled.
futures - the future valuesstatic <T> FluentFuture<List<T>> join(Iterable<? extends FluentFuture<? extends T>> futures)
The returned future's cancellation status will be kept in sync with the constituent futures. So if the returned future is cancelled, any unfinished constituent futures will also be cancelled. If any of the constituent futures is cancelled, the returned future will also be cancelled.
futures - the future values@SafeVarargs static <T> FluentFuture<T> firstOf(FluentFuture<? extends T>... futures)
If the returned future is cancelled, it will in turn result in all of the input futures being cancelled.
Note that this method does not block for completion of any future. It instead returns a new future that is completed upon the first given future completing.
futures - an array of futuresIllegalArgumentException - if the given array of futures is emptystatic <T> FluentFuture<T> firstOf(Iterable<FluentFuture<? extends T>> futures)
If the returned future is cancelled, it will in turn result in all of the input futures being cancelled.
Note that this method does not block for completion of any future. It instead returns a new future that is completed upon the first given future completing.
futures - a collection of futuresIllegalArgumentException - if the given collection of futures is empty@SafeVarargs static <T> FluentFuture<T> firstSuccessfulOf(FluentFuture<? extends T>... futures)
If the returned future is cancelled, it will in turn result in all of the input futures being cancelled.
Note that this method does not block for completion of any future. It instead returns a new future that is completed upon the first given future successfully completing.
futures - an array of futuresIllegalArgumentException - if the given array of futures is emptystatic <T> FluentFuture<T> firstSuccessfulOf(Iterable<FluentFuture<? extends T>> futures)
If the returned future is cancelled, it will in turn result in all of the input futures being cancelled.
Note that this method does not block for completion of any future. It instead returns a new future that is completed upon the first given future successfully completing.
futures - a collection of futuresIllegalArgumentException - if the given collection of futures is emptystatic <T> FluentFuture<T> dereference(FluentFuture<? extends FluentFuture<? extends T>> future)
The returned future's cancellation status will be kept in sync with the input futures. So if the returned future is cancelled, the input future or its value will also be cancelled. If the input future or its value is cancelled, the returned future will also be cancelled.
future - the future future