public class AsyncUtil
extends java.lang.Object
CompletableFutures.| Modifier and Type | Field and Description |
|---|---|
static java.util.concurrent.CompletableFuture<java.lang.Void> |
DONE
A completed future of type
Void. |
static java.util.concurrent.CompletableFuture<java.lang.Boolean> |
READY_FALSE
A completed future of type
Boolean that is set to false. |
static java.util.concurrent.CompletableFuture<java.lang.Boolean> |
READY_TRUE
A completed future of type
Boolean that is set to true. |
| Modifier and Type | Method and Description |
|---|---|
static <I,O> java.util.concurrent.CompletableFuture<O> |
applySafely(java.util.function.Function<I,? extends java.util.concurrent.CompletableFuture<O>> func,
I value)
Run
Function func, returning all caught exceptions as a
CompletableFuture in an error state. |
static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> |
collect(AsyncIterable<V> iterable)
Iterates over a stream of items and returns the result as a list.
|
static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> |
collect(AsyncIterable<V> iterable,
java.util.concurrent.Executor executor)
Iterates over a set of items and returns the result as a list.
|
static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> |
collectRemaining(AsyncIterator<V> iterator)
Iterates over a set of items and returns the remaining results as a list.
|
static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> |
collectRemaining(AsyncIterator<V> iterator,
java.util.concurrent.Executor executor)
Iterates over a set of items and returns the remaining results as a list.
|
static <V> java.util.concurrent.CompletableFuture<V> |
composeExceptionally(java.util.concurrent.CompletableFuture<V> task,
java.util.function.Function<java.lang.Throwable,java.util.concurrent.CompletableFuture<V>> fn)
Composes an asynchronous task with an exception-handler that returns a
CompletableFuture
of the same type. |
static <V,T> java.util.concurrent.CompletableFuture<T> |
composeHandle(java.util.concurrent.CompletableFuture<V> future,
java.util.function.BiFunction<V,java.lang.Throwable,? extends java.util.concurrent.CompletableFuture<T>> handler)
Compose a handler bi-function to the result of a future.
|
static <V,T> java.util.concurrent.CompletableFuture<T> |
composeHandleAsync(java.util.concurrent.CompletableFuture<V> future,
java.util.function.BiFunction<V,java.lang.Throwable,? extends java.util.concurrent.CompletableFuture<T>> handler)
Compose a handler bi-function to the result of a future.
|
static <V,T> java.util.concurrent.CompletableFuture<T> |
composeHandleAsync(java.util.concurrent.CompletableFuture<V> future,
java.util.function.BiFunction<V,java.lang.Throwable,? extends java.util.concurrent.CompletableFuture<T>> handler,
java.util.concurrent.Executor executor)
Compose a handler bi-function to the result of a future.
|
static <V> java.util.concurrent.CompletableFuture<java.lang.Void> |
forEach(AsyncIterable<V> iterable,
java.util.function.Consumer<? super V> consumer)
Run the
consumer on each element of the iterable in order. |
static <V> java.util.concurrent.CompletableFuture<java.lang.Void> |
forEach(AsyncIterable<V> iterable,
java.util.function.Consumer<? super V> consumer,
java.util.concurrent.Executor executor)
Run the
consumer on each element of the iterable in order. |
static <V> java.util.concurrent.CompletableFuture<java.lang.Void> |
forEachRemaining(AsyncIterator<V> iterator,
java.util.function.Consumer<? super V> consumer)
Run the
consumer on each element remaining in the iterator in order. |
static <V> java.util.concurrent.CompletableFuture<java.lang.Void> |
forEachRemaining(AsyncIterator<V> iterator,
java.util.function.Consumer<? super V> consumer,
java.util.concurrent.Executor executor)
Run the
consumer on each element remaining if the iterator in order. |
static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> |
getAll(java.util.Collection<java.util.concurrent.CompletableFuture<V>> tasks)
Collects the results of many asynchronous processes into one asynchronous output.
|
static <V,T> AsyncIterable<T> |
mapIterable(AsyncIterable<V> iterable,
java.util.function.Function<V,T> func)
Map an
AsyncIterable into an AsyncIterable of another type or with
each element modified in some fashion. |
static <V,T> AsyncIterator<T> |
mapIterator(AsyncIterator<V> iterator,
java.util.function.Function<V,T> func)
Map an
AsyncIterator into an AsyncIterator of another type or with
each element modified in some fashion. |
static <V,T> CloseableAsyncIterator<T> |
mapIterator(CloseableAsyncIterator<V> iterator,
java.util.function.Function<V,T> func)
Map a
CloseableAsyncIterator into a CloseableAsyncIterator of another type or with
each element modified in some fashion. |
static <V> java.util.concurrent.CompletableFuture<java.lang.Void> |
success(java.util.concurrent.CompletableFuture<V> task)
Maps the outcome of a task into a completion signal.
|
static <V,T> java.util.concurrent.CompletableFuture<V> |
tag(java.util.concurrent.CompletableFuture<T> task,
V value)
Replaces the output of an asynchronous task with a predetermined value.
|
static <V> java.util.concurrent.CompletableFuture<java.lang.Void> |
whenAll(java.util.Collection<? extends java.util.concurrent.CompletableFuture<V>> input)
Return a
CompletableFuture that will be set when all the CompletableFuture
inputs are done. |
static <V> java.util.concurrent.CompletableFuture<java.lang.Void> |
whenAny(java.util.Collection<? extends java.util.concurrent.CompletableFuture<V>> input)
Return a
CompletableFuture that will be set when any of the CompletableFuture
inputs are done. |
static <V> java.util.concurrent.CompletableFuture<java.lang.Void> |
whenReady(java.util.concurrent.CompletableFuture<V> task)
Maps the readiness of a
CompletableFuture into a completion signal. |
static java.util.concurrent.CompletableFuture<java.lang.Void> |
whileTrue(java.util.function.Function<java.lang.Void,? extends java.util.concurrent.CompletableFuture<java.lang.Boolean>> body)
Deprecated.
Since version 5.1.0. Use the version of
whileTrue that takes a
Supplier instead. |
static java.util.concurrent.CompletableFuture<java.lang.Void> |
whileTrue(java.util.function.Function<java.lang.Void,? extends java.util.concurrent.CompletableFuture<java.lang.Boolean>> body,
java.util.concurrent.Executor executor)
Deprecated.
Since version 5.1.0. Use the version of
whileTrue that takes a
Supplier instead. |
static java.util.concurrent.CompletableFuture<java.lang.Void> |
whileTrue(java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Boolean>> body)
Executes an asynchronous operation repeatedly until it returns
False. |
static java.util.concurrent.CompletableFuture<java.lang.Void> |
whileTrue(java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Boolean>> body,
java.util.concurrent.Executor executor)
Executes an asynchronous operation repeatedly until it returns
False. |
public static final java.util.concurrent.CompletableFuture<java.lang.Void> DONE
Void. In particular, it is completed to null,
but that shouldn't really matter for the Void type. This can be used instead
of creating a new future if one wants to signal that some asynchronous task has
already been completed.public static final java.util.concurrent.CompletableFuture<java.lang.Boolean> READY_TRUE
Boolean that is set to true. This can be
used instead of creating a new future if one wants to signal that some task has
already been completed with a true result.public static final java.util.concurrent.CompletableFuture<java.lang.Boolean> READY_FALSE
Boolean that is set to false. This can be
used instead of creating a new future if one wants to signal that some task has
already been completed with a false result.public static <I,O> java.util.concurrent.CompletableFuture<O> applySafely(java.util.function.Function<I,? extends java.util.concurrent.CompletableFuture<O>> func,
I value)
Function func, returning all caught exceptions as a
CompletableFuture in an error state.I - type of input to funcO - type of output of funcfunc - the Function to runvalue - the input to pass to funcfunc, or a CompletableFuture carrying any exception
caught in the process.public static <V> java.util.concurrent.CompletableFuture<java.lang.Void> forEach(AsyncIterable<V> iterable, java.util.function.Consumer<? super V> consumer)
consumer on each element of the iterable in order. The future will
complete with either the first error encountered by either the iterable itself
or by the consumer provided or with null if the future completes
successfully. Items are processed in order from the iterable, and each item
will be processed only after the item before it has finished processing.V - type of the items returned by the iterableiterable - the source of data over from which to consumeconsumer - operation to apply to each itempublic static <V> java.util.concurrent.CompletableFuture<java.lang.Void> forEach(AsyncIterable<V> iterable, java.util.function.Consumer<? super V> consumer, java.util.concurrent.Executor executor)
consumer on each element of the iterable in order. The future will
complete with either the first error encountered by either the iterable itself
or by the consumer provided or with null if the future completes
successfully. Items are processed in order from the iterable, and each item
will be processed only after the item before it has finished processing. Asynchronous
tasks needed to complete this operation are scheduled on the provided executor.V - type of the items returned by the iterableiterable - the source of data over from which to consumeconsumer - operation to apply to each itemexecutor - executor on which to schedule asynchronous taskspublic static <V> java.util.concurrent.CompletableFuture<java.lang.Void> forEachRemaining(AsyncIterator<V> iterator, java.util.function.Consumer<? super V> consumer)
consumer on each element remaining in the iterator in order. The future will
complete with either the first error encountered by either the iterator itself
or by the consumer provided or with null if the future completes
successfully. Items are processed in order from the iterator, and each item
will be processed only after the item before it has finished processing.V - type of the items returned by the iteratoriterator - the source of data over from which to consumeconsumer - operation to apply to each itempublic static <V> java.util.concurrent.CompletableFuture<java.lang.Void> forEachRemaining(AsyncIterator<V> iterator, java.util.function.Consumer<? super V> consumer, java.util.concurrent.Executor executor)
consumer on each element remaining if the iterator in order. The future will
complete with either the first error encountered by either the iterator itself
or by the consumer provided or with null if the future completes
successfully. Items are processed in order from the iterator, and each item
will be processed only after the item before it has finished processing. Asynchronous
tasks needed to complete this operation are scheduled on the provided executor.V - type of the items returned by the iteratoriterator - the source of data over from which to consumeconsumer - operation to apply to each itemexecutor - executor on which to schedule asynchronous taskspublic static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> collect(AsyncIterable<V> iterable)
V - type of the items returned by the iterableiterable - the source of data over which to iterateCompletableFuture which will be set to the amalgamation of results
from iteration.public static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> collectRemaining(AsyncIterator<V> iterator)
V - type of the items returned by the iteratoriterator - the source of data over which to iterate. This function will exhaust the iterator.CompletableFuture which will be set to the amalgamation of results
from iteration.public static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> collect(AsyncIterable<V> iterable, java.util.concurrent.Executor executor)
V - type of the items returned by the iterableiterable - the source of data over which to iterateexecutor - the Executor to use for asynchronous operationsCompletableFuture which will be set to the amalgamation of results
from iteration.public static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> collectRemaining(AsyncIterator<V> iterator, java.util.concurrent.Executor executor)
V - type of the items returned by the iteratoriterator - the source of data over which to iterate. This function will exhaust the iterator.executor - the Executor to use for asynchronous operationsCompletableFuture which will be set to the amalgamation of results
from iteration.public static <V,T> AsyncIterable<T> mapIterable(AsyncIterable<V> iterable, java.util.function.Function<V,T> func)
AsyncIterable into an AsyncIterable of another type or with
each element modified in some fashion.V - type of the items returned by the original iterableT - type of the items returned by the final iterableiterable - inputfunc - mapping function applied to each elementpublic static <V,T> AsyncIterator<T> mapIterator(AsyncIterator<V> iterator, java.util.function.Function<V,T> func)
AsyncIterator into an AsyncIterator of another type or with
each element modified in some fashion.V - type of the items returned by the original iteratorT - type of the items returned by the final iteratoriterator - inputfunc - mapping function applied to each elementpublic static <V,T> CloseableAsyncIterator<T> mapIterator(CloseableAsyncIterator<V> iterator, java.util.function.Function<V,T> func)
CloseableAsyncIterator into a CloseableAsyncIterator of another type or with
each element modified in some fashion.V - type of the items returned by the original iteratorT - type of the items returned by the final iteratoriterator - inputfunc - mapping function applied to each element@Deprecated public static java.util.concurrent.CompletableFuture<java.lang.Void> whileTrue(java.util.function.Function<java.lang.Void,? extends java.util.concurrent.CompletableFuture<java.lang.Boolean>> body)
whileTrue that takes a
Supplier instead.False.body - the asynchronous operation over which to loopCompletableFuture which will be set at completion of the loop.@Deprecated
public static java.util.concurrent.CompletableFuture<java.lang.Void> whileTrue(java.util.function.Function<java.lang.Void,? extends java.util.concurrent.CompletableFuture<java.lang.Boolean>> body,
java.util.concurrent.Executor executor)
whileTrue that takes a
Supplier instead.False.body - the asynchronous operation over which to loopexecutor - the Executor to use for asynchronous operationsCompletableFuture which will be set at completion of the loop.public static java.util.concurrent.CompletableFuture<java.lang.Void> whileTrue(java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Boolean>> body)
False.body - the asynchronous operation over which to loopCompletableFuture which will be set at completion of the loop.public static java.util.concurrent.CompletableFuture<java.lang.Void> whileTrue(java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Boolean>> body,
java.util.concurrent.Executor executor)
False.body - the asynchronous operation over which to loopexecutor - the Executor to use for asynchronous operationsCompletableFuture which will be set at completion of the loop.public static <V> java.util.concurrent.CompletableFuture<java.lang.Void> success(java.util.concurrent.CompletableFuture<V> task)
task has
side-effects for which all is needed is a signal of completion.
All errors from task will be passed to the resulting CompletableFuture.V - type of element returned by tasktask - the asynchronous process for which to signal completionCompletableFuture that is set when task completespublic static <V> java.util.concurrent.CompletableFuture<java.lang.Void> whenReady(java.util.concurrent.CompletableFuture<V> task)
CompletableFuture into a completion signal. When
the given CompletableFuture is set to a value or an error, the returned CompletableFuture
will be set to null. The returned CompletableFuture will never be set to an error unless
it is explicitly cancelled.V - return type of the asynchronous tasktask - the asynchronous process to monitor the readiness ofCompletableFuture that is set when task is ready.public static <V> java.util.concurrent.CompletableFuture<V> composeExceptionally(java.util.concurrent.CompletableFuture<V> task,
java.util.function.Function<java.lang.Throwable,java.util.concurrent.CompletableFuture<V>> fn)
CompletableFuture
of the same type. If task completes normally, this will return a CompletableFuture
with the same value as task. If task completes exceptionally,
this will call fn with the exception returned by task and return
the result of the CompletableFuture returned by that function.V - return type of the asynchronous tasktask - the asynchronous process to handle exceptions fromfn - a function mapping exceptions from task to a CompletableFuture of the same
type as taskCompletableFuture that contains the value returned by task
if task completes normally and the result of fn if task
completes exceptionallypublic static <V,T> java.util.concurrent.CompletableFuture<T> composeHandle(java.util.concurrent.CompletableFuture<V> future,
java.util.function.BiFunction<V,java.lang.Throwable,? extends java.util.concurrent.CompletableFuture<T>> handler)
CompletableFuture.handle()
function, which requires that the handler return a regular value, this
method requires that the handler return a CompletableFuture.
The returned future will then be ready with the result of the
handler's future (or an error if that future completes exceptionally).V - return type of original futureT - return type of final futurefuture - future to compose the handler ontohandler - handler bi-function to compose onto the passed futurepublic static <V,T> java.util.concurrent.CompletableFuture<T> composeHandleAsync(java.util.concurrent.CompletableFuture<V> future,
java.util.function.BiFunction<V,java.lang.Throwable,? extends java.util.concurrent.CompletableFuture<T>> handler)
CompletableFuture.handle()
function, which requires that the handler return a regular value, this
method requires that the handler return a CompletableFuture.
The returned future will then be ready with the result of the
handler's future (or an error if that future completes exceptionally).
The handler will execute on the default executor
used for asychronous tasks.V - return type of original futureT - return type of final futurefuture - future to compose the handler ontohandler - handler bi-function to compose onto the passed futurepublic static <V,T> java.util.concurrent.CompletableFuture<T> composeHandleAsync(java.util.concurrent.CompletableFuture<V> future,
java.util.function.BiFunction<V,java.lang.Throwable,? extends java.util.concurrent.CompletableFuture<T>> handler,
java.util.concurrent.Executor executor)
CompletableFuture.handle()
function, which requires that the handler return a regular value, this
method requires that the handler return a CompletableFuture.
The returned future will then be ready with the result of the
handler's future (or an error if that future completes excpetionally).
The handler will execute on the passed Executor.V - return type of original futureT - return type of final futurefuture - future to compose the handler ontohandler - handler bi-function to compose onto the passed futureexecutor - executor on which to execute the handler functionpublic static <V> java.util.concurrent.CompletableFuture<java.util.List<V>> getAll(java.util.Collection<java.util.concurrent.CompletableFuture<V>> tasks)
V - return type of the asynchronous taskstasks - the tasks whose output is to be added to the outputCompletableFuture that will be set to the collective result of the taskspublic static <V,T> java.util.concurrent.CompletableFuture<V> tag(java.util.concurrent.CompletableFuture<T> task,
V value)
V - return type of original futureT - return type of final futuretask - the asynchronous process whose output is to be replacedvalue - the predetermined value to be returned on success of taskCompletableFuture that will be set to value on completion of taskpublic static <V> java.util.concurrent.CompletableFuture<java.lang.Void> whenAny(java.util.Collection<? extends java.util.concurrent.CompletableFuture<V>> input)
CompletableFuture that will be set when any of the CompletableFuture
inputs are done. A CompletableFuture is done both on success and failure.V - return type of the asynchronous tasksinput - the list of CompletableFutures to monitor. This list
must not be modified during the execution of this call.CompletableFutures are donepublic static <V> java.util.concurrent.CompletableFuture<java.lang.Void> whenAll(java.util.Collection<? extends java.util.concurrent.CompletableFuture<V>> input)
CompletableFuture that will be set when all the CompletableFuture
inputs are done. A CompletableFuture is done both on success and failure.V - return type of the asynchronous tasksinput - the list of CompletableFutures to monitor. This list
must not be modified during the execution of this call.CompletableFutures are done