Package com.apple.foundationdb.async
Class AsyncUtil
java.lang.Object
com.apple.foundationdb.async.AsyncUtil
Provided utilities for using and manipulating
CompletableFutures.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CompletableFuture<Void>A completed future of typeVoid.static final CompletableFuture<Boolean>A completed future of typeBooleanthat is set tofalse.static final CompletableFuture<Boolean>A completed future of typeBooleanthat is set totrue. -
Method Summary
Modifier and TypeMethodDescriptionstatic <I,O> CompletableFuture<O> applySafely(Function<I, ? extends CompletableFuture<O>> func, I value) RunFunctionfunc, returning all caught exceptions as aCompletableFuturein an error state.static <V> CompletableFuture<List<V>>collect(AsyncIterable<V> iterable) Iterates over a stream of items and returns the result as a list.static <V> CompletableFuture<List<V>>collect(AsyncIterable<V> iterable, Executor executor) Iterates over a set of items and returns the result as a list.static <V> CompletableFuture<List<V>>collectRemaining(AsyncIterator<V> iterator) Iterates over a set of items and returns the remaining results as a list.static <V> CompletableFuture<List<V>>collectRemaining(AsyncIterator<V> iterator, Executor executor) Iterates over a set of items and returns the remaining results as a list.static <V> CompletableFuture<V>composeExceptionally(CompletableFuture<V> task, Function<Throwable, CompletableFuture<V>> fn) Composes an asynchronous task with an exception-handler that returns aCompletableFutureof the same type.static <V,T> CompletableFuture<T> composeHandle(CompletableFuture<V> future, BiFunction<V, Throwable, ? extends CompletableFuture<T>> handler) Compose a handler bi-function to the result of a future.static <V,T> CompletableFuture<T> composeHandleAsync(CompletableFuture<V> future, BiFunction<V, Throwable, ? extends CompletableFuture<T>> handler) Compose a handler bi-function to the result of a future.static <V,T> CompletableFuture<T> composeHandleAsync(CompletableFuture<V> future, BiFunction<V, Throwable, ? extends CompletableFuture<T>> handler, Executor executor) Compose a handler bi-function to the result of a future.static <V> CompletableFuture<Void>forEach(AsyncIterable<V> iterable, Consumer<? super V> consumer) Run theconsumeron each element of the iterable in order.static <V> CompletableFuture<Void>forEach(AsyncIterable<V> iterable, Consumer<? super V> consumer, Executor executor) Run theconsumeron each element of the iterable in order.static <V> CompletableFuture<Void>forEachRemaining(AsyncIterator<V> iterator, Consumer<? super V> consumer) Run theconsumeron each element remaining in the iterator in order.static <V> CompletableFuture<Void>forEachRemaining(AsyncIterator<V> iterator, Consumer<? super V> consumer, Executor executor) Run theconsumeron each element remaining if the iterator in order.static <V> CompletableFuture<List<V>>getAll(Collection<CompletableFuture<V>> tasks) Collects the results of many asynchronous processes into one asynchronous output.static <V,T> AsyncIterable<T> mapIterable(AsyncIterable<V> iterable, Function<V, T> func) Map anAsyncIterableinto anAsyncIterableof another type or with each element modified in some fashion.static <V,T> AsyncIterator<T> mapIterator(AsyncIterator<V> iterator, Function<V, T> func) Map anAsyncIteratorinto anAsyncIteratorof another type or with each element modified in some fashion.static <V,T> CloseableAsyncIterator<T> mapIterator(CloseableAsyncIterator<V> iterator, Function<V, T> func) Map aCloseableAsyncIteratorinto aCloseableAsyncIteratorof another type or with each element modified in some fashion.static <V> CompletableFuture<Void>success(CompletableFuture<V> task) Maps the outcome of a task into a completion signal.static <V,T> CompletableFuture<V> tag(CompletableFuture<T> task, V value) Replaces the output of an asynchronous task with a predetermined value.static <V> CompletableFuture<Void>whenAll(Collection<? extends CompletableFuture<V>> input) Return aCompletableFuturethat will be set when all theCompletableFutureinputs are done.static <V> CompletableFuture<Void>whenAny(Collection<? extends CompletableFuture<V>> input) Return aCompletableFuturethat will be set when any of theCompletableFutureinputs are done.static <V> CompletableFuture<Void>whenReady(CompletableFuture<V> task) Maps the readiness of aCompletableFutureinto a completion signal.static CompletableFuture<Void>whileTrue(Function<Void, ? extends CompletableFuture<Boolean>> body) Deprecated.Since version 5.1.0.static CompletableFuture<Void>Deprecated.Since version 5.1.0.static CompletableFuture<Void>whileTrue(Supplier<CompletableFuture<Boolean>> body) Executes an asynchronous operation repeatedly until it returnsFalse.static CompletableFuture<Void>whileTrue(Supplier<CompletableFuture<Boolean>> body, Executor executor) Executes an asynchronous operation repeatedly until it returnsFalse.
-
Field Details
-
DONE
-
READY_TRUE
A completed future of typeBooleanthat is set totrue. This can be used instead of creating a new future if one wants to signal that some task has already been completed with atrueresult. -
READY_FALSE
A completed future of typeBooleanthat is set tofalse. This can be used instead of creating a new future if one wants to signal that some task has already been completed with afalseresult.
-
-
Method Details
-
applySafely
public static <I,O> CompletableFuture<O> applySafely(Function<I, ? extends CompletableFuture<O>> func, I value) RunFunctionfunc, returning all caught exceptions as aCompletableFuturein an error state.- Type Parameters:
I- type of input tofuncO- type of output offunc- Parameters:
func- theFunctionto runvalue- the input to pass tofunc- Returns:
- the output of
func, or aCompletableFuturecarrying any exception caught in the process.
-
forEach
public static <V> CompletableFuture<Void> forEach(AsyncIterable<V> iterable, Consumer<? super V> consumer) Run theconsumeron 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 withnullif 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.- Type Parameters:
V- type of the items returned by the iterable- Parameters:
iterable- the source of data over from which to consumeconsumer- operation to apply to each item- Returns:
- a future that is ready once the asynchronous operation completes
-
forEach
public static <V> CompletableFuture<Void> forEach(AsyncIterable<V> iterable, Consumer<? super V> consumer, Executor executor) Run theconsumeron 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 withnullif 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.- Type Parameters:
V- type of the items returned by the iterable- Parameters:
iterable- the source of data over from which to consumeconsumer- operation to apply to each itemexecutor- executor on which to schedule asynchronous tasks- Returns:
- a future that is ready once the asynchronous operation completes
-
forEachRemaining
public static <V> CompletableFuture<Void> forEachRemaining(AsyncIterator<V> iterator, Consumer<? super V> consumer) Run theconsumeron 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 withnullif 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.- Type Parameters:
V- type of the items returned by the iterator- Parameters:
iterator- the source of data over from which to consumeconsumer- operation to apply to each item- Returns:
- a future that is ready once the asynchronous operation completes
-
forEachRemaining
public static <V> CompletableFuture<Void> forEachRemaining(AsyncIterator<V> iterator, Consumer<? super V> consumer, Executor executor) Run theconsumeron 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 withnullif 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.- Type Parameters:
V- type of the items returned by the iterator- Parameters:
iterator- the source of data over from which to consumeconsumer- operation to apply to each itemexecutor- executor on which to schedule asynchronous tasks- Returns:
- a future that is ready once the asynchronous operation completes
-
collect
Iterates over a stream of items and returns the result as a list.- Type Parameters:
V- type of the items returned by the iterable- Parameters:
iterable- the source of data over which to iterate- Returns:
- a
CompletableFuturewhich will be set to the amalgamation of results from iteration.
-
collectRemaining
Iterates over a set of items and returns the remaining results as a list.- Type Parameters:
V- type of the items returned by the iterator- Parameters:
iterator- the source of data over which to iterate. This function will exhaust the iterator.- Returns:
- a
CompletableFuturewhich will be set to the amalgamation of results from iteration.
-
collect
Iterates over a set of items and returns the result as a list.- Type Parameters:
V- type of the items returned by the iterable- Parameters:
iterable- the source of data over which to iterateexecutor- theExecutorto use for asynchronous operations- Returns:
- a
CompletableFuturewhich will be set to the amalgamation of results from iteration.
-
collectRemaining
public static <V> CompletableFuture<List<V>> collectRemaining(AsyncIterator<V> iterator, Executor executor) Iterates over a set of items and returns the remaining results as a list.- Type Parameters:
V- type of the items returned by the iterator- Parameters:
iterator- the source of data over which to iterate. This function will exhaust the iterator.executor- theExecutorto use for asynchronous operations- Returns:
- a
CompletableFuturewhich will be set to the amalgamation of results from iteration.
-
mapIterable
Map anAsyncIterableinto anAsyncIterableof another type or with each element modified in some fashion.- Type Parameters:
V- type of the items returned by the original iterableT- type of the items returned by the final iterable- Parameters:
iterable- inputfunc- mapping function applied to each element- Returns:
- a new iterable with each element mapped to a different value
-
mapIterator
Map anAsyncIteratorinto anAsyncIteratorof another type or with each element modified in some fashion.- Type Parameters:
V- type of the items returned by the original iteratorT- type of the items returned by the final iterator- Parameters:
iterator- inputfunc- mapping function applied to each element- Returns:
- a new iterator with each element mapped to a different value
-
mapIterator
public static <V,T> CloseableAsyncIterator<T> mapIterator(CloseableAsyncIterator<V> iterator, Function<V, T> func) Map aCloseableAsyncIteratorinto aCloseableAsyncIteratorof another type or with each element modified in some fashion.- Type Parameters:
V- type of the items returned by the original iteratorT- type of the items returned by the final iterator- Parameters:
iterator- inputfunc- mapping function applied to each element- Returns:
- a new iterator with each element mapped to a different value
-
whileTrue
@Deprecated public static CompletableFuture<Void> whileTrue(Function<Void, ? extends CompletableFuture<Boolean>> body) Executes an asynchronous operation repeatedly until it returnsFalse.- Parameters:
body- the asynchronous operation over which to loop- Returns:
- a
CompletableFuturewhich will be set at completion of the loop.
-
whileTrue
@Deprecated public static CompletableFuture<Void> whileTrue(Function<Void, ? extends CompletableFuture<Boolean>> body, Executor executor) Executes an asynchronous operation repeatedly until it returnsFalse.- Parameters:
body- the asynchronous operation over which to loopexecutor- theExecutorto use for asynchronous operations- Returns:
- a
CompletableFuturewhich will be set at completion of the loop.
-
whileTrue
Executes an asynchronous operation repeatedly until it returnsFalse.- Parameters:
body- the asynchronous operation over which to loop- Returns:
- a
CompletableFuturewhich will be set at completion of the loop.
-
whileTrue
public static CompletableFuture<Void> whileTrue(Supplier<CompletableFuture<Boolean>> body, Executor executor) Executes an asynchronous operation repeatedly until it returnsFalse.- Parameters:
body- the asynchronous operation over which to loopexecutor- theExecutorto use for asynchronous operations- Returns:
- a
CompletableFuturewhich will be set at completion of the loop.
-
success
Maps the outcome of a task into a completion signal. Can be useful iftaskhas side-effects for which all is needed is a signal of completion. All errors fromtaskwill be passed to the resultingCompletableFuture.- Type Parameters:
V- type of element returned bytask- Parameters:
task- the asynchronous process for which to signal completion- Returns:
- a newly created
CompletableFuturethat is set whentaskcompletes
-
whenReady
Maps the readiness of aCompletableFutureinto a completion signal. When the givenCompletableFutureis set to a value or an error, the returnedCompletableFuturewill be set to null. The returnedCompletableFuturewill never be set to an error unless it is explicitly cancelled.- Type Parameters:
V- return type of the asynchronous task- Parameters:
task- the asynchronous process to monitor the readiness of- Returns:
- a new
CompletableFuturethat is set whentaskis ready.
-
composeExceptionally
public static <V> CompletableFuture<V> composeExceptionally(CompletableFuture<V> task, Function<Throwable, CompletableFuture<V>> fn) Composes an asynchronous task with an exception-handler that returns aCompletableFutureof the same type. Iftaskcompletes normally, this will return aCompletableFuturewith the same value astask. Iftaskcompletes exceptionally, this will callfnwith the exception returned bytaskand return the result of theCompletableFuturereturned by that function.- Type Parameters:
V- return type of the asynchronous task- Parameters:
task- the asynchronous process to handle exceptions fromfn- a function mapping exceptions fromtaskto aCompletableFutureof the same type astask- Returns:
- a
CompletableFuturethat contains the value returned bytaskiftaskcompletes normally and the result offniftaskcompletes exceptionally
-
composeHandle
public static <V,T> CompletableFuture<T> composeHandle(CompletableFuture<V> future, BiFunction<V, Throwable, ? extends CompletableFuture<T>> handler) Compose a handler bi-function to the result of a future. Unlike theCompletableFuture.handle()function, which requires that the handler return a regular value, this method requires that the handler return aCompletableFuture. The returned future will then be ready with the result of the handler's future (or an error if that future completes exceptionally).- Type Parameters:
V- return type of original futureT- return type of final future- Parameters:
future- future to compose the handler ontohandler- handler bi-function to compose onto the passed future- Returns:
- future with same completion properties as the future returned by the handler
-
composeHandleAsync
public static <V,T> CompletableFuture<T> composeHandleAsync(CompletableFuture<V> future, BiFunction<V, Throwable, ? extends CompletableFuture<T>> handler) Compose a handler bi-function to the result of a future. Unlike theCompletableFuture.handle()function, which requires that the handler return a regular value, this method requires that the handler return aCompletableFuture. 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 thedefault executorused for asynchronous tasks.- Type Parameters:
V- return type of original futureT- return type of final future- Parameters:
future- future to compose the handler ontohandler- handler bi-function to compose onto the passed future- Returns:
- future with same completion properties as the future returned by the handler
-
composeHandleAsync
public static <V,T> CompletableFuture<T> composeHandleAsync(CompletableFuture<V> future, BiFunction<V, Throwable, ? extends CompletableFuture<T>> handler, Executor executor) Compose a handler bi-function to the result of a future. Unlike theCompletableFuture.handle()function, which requires that the handler return a regular value, this method requires that the handler return aCompletableFuture. 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 passedExecutor.- Type Parameters:
V- return type of original futureT- return type of final future- Parameters:
future- future to compose the handler ontohandler- handler bi-function to compose onto the passed futureexecutor- executor on which to execute the handler function- Returns:
- future with same completion properties as the future returned by the handler
-
getAll
Collects the results of many asynchronous processes into one asynchronous output. If any of the tasks returns an error, the output is set to that error.- Type Parameters:
V- return type of the asynchronous tasks- Parameters:
tasks- the tasks whose output is to be added to the output- Returns:
- a
CompletableFuturethat will be set to the collective result of the tasks
-
tag
Replaces the output of an asynchronous task with a predetermined value.- Type Parameters:
V- return type of original futureT- return type of final future- Parameters:
task- the asynchronous process whose output is to be replacedvalue- the predetermined value to be returned on success oftask- Returns:
- a
CompletableFuturethat will be set tovalueon completion oftask
-
whenAny
Return aCompletableFuturethat will be set when any of theCompletableFutureinputs are done. ACompletableFutureis done both on success and failure.- Type Parameters:
V- return type of the asynchronous tasks- Parameters:
input- the list ofCompletableFutures to monitor. This list must not be modified during the execution of this call.- Returns:
- a signal that will be set when any of the
CompletableFutures are done
-
whenAll
Return aCompletableFuturethat will be set when all theCompletableFutureinputs are done. ACompletableFutureis done both on success and failure.- Type Parameters:
V- return type of the asynchronous tasks- Parameters:
input- the list ofCompletableFutures to monitor. This list must not be modified during the execution of this call.- Returns:
- a signal that will be set when all of the
CompletableFutures are done
-