|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.common.util.concurrent.Futures
@Beta public class Futures
Static utility methods pertaining to the Future
interface.
Method Summary | ||
---|---|---|
static
|
chain(ListenableFuture<I> input,
Function<? super I,? extends ListenableFuture<? extends O>> function)
Creates a new ListenableFuture that wraps another
ListenableFuture . |
|
static
|
chain(ListenableFuture<I> input,
Function<? super I,? extends ListenableFuture<? extends O>> function,
Executor exec)
Creates a new ListenableFuture that wraps another
ListenableFuture . |
|
static
|
compose(Future<I> future,
Function<? super I,? extends O> function)
Creates a new Future that wraps another Future . |
|
static
|
compose(ListenableFuture<I> future,
Function<? super I,? extends O> function)
Creates a new ListenableFuture that wraps another
ListenableFuture . |
|
static
|
compose(ListenableFuture<I> future,
Function<? super I,? extends O> function,
Executor exec)
Creates a new ListenableFuture that wraps another
ListenableFuture . |
|
static
|
immediateCheckedFuture(T value)
Creates a CheckedFuture which has its value set immediately upon
construction. |
|
static
|
immediateFailedCheckedFuture(E exception)
Creates a CheckedFuture which has an exception set immediately
upon construction. |
|
static
|
immediateFailedFuture(Throwable throwable)
Creates a ListenableFuture which has an exception set immediately
upon construction. |
|
static
|
immediateFuture(T value)
Creates a ListenableFuture which has its value set immediately upon
construction. |
|
static
|
makeChecked(Future<T> future,
Function<Exception,E> mapper)
Creates a CheckedFuture out of a normal Future and a
Function that maps from Exception instances into the
appropriate checked type. |
|
static
|
makeListenable(Future<T> future)
Creates a ListenableFuture out of a normal Future . |
|
static
|
makeUninterruptible(Future<V> future)
Returns an uninterruptible view of a Future . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static <V> UninterruptibleFuture<V> makeUninterruptible(Future<V> future)
Future
. If a thread is
interrupted during an attempt to get()
from the returned future, it
continues to wait on the result until it is available or the timeout
elapses, and only then re-interrupts the thread.
public static <T> ListenableFuture<T> makeListenable(Future<T> future)
ListenableFuture
out of a normal Future
. The
returned future will create a thread to wait for the source future to
complete before executing the listeners.
Callers who have a future that subclasses
FutureTask
may want to instead subclass
ListenableFutureTask
, which adds the ListenableFuture
functionality to the standard FutureTask
implementation.
public static <T,E extends Exception> CheckedFuture<T,E> makeChecked(Future<T> future, Function<Exception,E> mapper)
CheckedFuture
out of a normal Future
and a
Function
that maps from Exception
instances into the
appropriate checked type.
The given mapping function will be applied to an
InterruptedException
, a CancellationException
, or an
ExecutionException
with the actual cause of the exception.
See Future.get()
for details on the exceptions thrown.
public static <T> ListenableFuture<T> immediateFuture(@Nullable T value)
ListenableFuture
which has its value set immediately upon
construction. The getters just return the value. This Future
can't
be canceled or timed out and its isDone()
method always returns
true
. It's useful for returning something that implements the
ListenableFuture
interface but already has the result.
public static <T,E extends Exception> CheckedFuture<T,E> immediateCheckedFuture(@Nullable T value)
CheckedFuture
which has its value set immediately upon
construction. The getters just return the value. This Future
can't
be canceled or timed out and its isDone()
method always returns
true
. It's useful for returning something that implements the
CheckedFuture
interface but already has the result.
public static <T> ListenableFuture<T> immediateFailedFuture(Throwable throwable)
ListenableFuture
which has an exception set immediately
upon construction. The getters just return the value. This Future
can't be canceled or timed out and its isDone()
method always
returns true
. It's useful for returning something that implements
the ListenableFuture
interface but already has a failed
result. Calling get()
will throw the provided Throwable
(wrapped in an ExecutionException
).
Error
- if the throwable was an Error
.public static <T,E extends Exception> CheckedFuture<T,E> immediateFailedCheckedFuture(E exception)
CheckedFuture
which has an exception set immediately
upon construction. The getters just return the value. This Future
can't be canceled or timed out and its isDone()
method always
returns true
. It's useful for returning something that implements
the CheckedFuture
interface but already has a failed result.
Calling get()
will throw the provided Throwable
(wrapped in
an ExecutionException
) and calling checkedGet()
will throw
the provided exception itself.
Error
- if the throwable was an Error
.public static <I,O> ListenableFuture<O> chain(ListenableFuture<I> input, Function<? super I,? extends ListenableFuture<? extends O>> function)
ListenableFuture
that wraps another
ListenableFuture
. The result of the new future is the result of
the provided function called on the result of the provided future.
The resulting future doesn't interrupt when aborted.
TODO: Add a version that accepts a normal Future
The typical use for this method would be when a RPC call is dependent on
the results of another RPC. One would call the first RPC (input), create a
function that calls another RPC based on input's result, and then call
chain on input and that function to get a ListenableFuture
of
the result.
input
- The future to chainfunction
- A function to chain 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.
public static <I,O> ListenableFuture<O> chain(ListenableFuture<I> input, Function<? super I,? extends ListenableFuture<? extends O>> function, Executor exec)
ListenableFuture
that wraps another
ListenableFuture
. The result of the new future is the result of
the provided function called on the result of the provided future.
The resulting future doesn't interrupt when aborted.
This version allows an arbitrary executor to be passed in for running
the chained Function. When using MoreExecutors.sameThreadExecutor()
,
the thread chained Function executes in will be whichever thread set the
result of the input Future, which may be the network thread in the case of
RPC-based Futures.
input
- The future to chainfunction
- A function to chain the results of the provided future
to the results of the returned future.exec
- Executor to run the function in.
public static <I,O> ListenableFuture<O> compose(ListenableFuture<I> future, Function<? super I,? extends O> function)
ListenableFuture
that wraps another
ListenableFuture
. The result of the new future is the result of
the provided function called on the result of the provided future.
The resulting future doesn't interrupt when aborted.
An example use of this method is to convert a serializable object returned from an RPC into a POJO.
future
- The future to composefunction
- A Function to compose 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.
public static <I,O> ListenableFuture<O> compose(ListenableFuture<I> future, Function<? super I,? extends O> function, Executor exec)
ListenableFuture
that wraps another
ListenableFuture
. The result of the new future is the result of
the provided function called on the result of the provided future.
The resulting future doesn't interrupt when aborted.
An example use of this method is to convert a serializable object returned from an RPC into a POJO.
This version allows an arbitrary executor to be passed in for running
the chained Function. When using MoreExecutors.sameThreadExecutor()
,
the thread chained Function executes in will be whichever thread set the
result of the input Future, which may be the network thread in the case of
RPC-based Futures.
future
- The future to composefunction
- A Function to compose the results of the provided future
to the results of the returned future.exec
- Executor to run the function in.
public static <I,O> Future<O> compose(Future<I> future, Function<? super I,? extends O> function)
Future
that wraps another Future
.
The result of the new future is the result of the provided function called
on the result of the provided future.
An example use of this method is to convert a Future that produces a handle to an object to a future that produces the object itself.
Each call to Future<O>.get(*)
results in a call to
Future<I>.get(*)
, but function
is only applied once, so it
is assumed that Future<I>.get(*)
is idempotent.
When calling Future.get(long, TimeUnit)
on the returned
future, the timeout only applies to the future passed in to this method.
Any additional time taken by applying function
is not considered.
future
- The future to composefunction
- A Function to compose the results of the provided future
to the results of the returned future. This will be run in the thread
that calls one of the varieties of get()
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |