Class Futures
ListenableFuture
s.
Mostly copied after Guava's Futures
, because that one is still marked as beta
and is subject to change.
- Author:
- Guava
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> void
addCallback
(com.google.common.util.concurrent.ListenableFuture<V> future, com.google.common.util.concurrent.FutureCallback<? super V> callback) Deprecated.Registers separate success and failure callbacks to be run when theFuture
's computation is complete or, if the computation is already complete, immediately.static <V> void
addCallback
(com.google.common.util.concurrent.ListenableFuture<V> future, com.google.common.util.concurrent.FutureCallback<? super V> callback, Executor executor) Deprecated.Registers separate success and failure callbacks to be run when theFuture
's computation is complete or, if the computation is already complete, immediately.static <V> com.google.common.util.concurrent.ListenableFuture<List<V>>
allAsList
(com.google.common.util.concurrent.ListenableFuture<? extends V>... futures) Deprecated.Creates a newListenableFuture
whose value is a list containing the values of all its input futures, if all succeed.static <V> com.google.common.util.concurrent.ListenableFuture<List<V>>
allAsList
(Iterable<? extends com.google.common.util.concurrent.ListenableFuture<? extends V>> futures) Deprecated.Creates a newListenableFuture
whose value is a list containing the values of all its input futures, if all succeed.static <V> com.google.common.util.concurrent.ListenableFuture<V>
immediateFailedFuture
(Throwable throwable) Deprecated.Returns aListenableFuture
which has an exception set immediately upon construction.static <V> com.google.common.util.concurrent.ListenableFuture<V>
immediateFuture
(V value) Deprecated.Creates aListenableFuture
which has its value set immediately upon construction.static <I,
O> com.google.common.util.concurrent.ListenableFuture<O> transform
(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.base.Function<? super I, ? extends O> function) Deprecated.Returns a newListenableFuture
whose result is the product of applying the givenFunction
to the result of the givenFuture
.static <I,
O> com.google.common.util.concurrent.ListenableFuture<O> transform
(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.base.Function<? super I, ? extends O> function, Executor executor) Deprecated.Returns a newListenableFuture
whose result is the product of applying the givenFunction
to the result of the givenFuture
.static <I,
O> com.google.common.util.concurrent.ListenableFuture<O> transform
(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.util.concurrent.AsyncFunction<? super I, ? extends O> function) Deprecated.Returns a newListenableFuture
whose result is asynchronously derived from the result of the givenFuture
.static <I,
O> com.google.common.util.concurrent.ListenableFuture<O> transform
(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.util.concurrent.AsyncFunction<? super I, ? extends O> function, Executor executor) Deprecated.Returns a newListenableFuture
whose result is asynchronously derived from the result of the givenFuture
.
-
Constructor Details
-
Futures
public Futures()Deprecated.
-
-
Method Details
-
addCallback
public static <V> void addCallback(com.google.common.util.concurrent.ListenableFuture<V> future, com.google.common.util.concurrent.FutureCallback<? super V> callback) Deprecated.Registers separate success and failure callbacks to be run when theFuture
's computation is complete or, if the computation is already complete, immediately.There is no guaranteed ordering of execution of callbacks, but any callback added through this method is guaranteed to be called once the computation is complete. Example:
ListenableFuture<QueryResult> future = ...; addCallback(future, new FutureCallback<QueryResult> { public void onSuccess(QueryResult result) { storeInCache(result); } public void onFailure(Throwable t) { reportError(t); } });
addCallback
will use a direct executor, which carries some caveats for heavier operations. For example, the callback may run on an unpredictable or undesirable thread:- If the input
Future
is done at the timeaddCallback
is called,addCallback
will execute the callback inline. - If the input
Future
is not yet done,addCallback
will schedule the callback to be run by the thread that completes the inputFuture
, which may be an internal system thread such as an RPC network thread.
For a more general interface to attach a completion listener to a
Future
, seeaddListener
.- Parameters:
future
- The future attach the callback to.callback
- The callback to invoke whenfuture
is completed.- Since:
- 10.0
- If the input
-
addCallback
public static <V> void addCallback(com.google.common.util.concurrent.ListenableFuture<V> future, com.google.common.util.concurrent.FutureCallback<? super V> callback, Executor executor) Deprecated.Registers separate success and failure callbacks to be run when theFuture
's computation is complete or, if the computation is already complete, immediately.The callback is run in
executor
. There is no guaranteed ordering of execution of callbacks, but any callback added through this method is guaranteed to be called once the computation is complete. Example:ListenableFuture<QueryResult> future = ...; Executor e = ... addCallback(future, e, new FutureCallback<QueryResult> { public void onSuccess(QueryResult result) { storeInCache(result); } public void onFailure(Throwable t) { reportError(t); } });
directExecutor
. However, be aware of the caveats documented in the link above.For a more general interface to attach a completion listener to a
Future
, seeaddListener
.- Parameters:
future
- The future attach the callback to.callback
- The callback to invoke whenfuture
is completed.executor
- The executor to runcallback
when the future completes.- Since:
- 10.0
-
immediateFuture
public static <V> com.google.common.util.concurrent.ListenableFuture<V> immediateFuture(@Nullable V value) Deprecated.Creates aListenableFuture
which has its value set immediately upon construction. The getters just return the value. ThisFuture
can't be canceled or timed out and itsisDone()
method always returnstrue
. -
immediateFailedFuture
public static <V> com.google.common.util.concurrent.ListenableFuture<V> immediateFailedFuture(Throwable throwable) Deprecated.Returns aListenableFuture
which has an exception set immediately upon construction.The returned
Future
can't be cancelled, and itsisDone()
method always returnstrue
. Callingget()
will immediately throw the providedThrowable
wrapped in anExecutionException
. -
transform
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.util.concurrent.AsyncFunction<? super I, ? extends O> function) Deprecated.Returns a newListenableFuture
whose result is asynchronously derived from the result of the givenFuture
. More precisely, the returnedFuture
takes its result from aFuture
produced by applying the givenAsyncFunction
to the result of the originalFuture
. Example:ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query); AsyncFunction<RowKey, QueryResult> queryFunction = new AsyncFunction<RowKey, QueryResult>() { public ListenableFuture<QueryResult> apply(RowKey rowKey) { return dataService.read(rowKey); } }; ListenableFuture<QueryResult> queryFuture = transform(rowKeyFuture, queryFunction);
Future
is slow or heavyweight to create (whether theFuture
itself is slow or heavyweight to complete is irrelevant), consider supplying an executor. If you do not supply an executor,transform
will use a direct executor, which carries some caveats for heavier operations. For example, the call tofunction.apply
may run on an unpredictable or undesirable thread:- If the input
Future
is done at the timetransform
is called,transform
will callfunction.apply
inline. - If the input
Future
is not yet done,transform
will schedulefunction.apply
to be run by the thread that completes the inputFuture
, which may be an internal system thread such as an RPC network thread.
function.apply
, all other registered but unexecuted listeners are prevented from running during its execution, even if those listeners are to run in other executors.The returned
Future
attempts to keep its cancellation state in sync with that of the input future and that of the future returned by the function. That is, if the returnedFuture
is cancelled, it will attempt to cancel the other two, and if either of the other two is cancelled, the returnedFuture
will receive a callback in which it will attempt to cancel itself.- Parameters:
input
- The future to transformfunction
- A function to transform the result of the input future to the result of the output future- Returns:
- A future that holds result of the function (if the input succeeded) or the original input's failure (if not)
- Since:
- 11.0
- If the input
-
transform
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.util.concurrent.AsyncFunction<? super I, ? extends O> function, Executor executor) Deprecated.Returns a newListenableFuture
whose result is asynchronously derived from the result of the givenFuture
. More precisely, the returnedFuture
takes its result from aFuture
produced by applying the givenAsyncFunction
to the result of the originalFuture
. Example:ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query); AsyncFunction<RowKey, QueryResult> queryFunction = new AsyncFunction<RowKey, QueryResult>() { public ListenableFuture<QueryResult> apply(RowKey rowKey) { return dataService.read(rowKey); } }; ListenableFuture<QueryResult> queryFuture = transform(rowKeyFuture, queryFunction, executor);
The returned
Future
attempts to keep its cancellation state in sync with that of the input future and that of the future returned by the chain function. That is, if the returnedFuture
is cancelled, it will attempt to cancel the other two, and if either of the other two is cancelled, the returnedFuture
will receive a callback in which it will attempt to cancel itself.When the execution of
function.apply
is fast and lightweight (though theFuture
it returns need not meet these criteria), consider omitting the executor or explicitly specifyingdirectExecutor
. However, be aware of the caveats documented in the link above.- Parameters:
input
- The future to transformfunction
- A function to transform the result of the input future to the result of the output futureexecutor
- Executor to run the function in.- Returns:
- A future that holds result of the function (if the input succeeded) or the original input's failure (if not)
- Since:
- 11.0
-
transform
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.base.Function<? super I, ? extends O> function) Deprecated.Returns a newListenableFuture
whose result is the product of applying the givenFunction
to the result of the givenFuture
. Example:ListenableFuture<QueryResult> queryFuture = ...; Function<QueryResult, List<Row>> rowsFunction = new Function<QueryResult, List<Row>>() { public List<Row> apply(QueryResult queryResult) { return queryResult.getRows(); } }; ListenableFuture<List<Row>> rowsFuture = transform(queryFuture, rowsFunction);
transform
will use an inline executor, which carries some caveats for heavier operations. For example, the call tofunction.apply
may run on an unpredictable or undesirable thread:- If the input
Future
is done at the timetransform
is called,transform
will callfunction.apply
inline. - If the input
Future
is not yet done,transform
will schedulefunction.apply
to be run by the thread that completes the inputFuture
, which may be an internal system thread such as an RPC network thread.
function.apply
, all other registered but unexecuted listeners are prevented from running during its execution, even if those listeners are to run in other executors.The returned
Future
attempts to keep its cancellation state in sync with that of the input future. That is, if the returnedFuture
is cancelled, it will attempt to cancel the input, and if the input is cancelled, the returnedFuture
will receive a callback in which it will attempt to cancel itself.An example use of this method is to convert a serializable object returned from an RPC into a POJO.
- Parameters:
input
- The future to transformfunction
- A Function to transform the results of the provided future to the results of the returned future. This will be run in the thread that notifies input it is complete.- Returns:
- A future that holds result of the transformation.
- Since:
- 9.0 (in 1.0 as
compose
)
- If the input
-
transform
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.base.Function<? super I, ? extends O> function, Executor executor) Deprecated.Returns a newListenableFuture
whose result is the product of applying the givenFunction
to the result of the givenFuture
. Example:ListenableFuture<QueryResult> queryFuture = ...; Function<QueryResult, List<Row>> rowsFunction = new Function<QueryResult, List<Row>>() { public List<Row> apply(QueryResult queryResult) { return queryResult.getRows(); } }; ListenableFuture<List<Row>> rowsFuture = transform(queryFuture, rowsFunction, executor);
The returned
Future
attempts to keep its cancellation state in sync with that of the input future. That is, if the returnedFuture
is cancelled, it will attempt to cancel the input, and if the input is cancelled, the returnedFuture
will receive a callback in which it will attempt to cancel itself.An example use of this method is to convert a serializable object returned from an RPC into a POJO.
When the transformation is fast and lightweight, consider omitting the executor or explicitly specifying
directExecutor
. However, be aware of the caveats documented in the link above.- Parameters:
input
- The future to transformfunction
- A Function to transform the results of the provided future to the results of the returned future.executor
- Executor to run the function in.- Returns:
- A future that holds result of the transformation.
- Since:
- 9.0 (in 2.0 as
compose
)
-
allAsList
@Beta public static <V> com.google.common.util.concurrent.ListenableFuture<List<V>> allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>... futures) Deprecated.Creates a newListenableFuture
whose value is a list containing the values of all its input futures, if all succeed. If any input fails, the returned future fails.The list of results is in the same order as the input list.
Canceling this future does not cancel any of the component futures; however, if any of the provided futures fails or is canceled, this one is, too.
- Parameters:
futures
- futures to combine- Returns:
- a future that provides a list of the results of the component futures
- Since:
- 10.0
-
allAsList
@Beta public static <V> com.google.common.util.concurrent.ListenableFuture<List<V>> allAsList(Iterable<? extends com.google.common.util.concurrent.ListenableFuture<? extends V>> futures) Deprecated.Creates a newListenableFuture
whose value is a list containing the values of all its input futures, if all succeed. If any input fails, the returned future fails.The list of results is in the same order as the input list.
Canceling this future does not cancel any of the component futures; however, if any of the provided futures fails or is canceled, this one is, too.
- Parameters:
futures
- futures to combine- Returns:
- a future that provides a list of the results of the component futures
- Since:
- 10.0
-