T
- The result object type returned by this futurepublic class SettableListenableFuture<T> extends java.lang.Object implements ListenableFuture<T>, FutureCallback<T>
ListenableFuture.ListenerOptimizationStrategy
Constructor and Description |
---|
SettableListenableFuture()
Constructs a new
SettableListenableFuture . |
SettableListenableFuture(boolean throwIfAlreadyComplete)
Constructs a new
SettableListenableFuture . |
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 interruptThread) |
void |
clearResult()
Clears the stored result from this set future.
|
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. |
void |
handleFailure(java.lang.Throwable t)
This call defers to
setFailure(Throwable) . |
void |
handleResult(T result)
This call defers to
setResult(Object) . |
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,
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. |
boolean |
setFailure(java.lang.Throwable failure)
Call to indicate this future is done, and provide the occurred failure.
|
boolean |
setResult(T result)
Call to indicate this future is done, and provide the given result.
|
void |
setRunningThread(java.lang.Thread thread)
Optional call to set the thread internally that will be generating the result for this
future.
|
<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, listener, map, map, map, mapFailure, mapFailure, mapFailure, resultCallback, resultCallback, throwMap, throwMap, throwMap
public SettableListenableFuture()
SettableListenableFuture
. You can return this immediately and
provide a result to the object later when it is ready.
This defaults in the behavior since version 1.2.0 where if the future has completed (either
by cancel(boolean)
, setResult(Object)
, or setFailure(Throwable)
),
any additional attempts to setResult(Object)
or setFailure(Throwable)
will
result in a IllegalStateException
being thrown.
public SettableListenableFuture(boolean throwIfAlreadyComplete)
SettableListenableFuture
. You can return this immediately and
provide a result to the object later when it is ready.
This constructor allows you to control the behavior when results are attempt to be set after
the future has already completed (either by
cancel(boolean)
, setResult(Object)
, or setFailure(Throwable)
).
If true
, any additional attempts to setResult(Object)
or
setFailure(Throwable)
will result in a IllegalStateException
being thrown.
If false
, additional attempts to set a result will just be silently ignored.
throwIfAlreadyComplete
- Defines the behavior when result or failure is set on a completed futurepublic 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 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
in interface ListenableFuture<T>
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.
resultCallback
in interface ListenableFuture<T>
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.
failureCallback
in interface ListenableFuture<T>
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 void handleResult(T result)
setResult(Object)
. It is implemented so that you can construct
this, return it immediately, but only later provide this as a callback to another
ListenableFuture
implementation.
This should never be invoked by the implementor, this should only be invoked by other
ListenableFuture
's through the use of providing this to
ListenableFuture.callback(FutureCallback)
.
If this is being used to chain together ListenableFuture
's,
setResult(Object)
/setFailure(Throwable)
should never be called manually (or
an exception will occur).
handleResult
in interface FutureCallback<T>
result
- Result object to provide to the future to be returned from get()
callpublic void handleFailure(java.lang.Throwable t)
setFailure(Throwable)
. It is implemented so that you can
construct this, return it immediately, but only later provide this as a callback to another
ListenableFuture
implementation. If the parent chained future was canceled this
implementation will attempt to cancel the future as well.
This should never be invoked by the implementor, this should only be invoked by other
ListenableFuture
's through the use of providing this to
ListenableFuture.callback(FutureCallback)
.
If this is being used to chain together ListenableFuture
's,
setResult(Object)
/setFailure(Throwable)
should never be called manually (or
an exception will occur).
handleFailure
in interface FutureCallback<T>
t
- Throwable to be provided as the cause from the ExecutionException thrown from get()
callpublic boolean setResult(T result)
setFailure(Throwable)
are called.
If future has already completed and constructed with SettableListenableFuture()
or
true
provided to SettableListenableFuture(boolean)
this will throw an
IllegalStateException
. If complete but constructed with a false
this result
will be ignored.
result
- result to provide for get()
call, can be null
true
if the result was set (ie future did not complete in failure or cancel}public boolean setFailure(java.lang.Throwable failure)
setResult(Object)
are called, and only called once. If the provided
failure is null
, a new Exception
will be created so that something is always
provided in the ExecutionException
on calls to get()
.
If future has already completed and constructed with SettableListenableFuture()
or
true
provided to SettableListenableFuture(boolean)
this will throw an
IllegalStateException
. If complete but constructed with a false
this failure
result will be ignored.
failure
- Throwable that caused failure during computation.true
if the failure was set (ie future did not complete with result or cancel}public void setRunningThread(java.lang.Thread thread)
cancel(boolean)
call is invoked
with true
, we can send an interrupt to this thread.
The reference to this thread will be cleared after this future has completed (thus allowing it to be garbage collected).
thread
- Thread that is generating the result for this futurepublic boolean cancel(boolean interruptThread)
cancel
in interface java.util.concurrent.Future<T>
public boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<T>
public void clearResult()
get()
will throw an
IllegalStateException
. So it is critical that this is only called after you are sure
no future calls to get the result on this future will be attempted.
The design of this is so that if you want to chain ListenableFuture
's together, you
can clear the results of old ones after their result has been consumed. This is really only
useful in very specific instances.
public boolean isDone()
isDone
in interface java.util.concurrent.Future<T>
public 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()
;isCompletedExceptionally
in interface ListenableFuture<T>
true
if this ListenableFuture completed by a thrown Exception or was canceledpublic T get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
get
in interface java.util.concurrent.Future<T>
java.lang.InterruptedException
java.util.concurrent.ExecutionException
public T get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
get
in interface java.util.concurrent.Future<T>
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
public java.lang.Throwable getFailure() throws java.lang.InterruptedException
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
.getFailure
in interface ListenableFuture<T>
null
if completed normallyjava.lang.InterruptedException
- If the current thread was interrupted while blockingpublic java.lang.Throwable getFailure(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
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
.getFailure
in interface ListenableFuture<T>
timeout
- The maximum time to waitunit
- The time unit of the timeout argumentnull
if completed normallyjava.lang.InterruptedException
- If the current thread was interrupted while blockingjava.util.concurrent.TimeoutException
- If the timeout was reached before the future completedpublic 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 <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