ListenableFuture that supports fluent chains of operations. For example:
ListenableFuture<Boolean> adminIsLoggedIn =
FluentFuture.from(usersDatabase.getAdminUser())
.transform(User::getId, directExecutor())
.transform(ActivityService::isLoggedIn, threadPool)
.catching(RpcException.class, e -> false, directExecutor());
When chaining together a graph of asynchronous operations, you will often find it easier to use a framework. Frameworks automate the process, often adding features like monitoring, debugging, and cancellation. Examples of frameworks include:
java.util.concurrent.CompletableFuture / java.util.concurrent.CompletionStage
Users of CompletableFuture will likely want to continue using CompletableFuture. FluentFuture is targeted at people who use ListenableFuture,
who can't use Java 8, or who want an API more focused than CompletableFuture. (If you
need to adapt between CompletableFuture and ListenableFuture, consider Future Converter.)
FluentFuture but with extra methods, we recommend declaring your
own subclass of ListenableFuture, complete with a method like from to adapt an
existing ListenableFuture, implemented atop a ForwardingListenableFuture that
forwards to that future and adds the desired methods.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv abstract static class |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public final void | addCallback(FutureCallback<? super V>
The callback to invoke when this callback, Executor Future is completed.The executor to run executor)callback when the future completes.Registers separate success and failure callbacks to be run when this |
| public final <X extends Throwable> FluentFuture | catching(Class<X>
the exception type that triggers use of exceptionType, Function<? super X, ? extends V> fallback. The exception
type is matched against the input's exception. "The input's exception" means the cause of
the ExecutionException thrown by input.get() or, if get() throws a
different kind of exception, that exception itself. To avoid hiding bugs and other
unrecoverable errors, callers should prefer more specific types, avoiding Throwable.class in particular.the fallback, Executor Function to be called if the input fails with the expected
exception type. The function's argument is the input's exception. "The input's exception"
means the cause of the ExecutionException thrown by this.get() or, if
get() throws a different kind of exception, that exception itself.the executor that runs executor)fallback if the input failsReturns a |
| public final <X extends Throwable> FluentFuture | catchingAsync(Class<X>
the exception type that triggers use of exceptionType, AsyncFunction<? super X, ? extends V> fallback. The exception
type is matched against the input's exception. "The input's exception" means the cause of
the ExecutionException thrown by this.get() or, if get() throws a
different kind of exception, that exception itself. To avoid hiding bugs and other
unrecoverable errors, callers should prefer more specific types, avoiding Throwable.class in particular.the fallback, Executor AsyncFunction to be called if the input fails with the expected
exception type. The function's argument is the input's exception. "The input's exception"
means the cause of the ExecutionException thrown by input.get() or, if
get() throws a different kind of exception, that exception itself.the executor that runs executor)fallback if the input failsReturns a |
| public static <V extends @Nullable Object> FluentFuture | |
| public static <V extends @Nullable Object> FluentFuture | |
| public final <T extends @Nullable Object> FluentFuture | Returns: A future that holds result of the transformation.A Function to transform the results of this future to the results of the
returned future. function, Executor Executor to run the function in. executor)Returns a new |
| public final <T extends @Nullable Object> FluentFuture | Returns: A future that holds result of the function (if the input succeeded) or the original input's failure (if not)A function to transform the result of this future to the result of the output
future function, Executor Executor to run the function in. executor)Returns a new |
| public final FluentFuture | withTimeout(Duration
when to time out the future timeout, ScheduledExecutorService The executor service to enforce the timeout. scheduledExecutor)Returns a future that delegates to this future but will finish early (via a |
| public final FluentFuture | withTimeout(long
when to time out the future timeout, TimeUnit the time unit of the time parameter unit, ScheduledExecutorService The executor service to enforce the timeout. scheduledExecutor)Returns a future that delegates to this future but will finish early (via a |
| FluentFuture | back to summary |
|---|---|
| pack-priv FluentFuture() | |
| addCallback | back to summary |
|---|---|
| public final void addCallback(FutureCallback<? super V> callback, Executor executor) Registers separate success and failure callbacks to be run when this The callback is run on Example:
When selecting an executor, note that For a more general interface to attach a completion listener, see This method is similar to
| |
| catching | back to summary |
|---|---|
| public final <X extends Throwable> FluentFuture Returns a Usage example:
When selecting an executor, note that This method is similar to
| |
| catchingAsync | back to summary |
|---|---|
| public final <X extends Throwable> FluentFuture Returns a Usage examples:
The fallback can also choose to propagate the original exception when desired:
When selecting an executor, note that This method is similar to
| |
| from | back to summary |
|---|---|
| public static <V extends @Nullable Object> FluentFuture Converts the given If the given | |
| from | back to summary |
|---|---|
| public static <V extends @Nullable Object> FluentFuture Deprecated no need to use this Simply returns its argument.
| |
| transform | back to summary |
|---|---|
| public final <T extends @Nullable Object> FluentFuture Returns a new
When selecting an executor, note that The returned An example use of this method is to convert a serializable object returned from an RPC into a POJO. This method is similar to
| |
| transformAsync | back to summary |
|---|---|
| public final <T extends @Nullable Object> FluentFuture Returns a new More precisely, the returned
When selecting an executor, note that The returned This method is similar to
| |
| withTimeout | back to summary |
|---|---|
| public final FluentFuture Returns a future that delegates to this future but will finish early (via a
| |
| withTimeout | back to summary |
|---|---|
| public final FluentFuture Returns a future that delegates to this future but will finish early (via a
| |
get calls exactly the implementation of AbstractFuture#get.
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public final void | addListener(Runnable
the listener to run when the computation is complete listener, Executor the executor to run the listener in executor)Overrides com. Implements com. |
| public final boolean | cancel(boolean
true if the thread
executing this task should be interrupted (if the thread is
known to the implementation); otherwise, in-progress tasks are
allowed to completeOverrides com. Implements java. |
| public final V | get()
Overrides com. Implements java. |
| public final V | get(long
the maximum time to wait timeout, TimeUnit the time unit of the timeout argument unit)Overrides com. Implements java. |
| public final boolean | isCancelled()
Overrides com. Implements java. true if this task was cancelled before it completed
normally.
|
| public final boolean | isDone()
Overrides com. Implements java. true if this task completed.
|
| TrustedFuture | back to summary |
|---|---|
| pack-priv TrustedFuture() | |
| addListener | back to summary |
|---|---|
| public final void addListener(Runnable listener, Executor executor) Overrides com. Implements com. Doc from com. Registers a listener to be run on the given executor.
The listener will run when the There is no guaranteed ordering of execution of listeners, but any listener added through this method is guaranteed to be called once the computation is complete. Exceptions thrown by a listener will be propagated up to the executor. Any exception thrown
during Note If your listener is lightweight -- and will not cause stack overflow by completing
more futures or adding more This is the most general listener interface. For common operations performed using
listeners, see Memory consistency effects: Actions in a thread prior to adding a listener happen-before its execution begins, perhaps in another thread. Guava implementations of | |
| cancel | back to summary |
|---|---|
| public final boolean cancel(boolean mayInterruptIfRunning) Overrides com. Implements java. Doc from java. Attempts to cancel execution of this task. This method has no
effect if the task is already completed or cancelled, or could
not be cancelled for some other reason. Otherwise, if this
task has not started when The return value from this method does not necessarily
indicate whether the task is now cancelled; use
| |
| get | back to summary |
|---|---|
| public final V get() throws InterruptedException, ExecutionException Overrides com. Implements java. Doc from java. Waits if necessary for the computation to complete, and then retrieves its result.
| |
| get | back to summary |
|---|---|
| public final V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Overrides com. Implements java. Doc from java. Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
| |
| isCancelled | back to summary |
|---|---|
| public final boolean isCancelled() Overrides com. Implements java. Doc from java. Returns
| |
| isDone | back to summary |
|---|---|
| public final boolean isDone() Overrides com. Implements java. Doc from java. Returns
| |