Class Futures
Future
interface.
Many of these methods use the ListenableFuture
API; consult the
Guava User Guide article on
ListenableFuture
.
- Since:
- 1.0
- Author:
- Kevin Bourrillion, Nishant Thakkar, Sven Mawson
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> ListenableFuture<V>
immediateFailedFuture
(Throwable throwable) Returns aListenableFuture
which has an exception set immediately upon construction.static <V> ListenableFuture<V>
immediateFuture
(V value) Creates aListenableFuture
which has its value set immediately upon construction.static <I,
O> ListenableFuture<O> transform
(ListenableFuture<I> input, Function<? super I, ? extends O> function) Returns a newListenableFuture
whose result is the product of applying the givenFunction
to the result of the givenFuture
.
-
Method Details
-
immediateFuture
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
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> ListenableFuture<O> transform(ListenableFuture<I> input, Function<? super I, ? extends O> function) 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);
Note: If the transformation is slow or heavyweight, consider supplying an executor. If you do not supply an executor,
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.
Also note that, regardless of which thread executes the
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
-