Class Futures
- java.lang.Object
-
- org.glassfish.jersey.internal.guava.Futures
-
public final class Futures extends Object
Static utility methods pertaining to theFutureinterface.Many of these methods use the
ListenableFutureAPI; consult the Guava User Guide article onListenableFuture.- Since:
- 1.0
- Author:
- Kevin Bourrillion, Nishant Thakkar, Sven Mawson
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <V> ListenableFuture<V>immediateFailedFuture(Throwable throwable)Returns aListenableFuturewhich has an exception set immediately upon construction.static <V> ListenableFuture<V>immediateFuture(V value)Creates aListenableFuturewhich has its value set immediately upon construction.static <I,O>
ListenableFuture<O>transform(ListenableFuture<I> input, Function<? super I,? extends O> function)Returns a newListenableFuturewhose result is the product of applying the givenFunctionto the result of the givenFuture.
-
-
-
Method Detail
-
immediateFuture
public static <V> ListenableFuture<V> immediateFuture(V value)
Creates aListenableFuturewhich has its value set immediately upon construction. The getters just return the value. ThisFuturecan't be canceled or timed out and itsisDone()method always returnstrue.
-
immediateFailedFuture
public static <V> ListenableFuture<V> immediateFailedFuture(Throwable throwable)
Returns aListenableFuturewhich has an exception set immediately upon construction.The returned
Futurecan't be cancelled, and itsisDone()method always returnstrue. Callingget()will immediately throw the providedThrowablewrapped in anExecutionException.
-
transform
public static <I,O> ListenableFuture<O> transform(ListenableFuture<I> input, Function<? super I,? extends O> function)
Returns a newListenableFuturewhose result is the product of applying the givenFunctionto 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,
transformwill use an inline executor, which carries some caveats for heavier operations. For example, the call tofunction.applymay run on an unpredictable or undesirable thread:- If the input
Futureis done at the timetransformis called,transformwill callfunction.applyinline. - If the input
Futureis not yet done,transformwill schedulefunction.applyto 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
Futureattempts to keep its cancellation state in sync with that of the input future. That is, if the returnedFutureis cancelled, it will attempt to cancel the input, and if the input is cancelled, the returnedFuturewill 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
-
-