public class FuturesExtra extends Object
ListenableFuture interface.| Modifier and Type | Class and Description |
|---|---|
static interface |
FuturesExtra.AsyncFunction2<Z,A,B>
Implementations of this interface is used inĀ
syncTransform2(com.google.common.util.concurrent.ListenableFuture<A>, com.google.common.util.concurrent.ListenableFuture<B>, com.spotify.futures.FuturesExtra.Function2<Z, ? super A, ? super B>) to asynchronously
transform two values into a return value. |
static interface |
FuturesExtra.AsyncFunction3<Z,A,B,C> |
static interface |
FuturesExtra.AsyncFunction4<Z,A,B,C,D> |
static interface |
FuturesExtra.AsyncFunction5<Z,A,B,C,D,E> |
static interface |
FuturesExtra.AsyncFunction6<Z,A,B,C,D,E,F> |
static interface |
FuturesExtra.Consumer<T>
Represents an operation that accepts a single input argument and returns no result.
|
static interface |
FuturesExtra.Function2<Z,A,B>
Implementations of this interface is used to synchronously transform the
input values into an output value.
|
static interface |
FuturesExtra.Function3<Z,A,B,C>
Implementations of this interface is used to synchronously transform the
input values into an output value.
|
static interface |
FuturesExtra.Function4<Z,A,B,C,D>
Implementations of this interface is used to synchronously transform the
input values into an output value.
|
static interface |
FuturesExtra.Function5<Z,A,B,C,D,E>
Implementations of this interface is used to synchronously transform the
input values into an output value.
|
static interface |
FuturesExtra.Function6<Z,A,B,C,D,E,F>
Implementations of this interface is used to synchronously transform the
input values into an output value.
|
| Constructor and Description |
|---|
FuturesExtra() |
| Modifier and Type | Method and Description |
|---|---|
static <T> void |
addCallback(com.google.common.util.concurrent.ListenableFuture<T> future,
FuturesExtra.Consumer<? super T> success,
FuturesExtra.Consumer<Throwable> failure)
A lambda-friendly way to attach callbacks to a
ListenableFuture. |
static <T> void |
addFailureCallback(com.google.common.util.concurrent.ListenableFuture<T> future,
FuturesExtra.Consumer<Throwable> consumer)
A lambda-friendly way to attach a callback to a
ListenableFuture. |
static <T> void |
addSuccessCallback(com.google.common.util.concurrent.ListenableFuture<T> future,
FuturesExtra.Consumer<? super T> consumer)
A lambda-friendly way to attach a callback to a
ListenableFuture. |
static <I,O> com.google.common.util.concurrent.ListenableFuture<O> |
asyncTransform(com.google.common.util.concurrent.ListenableFuture<I> input,
com.google.common.util.concurrent.AsyncFunction<? super I,? extends O> function) |
static <Z,A,B> com.google.common.util.concurrent.ListenableFuture<Z> |
asyncTransform2(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
FuturesExtra.AsyncFunction2<Z,? super A,? super B> function)
Transform the input futures into a single future, using the provided
transform function.
|
static <Z,A,B,C> com.google.common.util.concurrent.ListenableFuture<Z> |
asyncTransform3(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
FuturesExtra.AsyncFunction3<Z,? super A,? super B,? super C> function)
Transform the input futures into a single future, using the provided
transform function.
|
static <Z,A,B,C,D> |
asyncTransform4(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
FuturesExtra.AsyncFunction4<Z,? super A,? super B,? super C,? super D> function)
Transform the input futures into a single future, using the provided
transform function.
|
static <Z,A,B,C,D,E> |
asyncTransform5(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
com.google.common.util.concurrent.ListenableFuture<E> e,
FuturesExtra.AsyncFunction5<Z,? super A,? super B,? super C,? super D,? super E> function)
Transform the input futures into a single future, using the provided
transform function.
|
static <Z,A,B,C,D,E,F> |
asyncTransform6(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
com.google.common.util.concurrent.ListenableFuture<E> e,
com.google.common.util.concurrent.ListenableFuture<F> f,
FuturesExtra.AsyncFunction6<Z,? super A,? super B,? super C,? super D,? super E,? super F> function)
Transform the input futures into a single future, using the provided
transform function.
|
static <T> void |
checkCompleted(com.google.common.util.concurrent.ListenableFuture<T> future)
check that a future is completed.
|
static <A,B> com.google.common.util.concurrent.ListenableFuture<A> |
fastFail(com.google.common.util.concurrent.ListenableFuture<B> conditionValue,
com.google.common.util.concurrent.ListenableFuture<A> future,
Validator<B> validator)
This takes two futures of type
A and B and works like
a valve on A, with validation executed on B. |
static <T> T |
getCompleted(com.google.common.util.concurrent.ListenableFuture<T> future)
Get the value of a completed future.
|
static <T> Throwable |
getException(com.google.common.util.concurrent.ListenableFuture<T> future)
Get the exception of a completed future.
|
static com.google.common.util.concurrent.ListenableFuture<JoinedResults> |
join(List<? extends com.google.common.util.concurrent.ListenableFuture<?>> inputs)
Transform a list of futures to a future that returns a joined result of them all.
|
static com.google.common.util.concurrent.ListenableFuture<JoinedResults> |
join(com.google.common.util.concurrent.ListenableFuture<?>... inputs)
Transform a list of futures to a future that returns a joined result of them all.
|
static <T> com.google.common.util.concurrent.ListenableFuture<T> |
makeTimeoutFuture(ScheduledExecutorService scheduledExecutorService,
com.google.common.util.concurrent.ListenableFuture<T> future,
long timeout,
TimeUnit unit)
Returns a future that fails with a
TimeoutException if the parent future has not
finished before the timeout. |
static <T> com.google.common.util.concurrent.ListenableFuture<T> |
select(List<? extends com.google.common.util.concurrent.ListenableFuture<T>> futures)
Returns a new
ListenableFuture with the result of the first of futures that
successfully completes. |
static <I,O> com.google.common.util.concurrent.ListenableFuture<O> |
syncTransform(com.google.common.util.concurrent.ListenableFuture<I> input,
com.google.common.base.Function<? super I,? extends O> function) |
static <Z,A,B> com.google.common.util.concurrent.ListenableFuture<Z> |
syncTransform2(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
FuturesExtra.Function2<Z,? super A,? super B> function)
Transform the input futures into a single future, using the provided
transform function.
|
static <Z,A,B,C> com.google.common.util.concurrent.ListenableFuture<Z> |
syncTransform3(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
FuturesExtra.Function3<Z,? super A,? super B,? super C> function)
Transform the input futures into a single future, using the provided
transform function.
|
static <Z,A,B,C,D> |
syncTransform4(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
FuturesExtra.Function4<Z,? super A,? super B,? super C,? super D> function)
Transform the input futures into a single future, using the provided
transform function.
|
static <Z,A,B,C,D,E> |
syncTransform5(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
com.google.common.util.concurrent.ListenableFuture<E> e,
FuturesExtra.Function5<Z,? super A,? super B,? super C,? super D,? super E> function)
Transform the input futures into a single future, using the provided
transform function.
|
static <Z,A,B,C,D,E,F> |
syncTransform6(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
com.google.common.util.concurrent.ListenableFuture<E> e,
com.google.common.util.concurrent.ListenableFuture<F> f,
FuturesExtra.Function6<Z,? super A,? super B,? super C,? super D,? super E,? super F> function)
Transform the input futures into a single future, using the provided
transform function.
|
private static <Z> com.google.common.util.concurrent.ListenableFuture<Z> |
transform(List<? extends com.google.common.util.concurrent.ListenableFuture<?>> inputs,
com.google.common.util.concurrent.AsyncFunction<List<Object>,Z> function) |
private static <Z> com.google.common.util.concurrent.ListenableFuture<Z> |
transform(List<? extends com.google.common.util.concurrent.ListenableFuture<?>> inputs,
com.google.common.base.Function<List<Object>,Z> function) |
public static <T> com.google.common.util.concurrent.ListenableFuture<T> makeTimeoutFuture(ScheduledExecutorService scheduledExecutorService, com.google.common.util.concurrent.ListenableFuture<T> future, long timeout, TimeUnit unit)
TimeoutException if the parent future has not
finished before the timeout. The new returned future will always be executed on the provided
scheduledExecutorService, even when the parent future does not timeout.scheduledExecutorService - executor that runs the timeout code. If the future times out,
this is also the thread any callbacks will run on.future - the future to wrap as a timeout future.timeout - how long to wait before timing out a futureunit - unit of the timeoutpublic static <A,B> com.google.common.util.concurrent.ListenableFuture<A> fastFail(com.google.common.util.concurrent.ListenableFuture<B> conditionValue,
com.google.common.util.concurrent.ListenableFuture<A> future,
Validator<B> validator)
A and B and works like
a valve on A, with validation executed on B.
Returns a future with the result of A that will wait for a
condition on B to be validated first. Both futures can run in
parallel. If the condition fails validation, the A future will
be cancelled by a call to Future.cancel(boolean) with
false.
This is useful for when you want to optimistically run a time consuming
path while validating if it should be computed or not by a parallel
async computation.conditionValue - The future computing the value for validation.future - The actual value future.validator - A validator for the condition.ListenableFuture eventually either containing
or any exception thrown by .public static <T> com.google.common.util.concurrent.ListenableFuture<T> select(List<? extends com.google.common.util.concurrent.ListenableFuture<T>> futures)
ListenableFuture with the result of the first of futures that
successfully completes. If all ListenableFutures in futures fails the returned feature
fails as well with the Exception of the last failed future. If futures is an empty
list the returned future fails immediately with NoSuchElementExceptionfutures - a List of futuresNullPointerException - if the is nullpublic static <T> void addCallback(com.google.common.util.concurrent.ListenableFuture<T> future,
FuturesExtra.Consumer<? super T> success,
FuturesExtra.Consumer<Throwable> failure)
ListenableFuture. The success
callback will only be run if the future completes successfully, and failure will
only be run if the future fails.future - a ListenableFuture to attach the callbacks to.success - a consumer, to be called with the result of the successful future.failure - a consumer, to be called with the result of the failed future.NullPointerException - if the and are nullpublic static <T> void addSuccessCallback(com.google.common.util.concurrent.ListenableFuture<T> future,
FuturesExtra.Consumer<? super T> consumer)
ListenableFuture. The callback will
only be run if the future completes successfully.future - a ListenableFuture to attach the callback to.consumer - a consumer, to be called with the result of the successful future.public static <T> void addFailureCallback(com.google.common.util.concurrent.ListenableFuture<T> future,
FuturesExtra.Consumer<Throwable> consumer)
ListenableFuture. The callback will
only be run if the future fails.future - a ListenableFuture to attach the callback to.consumer - a consumer, to be called with the result of the failed future.public static <Z,A,B> com.google.common.util.concurrent.ListenableFuture<Z> syncTransform2(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
FuturesExtra.Function2<Z,? super A,? super B> function)
Futures.transform(ListenableFuture, Function) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinefunction - the implementation of the transformpublic static <Z,A,B> com.google.common.util.concurrent.ListenableFuture<Z> asyncTransform2(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
FuturesExtra.AsyncFunction2<Z,? super A,? super B> function)
Futures.transform(ListenableFuture, AsyncFunction) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinefunction - the implementation of the transformpublic static <Z,A,B,C> com.google.common.util.concurrent.ListenableFuture<Z> syncTransform3(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
FuturesExtra.Function3<Z,? super A,? super B,? super C> function)
Futures.transform(ListenableFuture, Function) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinec - a ListenableFuture to combinefunction - the implementation of the transformpublic static <Z,A,B,C> com.google.common.util.concurrent.ListenableFuture<Z> asyncTransform3(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
FuturesExtra.AsyncFunction3<Z,? super A,? super B,? super C> function)
Futures.transform(ListenableFuture, AsyncFunction) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinec - a ListenableFuture to combinefunction - the implementation of the transformpublic static <Z,A,B,C,D> com.google.common.util.concurrent.ListenableFuture<Z> syncTransform4(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
FuturesExtra.Function4<Z,? super A,? super B,? super C,? super D> function)
Futures.transform(ListenableFuture, Function) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinec - a ListenableFuture to combined - a ListenableFuture to combinefunction - the implementation of the transformpublic static <Z,A,B,C,D> com.google.common.util.concurrent.ListenableFuture<Z> asyncTransform4(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
FuturesExtra.AsyncFunction4<Z,? super A,? super B,? super C,? super D> function)
Futures.transform(ListenableFuture, AsyncFunction) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinec - a ListenableFuture to combined - a ListenableFuture to combinefunction - the implementation of the transformpublic static <Z,A,B,C,D,E> com.google.common.util.concurrent.ListenableFuture<Z> syncTransform5(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
com.google.common.util.concurrent.ListenableFuture<E> e,
FuturesExtra.Function5<Z,? super A,? super B,? super C,? super D,? super E> function)
Futures.transform(ListenableFuture, Function) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinec - a ListenableFuture to combined - a ListenableFuture to combinee - a ListenableFuture to combinefunction - the implementation of the transformpublic static <Z,A,B,C,D,E> com.google.common.util.concurrent.ListenableFuture<Z> asyncTransform5(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
com.google.common.util.concurrent.ListenableFuture<E> e,
FuturesExtra.AsyncFunction5<Z,? super A,? super B,? super C,? super D,? super E> function)
Futures.transform(ListenableFuture, AsyncFunction) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinec - a ListenableFuture to combined - a ListenableFuture to combinee - a ListenableFuture to combinefunction - the implementation of the transformpublic static <Z,A,B,C,D,E,F> com.google.common.util.concurrent.ListenableFuture<Z> syncTransform6(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
com.google.common.util.concurrent.ListenableFuture<E> e,
com.google.common.util.concurrent.ListenableFuture<F> f,
FuturesExtra.Function6<Z,? super A,? super B,? super C,? super D,? super E,? super F> function)
Futures.transform(ListenableFuture, Function) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinec - a ListenableFuture to combined - a ListenableFuture to combinee - a ListenableFuture to combinef - a ListenableFuture to combinefunction - the implementation of the transformpublic static <Z,A,B,C,D,E,F> com.google.common.util.concurrent.ListenableFuture<Z> asyncTransform6(com.google.common.util.concurrent.ListenableFuture<A> a,
com.google.common.util.concurrent.ListenableFuture<B> b,
com.google.common.util.concurrent.ListenableFuture<C> c,
com.google.common.util.concurrent.ListenableFuture<D> d,
com.google.common.util.concurrent.ListenableFuture<E> e,
com.google.common.util.concurrent.ListenableFuture<F> f,
FuturesExtra.AsyncFunction6<Z,? super A,? super B,? super C,? super D,? super E,? super F> function)
Futures.transform(ListenableFuture, AsyncFunction) and the input
futures are combined using Futures.allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...).a - a ListenableFuture to combineb - a ListenableFuture to combinec - a ListenableFuture to combined - a ListenableFuture to combinee - a ListenableFuture to combinef - a ListenableFuture to combinefunction - the implementation of the transformprivate static <Z> com.google.common.util.concurrent.ListenableFuture<Z> transform(List<? extends com.google.common.util.concurrent.ListenableFuture<?>> inputs, com.google.common.base.Function<List<Object>,Z> function)
private static <Z> com.google.common.util.concurrent.ListenableFuture<Z> transform(List<? extends com.google.common.util.concurrent.ListenableFuture<?>> inputs, com.google.common.util.concurrent.AsyncFunction<List<Object>,Z> function)
public static com.google.common.util.concurrent.ListenableFuture<JoinedResults> join(List<? extends com.google.common.util.concurrent.ListenableFuture<?>> inputs)
Transform a list of futures to a future that returns a joined result of them all. The result can be used to get the joined results and ensures no future that were not part of the join is accessed.
join(ListenableFuture...)public static com.google.common.util.concurrent.ListenableFuture<JoinedResults> join(com.google.common.util.concurrent.ListenableFuture<?>... inputs)
Transform a list of futures to a future that returns a joined result of them all. The result can be used to get the joined results and ensures no future that were not part of the join is accessed.
Example
final Future<String> first = Futures.immediateFuture("ok");
final Future<Integer> second = Futures.immediateFuture(1);
JoinedResults futures = FuturesExtra.join(first, second).get();
assertEquals("ok", futures.get(first));
assertEquals(1, futures.get(second));
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> syncTransform(com.google.common.util.concurrent.ListenableFuture<I> input,
com.google.common.base.Function<? super I,? extends O> function)
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> asyncTransform(com.google.common.util.concurrent.ListenableFuture<I> input,
com.google.common.util.concurrent.AsyncFunction<? super I,? extends O> function)
public static <T> void checkCompleted(com.google.common.util.concurrent.ListenableFuture<T> future)
future - the future.IllegalStateException - if the future is not completed.public static <T> T getCompleted(com.google.common.util.concurrent.ListenableFuture<T> future)
future - a completed future.IllegalStateException - if the future is not completed.com.google.common.util.concurrent.UncheckedExecutionException - if the future has failedpublic static <T> Throwable getException(com.google.common.util.concurrent.ListenableFuture<T> future)
future - a completed future.IllegalStateException - if the future is not completed.Copyright © 2015 Spotify AB. All Rights Reserved.