T
- The result object type returned by this futurepublic class ImmediateFailureListenableFuture<T>
extends java.lang.Object
ListenableFuture
that will immediately provide a failure
condition. Meaning listeners added will immediately be ran/executed, FutureCallback
's
will immediately get called with the throwable provided, and get()
will immediately
throw an ExecutionException
.ListenableFuture.ListenerOptimizationStrategy
Constructor and Description |
---|
ImmediateFailureListenableFuture(java.lang.Throwable failure)
Constructs a completed future with the provided failure.
|
Modifier and Type | Method and Description |
---|---|
ListenableFuture<T> |
callback(FutureCallback<? super T> callback,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimize)
Add a
FutureCallback to be called once the future has completed. |
boolean |
cancel(boolean mayInterruptIfRunning)
This has no effect in this implementation, as this future can not be canceled.
|
ListenableFuture<T> |
failureCallback(java.util.function.Consumer<java.lang.Throwable> callback,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimize)
Add a
Consumer to be called once the future has completed. |
<R> ListenableFuture<R> |
flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper)
Similar to
ListenableFuture.map(Function) , in that this will apply a mapper function once the applied
to future completes. |
<R> ListenableFuture<R> |
flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper,
java.util.concurrent.Executor executor)
Similar to
ListenableFuture.map(Function, Executor) , in that this will apply a mapper function once
the applied to future completes. |
<R> ListenableFuture<R> |
flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Similar to
ListenableFuture.map(Function, Executor) , in that this will apply a mapper function once
the applied to future completes. |
<R> ListenableFuture<R> |
flatMap(ListenableFuture<R> future)
Convenience function for mapping this future in with an existing future, ignoring the result
of this current future.
|
<TT extends java.lang.Throwable> |
flatMapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,ListenableFuture<T>> mapper)
Similar to
ListenableFuture.mapFailure(Class, Function) except that this mapper function returns a
ListenableFuture if it needs to map the Throwable / failure into a result or another
failure. |
<TT extends java.lang.Throwable> |
flatMapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,ListenableFuture<T>> mapper,
java.util.concurrent.Executor executor)
Similar to
ListenableFuture.mapFailure(Class, Function, Executor) except that this mapper function
returns a ListenableFuture if it needs to map the Throwable / failure into a result
or another failure. |
<TT extends java.lang.Throwable> |
flatMapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,ListenableFuture<T>> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Similar to
ListenableFuture.mapFailure(Class, Function, Executor, ListenerOptimizationStrategy) except
that this mapper function returns a ListenableFuture if it needs to map the Throwable
into a result or another failure. |
T |
get() |
T |
get(long timeout,
java.util.concurrent.TimeUnit unit) |
java.lang.Throwable |
getFailure()
Similar to
Future.get() except instead of providing a result, this will provide a thrown
exception if ListenableFuture.isCompletedExceptionally() returns true . |
java.lang.Throwable |
getFailure(long timeout,
java.util.concurrent.TimeUnit unit)
Similar to
Future.get(long, TimeUnit) except instead of providing a result, this will
provide a thrown exception if ListenableFuture.isCompletedExceptionally() returns true . |
java.lang.StackTraceElement[] |
getRunningStackTrace()
A best effort to return the stack trace for for the executing thread of either this future,
or a future which this depends on through the use of
ListenableFuture.map(Function) or similar
functions. |
boolean |
isCancelled() |
boolean |
isCompletedExceptionally()
Returns
true if the future is both done and has completed with an error or was
canceled. |
boolean |
isDone() |
ListenableFuture<T> |
listener(java.lang.Runnable listener)
Add a listener to be called once the future has completed.
|
ListenableFuture<T> |
listener(java.lang.Runnable listener,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimize)
Add a listener to be called once the future has completed.
|
<R> ListenableFuture<R> |
map(java.util.function.Function<? super T,? extends R> mapper)
Transform this future's result into another result by applying the provided mapper function.
|
<R> ListenableFuture<R> |
map(java.util.function.Function<? super T,? extends R> mapper,
java.util.concurrent.Executor executor)
Transform this future's result into another result by applying the provided mapper function.
|
<R> ListenableFuture<R> |
map(java.util.function.Function<? super T,? extends R> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Transform this future's result into another result by applying the provided mapper function.
|
<TT extends java.lang.Throwable> |
mapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,? extends T> mapper)
Similar to
ListenableFuture.throwMap(Function) except this mapper will only be invoked when the
future is in a failure state (from either the original computation or an earlier mapper
throwing an exception). |
<TT extends java.lang.Throwable> |
mapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,? extends T> mapper,
java.util.concurrent.Executor executor)
Similar to
ListenableFuture.throwMap(Function, Executor) except this mapper will only be invoked when
the future is in a failure state (from either the original computation or an earlier mapper
throwing an exception). |
<TT extends java.lang.Throwable> |
mapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,? extends T> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Similar to
ListenableFuture.throwMap(Function, Executor, ListenerOptimizationStrategy) except this
mapper will only be invoked when the future is in a failure state (from either the original
computation or an earlier mapper throwing an exception). |
ListenableFuture<T> |
resultCallback(java.util.function.Consumer<? super T> callback,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimize)
Add a
Consumer to be called once the future has completed. |
<R> ListenableFuture<R> |
throwMap(java.util.function.Function<? super T,? extends R> mapper)
Transform this future's result into another result by applying the provided mapper function.
|
<R> ListenableFuture<R> |
throwMap(java.util.function.Function<? super T,? extends R> mapper,
java.util.concurrent.Executor executor)
Transform this future's result into another result by applying the provided mapper function.
|
<R> ListenableFuture<R> |
throwMap(java.util.function.Function<? super T,? extends R> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Transform this future's result into another result by applying the provided mapper function.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
callback, callback, failureCallback, failureCallback, flatMap, flatMap, flatMap, flatMap, flatMapFailure, flatMapFailure, flatMapFailure, listener, map, map, map, mapFailure, mapFailure, mapFailure, resultCallback, resultCallback, throwMap, throwMap, throwMap
public ImmediateFailureListenableFuture(java.lang.Throwable failure)
null
, a
new Exception
will be created to represent it.failure
- to be the cause of the ExecutionException from get()
callspublic boolean isCompletedExceptionally()
ListenableFuture
true
if the future is both done and has completed with an error or was
canceled. If this returns true
the Throwable
responsible for the error can
be retrieved using ListenableFuture.getFailure()
;true
if this ListenableFuture completed by a thrown Exception or was canceledpublic ListenableFuture<T> callback(FutureCallback<? super T> callback, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimize)
ListenableFuture
FutureCallback
to be called once the future has completed. If the future has
already finished, this will be called immediately.
If the provided Executor
is null, the callback will execute on the thread which
computed the original future (once it is done). If the future has already completed, the
callback will execute immediately on the thread which is adding the callback.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
If you only care about the success result case please see
ListenableFuture.resultCallback(Consumer, Executor, ListenerOptimizationStrategy)
or conversely if you
only want to be invoked for failure cases please see
ListenableFuture.failureCallback(Consumer, Executor, ListenerOptimizationStrategy)
.
callback
- to be invoked when the computation is completeexecutor
- Executor
the callback should be ran on, or null
optimize
- true
to avoid listener queuing for execution if already on the desired poolthis
instance to add more callbacks or other functional operationspublic ListenableFuture<T> resultCallback(java.util.function.Consumer<? super T> callback, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimize)
ListenableFuture
Consumer
to be called once the future has completed. If the future has
already finished, this will be called immediately. Assuming the future has completed without
error, the result will be provided to the Consumer
, otherwise it will go un-invoked.
If the provided Executor
is null, the callback will execute on the thread which
computed the original future (once it is done). If the future has already completed, the
callback will execute immediately on the thread which is adding the callback.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
callback
- to be invoked when the computation is completeexecutor
- Executor
the callback should be ran on, or null
optimize
- true
to avoid listener queuing for execution if already on the desired poolthis
instance to add more callbacks or other functional operationspublic ListenableFuture<T> failureCallback(java.util.function.Consumer<java.lang.Throwable> callback, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimize)
ListenableFuture
Consumer
to be called once the future has completed. If the future has
already finished, this will be called immediately. Assuming the future has completed with an
error, the Throwable
will be provided to the Consumer
, otherwise if no error
occurred, the callback will go un-invoked.
If the provided Executor
is null, the callback will execute on the thread which
computed the original future (once it is done). If the future has already completed, the
callback will execute immediately on the thread which is adding the callback.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
callback
- to be invoked when the computation is completeexecutor
- Executor
the callback should be ran on, or null
optimize
- true
to avoid listener queuing for execution if already on the desired poolthis
instance to add more callbacks or other functional operationspublic T get() throws java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException
public T get(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException
public java.lang.Throwable getFailure()
ListenableFuture
Future.get()
except instead of providing a result, this will provide a thrown
exception if ListenableFuture.isCompletedExceptionally()
returns true
. If the future has not
completed yet this function will block until completion. If the future completed normally,
this will return null
.null
if completed normallypublic java.lang.Throwable getFailure(long timeout, java.util.concurrent.TimeUnit unit)
ListenableFuture
Future.get(long, TimeUnit)
except instead of providing a result, this will
provide a thrown exception if ListenableFuture.isCompletedExceptionally()
returns true
. If
the future has not completed yet this function will block until completion. If the future
completed normally, this will return null
.timeout
- The maximum time to waitunit
- The time unit of the timeout argumentnull
if completed normallypublic boolean isDone()
isDone
in interface java.util.concurrent.Future<T>
public ListenableFuture<T> listener(java.lang.Runnable listener)
ListenableFuture
The listener from this call will execute on the same thread the result was produced on, or on
the adding thread if the future is already complete. If the runnable has high complexity,
consider using ListenableFuture.listener(Runnable, Executor)
.
listener
in interface ListenableFuture<T>
listener
- the listener to run when the computation is completethis
instance to add more listeners or other functional operationspublic ListenableFuture<T> listener(java.lang.Runnable listener, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimize)
ListenableFuture
If the provided Executor
is null, the listener will execute on the thread which
computed the original future (once it is done). If the future has already completed, the
listener will execute immediately on the thread which is adding the listener.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
listener
in interface ListenableFuture<T>
listener
- the listener to run when the computation is completeexecutor
- Executor
the listener should be ran on, or null
optimize
- true
to avoid listener queuing for execution if already on the desired poolthis
instance to add more listeners or other functional operationspublic java.lang.StackTraceElement[] getRunningStackTrace()
ListenableFuture
ListenableFuture.map(Function)
or similar
functions. If there is no thread executing the future yet, or the future has already
completed, then this will return null
.
This is done without locking (though generating a stack trace still requires a JVM safe point), so the resulting stack trace is NOT guaranteed to be accurate. In most cases (particularly when blocking) this should be accurate though.
getRunningStackTrace
in interface ListenableFuture<T>
null
if unavailablepublic boolean cancel(boolean mayInterruptIfRunning)
cancel
in interface java.util.concurrent.Future<T>
mayInterruptIfRunning
- will be ignoredfalse
, as this future can't be canceledpublic boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<T>
public <R> ListenableFuture<R> map(java.util.function.Function<? super T,? extends R> mapper)
ListenableFuture
ExceptionUtils.handleException(Throwable)
will be invoked. If you
don't want mapped exceptions to be treated as unexpected / uncaught please see
ListenableFuture.throwMap(Function)
.
This can be easily used to chain together a series of operations, happening async (or in
calling thread if already complete) until the final result is actually needed.
ListenableFuture.map(Function, Executor)
can be used if transformation mapper is expensive and thus
async execution is absolutely required.
If the future is complete already, the function may be invoked on the invoking thread. If the future is not complete then the function will be invoked on the thread which completes the future (immediately after it completes).
If your function returns a future, consider using ListenableFuture.flatMap(Function)
as an alternative.
Example use:
public Integer countSomething(String value);
public ListenableFuture<String> lookupSomething();
ListenableFuture<Integer> count = lookupSomething().map((s) -> countSomething(s));
map
in interface ListenableFuture<T>
R
- The type for the object returned from the mappermapper
- Function to invoke in order to transform the futures resultListenableFuture
with the specified result typepublic <R> ListenableFuture<R> map(java.util.function.Function<? super T,? extends R> mapper, java.util.concurrent.Executor executor)
ListenableFuture
ExceptionUtils.handleException(Throwable)
will be invoked. If you
don't want mapped exceptions to be treated as unexpected / uncaught please see
ListenableFuture.throwMap(Function, Executor)
.
This can be easily used to chain together a series of operations, happening async until the
final result is actually needed. Once the future completes the mapper function will be invoked
on the executor (if provided). Because of that providing an executor can ensure this will
never block. If an executor is not provided then the mapper may be invoked on the calling
thread (if the future is already complete), or on the same thread which the future completes
on. If the mapper function is very fast and cheap to run then ListenableFuture.map(Function)
or
providing null
for the executor can allow more efficient operation.
If your function returns a future, consider using ListenableFuture.flatMap(Function, Executor)
as an
alternative.
map
in interface ListenableFuture<T>
R
- The type for the object returned from the mappermapper
- Function to invoke in order to transform the futures resultexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)ListenableFuture
with the specified result typepublic <R> ListenableFuture<R> map(java.util.function.Function<? super T,? extends R> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
ListenableFuture
ExceptionUtils.handleException(Throwable)
will be invoked. If you
don't want mapped exceptions to be treated as unexpected / uncaught please see
ListenableFuture.throwMap(Function, Executor, ListenerOptimizationStrategy)
.
This can be easily used to chain together a series of operations, happening async until the
final result is actually needed. Once the future completes the mapper function will be invoked
on the executor (if provided). Because of that providing an executor can ensure this will
never block. If an executor is not provided then the mapper may be invoked on the calling
thread (if the future is already complete), or on the same thread which the future completes
on. If the mapper function is very fast and cheap to run then ListenableFuture.map(Function)
or
providing null
for the executor can allow more efficient operation.
If your function returns a future, consider using ListenableFuture.flatMap(Function, Executor)
as an
alternative.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
map
in interface ListenableFuture<T>
R
- The type for the object returned from the mappermapper
- Function to invoke in order to transform the futures resultexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolListenableFuture
with the specified result typepublic <R> ListenableFuture<R> throwMap(java.util.function.Function<? super T,? extends R> mapper)
ListenableFuture
This function differs from ListenableFuture.map(Function)
only in how exceptions thrown from the
mapper
function are handled. Please see ListenableFuture.map(Function)
for a general
understanding for the behavior of this operation. This is to be used when the mapper function
throwing an exception is EXPECTED. Used to avoid treating mapping an exception as an
unexpected behavior.
Say for example you wanted to use FutureUtils.scheduleWhile()
to retry an operation
as long as it kept throwing an exception. You might put the result and exception into
a Pair
so it can be checked for a maximum number of times like this:
ListenableFuture<Pair<ResultType, RetryException>> exceptionConvertingLoop =
FutureUtils.scheduleWhile(scheduler, retryDelayMillis, true, () -> {
try {
return new Pair<>(makeResultOrThrow(), null);
} catch (RetryException e) {
// we can't throw or we will break the loop
return new Pair<>(null, e);
}
}, new Predicate<Pair<?, RetryException>>() {
private int selfRetryCount = 0;
public boolean test(Pair<?, RetryException> p) {
return p.getLeft() == null && ++selfRetryCount <= maxRetryCount;
}
});
You would then need to use ListenableFuture.throwMap(Function)
in order to convert that Pair
back into a ListenableFuture
with either a result, or the contained failure. For
example:
ListenableFuture<ResultType> resultFuture =
exceptionConvertingLoop.throwMap((p) -> {
if (p.getLeft() != null) {
return p.getLeft();
} else {
throw p.getRight();
}
});
throwMap
in interface ListenableFuture<T>
R
- The type for the object returned from the mappermapper
- Function to invoke in order to transform the futures resultListenableFuture
with the specified result typepublic <R> ListenableFuture<R> throwMap(java.util.function.Function<? super T,? extends R> mapper, java.util.concurrent.Executor executor)
ListenableFuture
This function differs from ListenableFuture.map(Function, Executor)
only in how exceptions thrown
from the mapper
function are handled. Please see ListenableFuture.map(Function, Executor)
for a general understanding for the behavior of this operation. This is to be used when the
mapper function throwing an exception is EXPECTED. Used to avoid treating mapping an
exception as an unexpected behavior. Please see ListenableFuture.throwMap(Function)
for more usage
examples.
Once the future completes the mapper function will be invoked on the executor (if provided).
Because of that providing an executor can ensure this will never block. If an executor is not
provided then the mapper may be invoked on the calling thread (if the future is already
complete), or on the same thread which the future completes on. If the mapper function is
very fast and cheap to run then ListenableFuture.throwMap(Function)
or providing null
for
the executor can allow more efficient operation.
throwMap
in interface ListenableFuture<T>
R
- The type for the object returned from the mappermapper
- Function to invoke in order to transform the futures resultexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)ListenableFuture
with the specified result typepublic <R> ListenableFuture<R> throwMap(java.util.function.Function<? super T,? extends R> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
ListenableFuture
This can be easily used to chain together a series of operations, happening async until the
final result is actually needed. Once the future completes the mapper function will be invoked
on the executor (if provided). Because of that providing an executor can ensure this will
never block. If an executor is not provided then the mapper may be invoked on the calling
thread (if the future is already complete), or on the same thread which the future completes
on. If the mapper function is very fast and cheap to run then ListenableFuture.throwMap(Function)
or
providing null
for the executor can allow more efficient operation.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
throwMap
in interface ListenableFuture<T>
R
- The type for the object returned from the mappermapper
- Function to invoke in order to transform the futures resultexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolListenableFuture
with the specified result typepublic <R> ListenableFuture<R> flatMap(ListenableFuture<R> future)
ListenableFuture
flatMap((ignored) -> future)
, and
conceptually the same as FutureUtils.makeFailurePropagatingCompleteFuture(Iterable)
with both futures being provided (though this allows us to capture the result of the provided
future).
Please be aware that flatMap(futureProducer.get())
is NOT equivalent to
flatMap((ignored) -> futureProducer.get())
. As the second version would delay starting
the future generation until this future completes. By calling into this with a future you will
be starting its execution immediately.
flatMap
in interface ListenableFuture<T>
R
- The type of result returned from the provided futurefuture
- The future to flat mpa against this oneListenableFuture
that will complete when both this and the provided future doespublic <R> ListenableFuture<R> flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper)
ListenableFuture
ListenableFuture.map(Function)
, in that this will apply a mapper function once the applied
to future completes. Once this future resolves it will provide the result into the provided
function. Unlike ListenableFuture.map(Function)
, this will then unwrap a future provided from the
function so that instead of having ListenableFuture<ListenableFuture<R>>
you can
simply extract the final value. The returned future will only resolve once the future of the
provided function completes.
If the future is complete already, the function may be invoked on the invoking thread. If the future is not complete then the function will be invoked on the thread which completes the future (immediately after it completes).
Example use:
public ListenableFuture<Integer> countSomething(String value);
public ListenableFuture<String> lookupSomething();
ListenableFuture<Integer> count = lookupSomething().flatMap((s) -> countSomething(s));
flatMap
in interface ListenableFuture<T>
R
- The type for the object contained in the future which is returned from the mappermapper
- Function to invoke in order to transform the futures resultListenableFuture
with the specified result typepublic <R> ListenableFuture<R> flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper, java.util.concurrent.Executor executor)
ListenableFuture
ListenableFuture.map(Function, Executor)
, in that this will apply a mapper function once
the applied to future completes. Once this future resolves it will provide the result into
the provided function. Unlike ListenableFuture.map(Function, Executor)
, this will then unwrap a
future provided from the function so that instead of having
ListenableFuture<ListenableFuture<R>>
you can simply extract the final value. The
returned future will only resolve once the future of the provided function completes.
Once the future completes the mapper function will be invoked on the executor (if provided).
Because of that providing an executor can ensure this will never block. If an executor is
not provided then the mapper may be invoked on the calling thread (if the future is already
complete), or on the same thread which the future completes on. If the mapper function is
very fast and cheap to run then ListenableFuture.flatMap(Function)
or providing null
for the
executor can allow more efficient operation.
flatMap
in interface ListenableFuture<T>
R
- The type for the object contained in the future which is returned from the mappermapper
- Function to invoke in order to transform the futures resultexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)ListenableFuture
with the specified result typepublic <R> ListenableFuture<R> flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
ListenableFuture
ListenableFuture.map(Function, Executor)
, in that this will apply a mapper function once
the applied to future completes. Once this future resolves it will provide the result into
the provided function. Unlike ListenableFuture.map(Function, Executor)
, this will then unwrap a
future provided from the function so that instead of having
ListenableFuture<ListenableFuture<R>>
you can simply extract the final value. The
returned future will only resolve once the future of the provided function completes.
Once the future completes the mapper function will be invoked on the executor (if provided).
Because of that providing an executor can ensure this will never block. If an executor is
not provided then the mapper may be invoked on the calling thread (if the future is already
complete), or on the same thread which the future completes on. If the mapper function is
very fast and cheap to run then ListenableFuture.flatMap(Function)
or providing null
for the
executor can allow more efficient operation.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
flatMap
in interface ListenableFuture<T>
R
- The type for the object contained in the future which is returned from the mappermapper
- Function to invoke in order to transform the futures resultexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolListenableFuture
with the specified result typepublic <TT extends java.lang.Throwable> ListenableFuture<T> mapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,? extends T> mapper)
ListenableFuture
ListenableFuture.throwMap(Function)
except this mapper will only be invoked when the
future is in a failure state (from either the original computation or an earlier mapper
throwing an exception). If this future does resolve in a failure state, and that exception
class matches the one provided here. The mapper function will then be provided that
throwable, it can then map that throwable back into a result (perhaps an Optional
),
or re-throw either the same or a different exception keep the future in a failure state. If
the future completes with a normal result, this mapper will be ignored, and the result will
be forwarded on without invoking this mapper.mapFailure
in interface ListenableFuture<T>
TT
- The type of throwable that should be handledthrowableType
- The class referencing to the type of throwable this mapper handlesmapper
- The mapper to convert a thrown exception to either a result or thrown exceptionListenableFuture
that will resolve after the mapper is consideredpublic <TT extends java.lang.Throwable> ListenableFuture<T> mapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,? extends T> mapper, java.util.concurrent.Executor executor)
ListenableFuture
ListenableFuture.throwMap(Function, Executor)
except this mapper will only be invoked when
the future is in a failure state (from either the original computation or an earlier mapper
throwing an exception). If this future does resolve in a failure state, and that exception
class matches the one provided here. The mapper function will then be provided that
throwable, it can then map that throwable back into a result (perhaps an Optional
),
or re-throw either the same or a different exception keep the future in a failure state. If
the future completes with a normal result, this mapper will be ignored, and the result will
be forwarded on without invoking this mapper.mapFailure
in interface ListenableFuture<T>
TT
- The type of throwable that should be handledthrowableType
- The class referencing to the type of throwable this mapper handlesmapper
- The mapper to convert a thrown exception to either a result or thrown exceptionexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)ListenableFuture
that will resolve after the mapper is consideredpublic <TT extends java.lang.Throwable> ListenableFuture<T> mapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,? extends T> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
ListenableFuture
ListenableFuture.throwMap(Function, Executor, ListenerOptimizationStrategy)
except this
mapper will only be invoked when the future is in a failure state (from either the original
computation or an earlier mapper throwing an exception). If this future does resolve in a
failure state, and that exception class matches the one provided here. The mapper function
will then be provided that throwable, it can then map that throwable back into a result
perhaps an Optional
), or re-throw either the same or a different exception keep the
future in a failure state. If the future completes with a normal result, this mapper will be
ignored, and the result will be forwarded on without invoking this mapper.mapFailure
in interface ListenableFuture<T>
TT
- The type of throwable that should be handledthrowableType
- The class referencing to the type of throwable this mapper handlesmapper
- The mapper to convert a thrown exception to either a result or thrown exceptionexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolListenableFuture
that will resolve after the mapper is consideredpublic <TT extends java.lang.Throwable> ListenableFuture<T> flatMapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,ListenableFuture<T>> mapper)
ListenableFuture
ListenableFuture.mapFailure(Class, Function)
except that this mapper function returns a
ListenableFuture
if it needs to map the Throwable / failure into a result or another
failure. The mapper function can return a Future that will (or may) provide a result, or it
can provide a future that will result in the same or another failure. Similar to
ListenableFuture.mapFailure(Class, Function)
the mapper can also throw an exception directly.flatMapFailure
in interface ListenableFuture<T>
TT
- The type of throwable that should be handledthrowableType
- The class referencing to the type of throwable this mapper handlesmapper
- Function to invoke in order to transform the futures resultListenableFuture
that will resolve after the mapper is consideredpublic <TT extends java.lang.Throwable> ListenableFuture<T> flatMapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,ListenableFuture<T>> mapper, java.util.concurrent.Executor executor)
ListenableFuture
ListenableFuture.mapFailure(Class, Function, Executor)
except that this mapper function
returns a ListenableFuture
if it needs to map the Throwable / failure into a result
or another failure. The mapper function can return a Future that will (or may) provide a
result, or it can provide a future that will result in the same or another failure. Similar
to ListenableFuture.mapFailure(Class, Function, Executor)
the mapper can also throw an exception
directly.flatMapFailure
in interface ListenableFuture<T>
TT
- The type of throwable that should be handledthrowableType
- The class referencing to the type of throwable this mapper handlesmapper
- Function to invoke in order to transform the futures resultexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)ListenableFuture
that will resolve after the mapper is consideredpublic <TT extends java.lang.Throwable> ListenableFuture<T> flatMapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,ListenableFuture<T>> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
ListenableFuture
ListenableFuture.mapFailure(Class, Function, Executor, ListenerOptimizationStrategy)
except
that this mapper function returns a ListenableFuture
if it needs to map the Throwable
into a result or another failure. The mapper function can return a Future that will (or may)
provide a result, or it can provide a future that will result in the same or another failure.
Similar to ListenableFuture.mapFailure(Class, Function, Executor, ListenerOptimizationStrategy)
the
mapper can also throw an exception directly.flatMapFailure
in interface ListenableFuture<T>
TT
- The type of throwable that should be handledthrowableType
- The class referencing to the type of throwable this mapper handlesmapper
- Function to invoke in order to transform the futures resultexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolListenableFuture
that will resolve after the mapper is considered