public abstract class AsyncFunctionUtils extends Object
Constructor and Description |
---|
AsyncFunctionUtils() |
Modifier and Type | Method and Description |
---|---|
static <I,O> AsyncFunction<I,List<O>> |
aggregate(AsyncFunction<? super I,? extends O>... functions)
Creates an asynchronous function that calls the specified asynchronous
functions and produces a listenable future that is the aggregation of the
futures returned by the individual functions.
|
static <I,O> AsyncFunction<I,List<O>> |
aggregate(Iterable<? extends AsyncFunction<? super I,? extends O>> functions)
Creates an asynchronous function that calls the specified asynchronous
functions and produces a listenable future that is the aggregation of the
futures returned by the individual functions.
|
static <I,O> AsyncFunction<I,O> |
asAsyncFunction(Function<? super I,? extends O> function)
Converts a synchronous function to an asynchronous function.
|
static <I,O,T> AsyncFunction<I,O> |
compose(AsyncFunction<? super I,T> function1,
AsyncFunction<? super T,? extends O> function2)
Creates an asynchronous function that is the composition of the two
specified asynchronous functions called in series.
|
static <I,O,T> AsyncFunction<I,O> |
compose(AsyncFunction<? super I,T> function1,
Function<? super T,? extends O> function2)
Creates an asynchronous function that is the composition of an
asynchronous function and a synchronous function called in series.
|
static <T> AsyncFunction<T,T> |
compose(AsyncFunction<? super T,? extends T>... functions)
Creates an asynchronous function that calls the specified asynchronous
functions in series.
|
static <I,O,T> AsyncFunction<I,O> |
compose(Executor executor,
AsyncFunction<? super I,T> function1,
AsyncFunction<? super T,? extends O> function2)
Creates an asynchronous function that is the composition of the two
specified asynchronous functions called in series.
|
static <I,O,T> AsyncFunction<I,O> |
compose(Executor executor,
AsyncFunction<? super I,T> function1,
Function<? super T,? extends O> function2)
Creates an asynchronous function that is the composition of an
asynchronous function and a synchronous function called in series.
|
static <T> AsyncFunction<T,T> |
compose(Executor executor,
AsyncFunction<? super T,? extends T>... functions)
Creates an asynchronous function that calls the specified asynchronous
functions in series.
|
static <T> AsyncFunction<T,T> |
compose(Executor executor,
Iterable<? extends AsyncFunction<? super T,? extends T>> functions)
Creates an asynchronous function that calls the specified asynchronous
functions in series.
|
static <I,O,T> AsyncFunction<I,O> |
compose(Function<? super I,T> function1,
AsyncFunction<? super T,O> function2)
Creates an asynchronous function that is the composition of a synchronous
function and an asynchronous function called in series.
|
static <T> AsyncFunction<T,T> |
compose(Iterable<? extends AsyncFunction<? super T,? extends T>> functions)
Creates an asynchronous function that calls the specified asynchronous
functions in series.
|
static <T> AsyncFunction<T,T> |
identity()
Returns the identity function.
|
static <T> AsyncFunction<T,T> |
ifThen(Predicate<? super T> predicate,
AsyncFunction<? super T,T> function)
Creates an asynchronous function that only executes another asynchronous
function if a condition is met.
|
static <I,O> AsyncFunction<I,O> |
ifThenElse(Predicate<? super I> predicate,
AsyncFunction<? super I,O> functionIfTrue,
AsyncFunction<? super I,O> functionIfFalse)
Creates an asynchronous function that executes either of two asynchronous
functions based on a predicate.
|
@SafeVarargs public static <I,O> AsyncFunction<I,List<O>> aggregate(AsyncFunction<? super I,? extends O>... functions)
Creates an asynchronous function that calls the specified asynchronous functions and produces a listenable future that is the aggregation of the futures returned by the individual functions.
The asynchronous function returned will call the passed functions'
apply(...)
methods in series (in the
thread calling the returned function's apply(...)
method),
but the future returned by the apply(...)
of the returned
function will represent a future that completes when all of the futures
returned by the individual functions have completed. If all those futures
completed successfully, the future provides a list with the values of the
individual futures. If any of the futures returned by the individual
functions fails, the future returned by the returned function also fails.
This method is very similar to Futures.allAsList(Iterable)
. The
main difference is, that it works on AsyncFunction
s instead of
ListenableFuture
s and thus is suitable for the combination of
functions before they are actually supposed to be called.
I
- input type of the resulting function.O
- type of the elements of the resulting function's output list.
This must be compatible with the output types of the specified
functions
.functions
- functions shall be called when the function returned by this
method is called. Each of these functions gets the input
provided to the function returned by this method passed when
being called.NullPointerException
- if any of the passed functions are null
.public static <I,O> AsyncFunction<I,List<O>> aggregate(Iterable<? extends AsyncFunction<? super I,? extends O>> functions)
Creates an asynchronous function that calls the specified asynchronous functions and produces a listenable future that is the aggregation of the futures returned by the individual functions.
The asynchronous function returned will call the passed functions'
apply(...)
methods in series (in the
thread calling the returned function's apply(...)
method),
but the future returned by the apply(...)
of the returned
function will represent a future that completes when all of the futures
returned by the individual functions have completed. If all those futures
completed successfully, the future provides a list with the values of the
individual futures. If any of the futures returned by the individual
functions fails, the future returned by the returned function also fails.
This method is very similar to Futures.allAsList(Iterable)
. The
main difference is, that it works on AsyncFunction
s instead of
ListenableFuture
s and thus is suitable for the combination of
functions before they are actually supposed to be called.
I
- input type of the resulting function.O
- type of the elements of the resulting function's output list.
This must be compatible with the output types of the specified
functions
.functions
- functions shall be called when the function returned by this
method is called. Each of these functions gets the input
provided to the function returned by this method passed when
being called.NullPointerException
- if any of the elements returned by the passed iterable are
null
.public static <I,O> AsyncFunction<I,O> asAsyncFunction(Function<? super I,? extends O> function)
I
- input type of the resulting function.O
- output type of the resulting function.function
- synchronous function that shall be wrapped in an asynchronous
function.NullPointerException
- if the specified function
is null.public static <I,O,T> AsyncFunction<I,O> compose(AsyncFunction<? super I,T> function1, AsyncFunction<? super T,? extends O> function2)
Creates an asynchronous function that is the composition of the two specified asynchronous functions called in series.
The returned function will first call the first of the specified functions. When the future returned by that function completes, it will call the second of the specified functions, passing it the value of the future. The returned function returns the future returned by the second of the specified functions.
The first function is called in the thread that called the returned function. The second function is called in the thread that completes the future returned by the first function. If the future returned by the first function completes before the first function returns, the second function is called in the thread that calls the returned function. For these reasons, the second asynchronous function should not block or perform a considerable amount of work. Instead, it should quickly return a future and perform all actual work in a separate thread that completes the future when the work has finished.
This method is similar to
Futures.transform(ListenableFuture, AsyncFunction)
, but it works
on asynchronous functions instead of futures. Therefore, it is suitable
for building an asynchronous function that represents the concatenation
of other asynchronous functions before any of those functions are
actually supposed to be called.
The asynchronous function returned by this method works in a completely asynchronous way: When called, the function returns a listenable future that completes when all functions have completed or fails if one of the functions has failed. Each function is called asynchronously once the future returned by the previous function has completed.
I
- input type of the resulting function.O
- output type of the resulting function.T
- output type of function1
. This type must be
compatible with the input type of function2
.function1
- first function to be called. This function is executed in the
thread that calls the returned function.function2
- second function to be called. This function is called with the
return value of the first function when the future returned by
the first function completes. The function is called in the
thread that completes the future (or the thread that called
the returned function if the future has already been completed
when the first function returns).NullPointerException
- if any of the specified arguments is null
.public static <I,O,T> AsyncFunction<I,O> compose(Executor executor, AsyncFunction<? super I,T> function1, AsyncFunction<? super T,? extends O> function2)
Creates an asynchronous function that is the composition of the two specified asynchronous functions called in series.
The returned function will first call the first of the specified functions. When the future returned by that function completes, it will call the second of the specified functions, passing it the value of the future. The returned function returns the future returned by the second of the specified functions.
The first function is called in the thread that called the returned function. The second function is called through the means of the specified executor.
This method is similar to
Futures.transform(ListenableFuture, AsyncFunction, Executor)
, but
it works on asynchronous functions instead of futures. Therefore, it is
suitable for building an asynchronous function that represents the
concatenation of other asynchronous functions before any of those
functions are actually supposed to be called.
The asynchronous function returned by this method works in a completely asynchronous way: When called, the function returns a listenable future that completes when all functions have completed or fails if one of the functions has failed. Each function is called asynchronously once the future returned by the previous function has completed.
I
- input type of the resulting function.O
- output type of the resulting function.T
- output type of function1
. This type must be
compatible with the input type of function2
.executor
- executor used for executing the second function.function1
- first function to be called. This function is executed in the
thread that calls the returned function.function2
- second function to be called. This function is called with the
return value of the first function when the future returned by
the first function completes. The function is called through
means of the specified executor
.NullPointerException
- if any of the specified arguments is null
.public static <I,O,T> AsyncFunction<I,O> compose(AsyncFunction<? super I,T> function1, Function<? super T,? extends O> function2)
Creates an asynchronous function that is the composition of an asynchronous function and a synchronous function called in series.
The returned function will first call the specified asynchronous function. When the future returned by that function completes, it will call the specified synchronous function, passing it the value of the future. The returned function returns a future that provides the value returned by the specified synchronous function.
The asynchronous function is called in the thread that calls the returned function. The synchronous function is called in the thread that completes the future returned by the asynchronous function. If the future returned by the asynchronous function completes before the asynchronous function returns, the synchronous function is called in the thread that calls the returned function. For these reasons, the supplied synchronous function should not block or perform a considerable amount of work.
This method is similar to
Futures.transform(ListenableFuture, Function)
, but it works on an
asynchronous function instead of a future. Therefore, it is suitable for
building an asynchronous function that represents the concatenation of
other functions before any of those functions are actually supposed to be
called.
The asynchronous function returned by this method works in a completely asynchronous way: When called, the function returns a listenable future that completes when all functions have completed or fails if one of the functions has failed. Each function is called asynchronously once the future returned by the previous function has completed.
I
- input type of the resulting function.O
- output type of the resulting function.T
- output type of function1
. This type must be
compatible with the input type of function2
.function1
- first function to be called. This function is executed in the
thread that calls the returned function.function2
- second function to be called. This function is called with the
return value of the first function when the future returned by
the first function completes. The function is called in the
thread that completes the future (or the thread that called
the returned function if the future has already been completed
when the first function returns).NullPointerException
- if any of the specified arguments is null
.public static <I,O,T> AsyncFunction<I,O> compose(Executor executor, AsyncFunction<? super I,T> function1, Function<? super T,? extends O> function2)
Creates an asynchronous function that is the composition of an asynchronous function and a synchronous function called in series.
The returned function will first call the specified asynchronous function. When the future returned by that function completes, it will call the specified synchronous function, passing it the value of the future. The returned function returns a future that provides the value returned by the specified synchronous function.
The asynchronous function is called in the thread that calls the returned function. The synchronous function is called through the means of the specified executor.
This method is similar to
Futures.transform(ListenableFuture, Function, Executor)
, but it
works on an asynchronous function instead of a future. Therefore, it is
suitable for building an asynchronous function that represents the
concatenation of other functions before any of those functions are
actually supposed to be called.
The asynchronous function returned by this method works in a completely asynchronous way: When called, the function returns a listenable future that completes when all functions have completed or fails if one of the functions has failed. Each function is called asynchronously once the future returned by the previous function has completed.
I
- input type of the resulting function.O
- output type of the resulting function.T
- output type of function1
. This type must be
compatible with the input type of function2
.executor
- executor used for executing the synchronous function.function1
- first function to be called. This function is executed in the
thread that calls the returned function.function2
- second function to be called. This function is called with the
return value of the first function when the future returned by
the first function completes. The function is called through
means of the specified executor
.NullPointerException
- if any of the specified arguments is null
.public static <I,O,T> AsyncFunction<I,O> compose(Function<? super I,T> function1, AsyncFunction<? super T,O> function2)
Creates an asynchronous function that is the composition of a synchronous function and an asynchronous function called in series.
The returned function will first call the specified synchronous function. It will then call the specified asynchronous function, passing it the return value of the synchronous function. The returned function returns the future returned by the specified asynchronous function.
Both functions are called in the thread that calls the returned function.
I
- input type of the resulting function.O
- output type of the resulting function.T
- output type of function1
. This type must be
compatible with the input type of function2
.function1
- first function to be called. This function is executed in the
thread that calls the returned function.function2
- second function to be called. This function is called with the
return value of the first function. The function is called in
the thread that calls the returned function.NullPointerException
- if any of the specified arguments is null
.@SafeVarargs public static <T> AsyncFunction<T,T> compose(AsyncFunction<? super T,? extends T>... functions)
Creates an asynchronous function that calls the specified asynchronous functions in series.
The returned function will call the specified functions in the specified order, passing the result of each function to the following one. The first of the specified functions receives the input passed to the returned function and the returned function returns the output returned by the last of the specified functions.
Each of the functions' apply(...)
method is called in the thread that completes the future returned by the
previous function. The first function is called in the thread that calls
the returned function. For this reason, the supplied asynchronous
functions should not block or perform a considerable amount of work.
Instead, they should quickly return a future and perform all actual work
in a separate thread that completes the future when the work has
finished.
This method is similar to
Futures.transform(ListenableFuture, AsyncFunction)
, but it works
on asynchronous functions instead of futures and can take several
functions. Therefore, it is suitable for building an asynchronous
function that represents the concatenation of other asynchronous
functions before any of those functions are actually supposed to be
called.
Due to limitations of Java generics, this method only supports a list of
functions where the return type of each function is compatible with the
input type of each function. However, this is not a limitation of the
implementation. When raw types are used, this method can work with
arbitrary asynchronous functions as long as the return type of each
function is compatible with the input type of the next function. For
concatenating functions with different types in a type-safe way, consider
using compose(AsyncFunction, AsyncFunction)
instead.
The asynchronous function returned by this method works in a completely asynchronous way: When called, the function returns a listenable future that completes when all functions have completed or fails if one of the functions has failed. Each function is called asynchronously once the future returned by the previous function has completed.
T
- input and output type of the resulting function. The input and
output types of the supplied functions
must be
compatible with this type.functions
- functions to be encapsulated in the returned function. The
functions are executed in series when the returned function is
called.NullPointerException
- if any of the specified functions
are
null
.@SafeVarargs public static <T> AsyncFunction<T,T> compose(Executor executor, AsyncFunction<? super T,? extends T>... functions)
Creates an asynchronous function that calls the specified asynchronous functions in series.
The returned function will call the specified functions in the specified order, passing the result of each function to the following one. The first of the specified functions receives the input passed to the returned function and the returned function returns the output returned by the last of the specified functions.
The functions' apply(...)
method is
called through means of the specified executor
, except for
the first of the specified functions which is always called in the thread
that calls the returned function.
This method is similar to
Futures.transform(ListenableFuture, AsyncFunction, Executor)
, but
it works on asynchronous functions instead of futures and can take
several functions. Therefore, it is suitable for building an asynchronous
function that represents the concatenation of other asynchronous
functions before any of those functions are actually supposed to be
called.
Due to limitations of Java generics, this method only supports a list of
functions where the return type of each function is compatible with the
input type of each function. However, this is not a limitation of the
implementation. When raw types are used, this method can work with
arbitrary asynchronous functions as long as the return type of each
function is compatible with the input type of the next function. For
concatenating functions with different types in a type-safe way, consider
using compose(Executor, AsyncFunction, AsyncFunction)
instead.
The asynchronous function returned by this method works in a completely asynchronous way: When called, the function returns a listenable future that completes when all functions have completed or fails if one of the functions has failed. Each function is called asynchronously once the future returned by the previous function has completed.
T
- input and output type of the resulting function. The input and
output types of the supplied functions
must be
compatible with this type.executor
- executor used for calling the functions'
apply(...)
methods.functions
- functions to be encapsulated in the returned function. The
functions are executed in series when the returned function is
called.NullPointerException
- if the specified executor
is null
and if any of the specified functions
are
null
.public static <T> AsyncFunction<T,T> compose(Iterable<? extends AsyncFunction<? super T,? extends T>> functions)
Creates an asynchronous function that calls the specified asynchronous functions in series.
The returned function will call the specified functions in the specified order, passing the result of each function to the following one. The first of the specified functions receives the input passed to the returned function and the returned function returns the output returned by the last of the specified functions.
Each of the functions' apply(...)
method is called in the thread that completes the future returned by the
previous function. The first function is called in the thread that calls
the returned function. For this reason, the supplied asynchronous
functions should not block or perform a considerable amount of work.
Instead, they should quickly return a future and perform all actual work
in a separate thread that completes the future when the work has
finished.
This method is similar to
Futures.transform(ListenableFuture, AsyncFunction, Executor)
, but
it works on asynchronous functions instead of futures and can take
several functions. Therefore, it is suitable for building an asynchronous
function that represents the concatenation of other asynchronous
functions before any of those functions are actually supposed to be
called.
Due to limitations of Java generics, this method only supports a list of
functions where the return type of each function is compatible with the
input type of each function. However, this is not a limitation of the
implementation. When raw types are used, this method can work with
arbitrary asynchronous functions as long as the return type of each
function is compatible with the input type of the next function. For
concatenating functions with different types in a type-safe way, consider
using compose(AsyncFunction, AsyncFunction)
instead.
The asynchronous function returned by this method works in a completely asynchronous way: When called, the function returns a listenable future that completes when all functions have completed or fails if one of the functions has failed. Each function is called asynchronously once the future returned by the previous function has completed.
T
- input and output type of the resulting function. The input and
output types of the supplied functions
must be
compatible with this type.functions
- functions to be encapsulated in the returned function. The
functions are executed in series when the returned function is
called.NullPointerException
- if any of the elements returned by the passed iterable are
null
.public static <T> AsyncFunction<T,T> compose(Executor executor, Iterable<? extends AsyncFunction<? super T,? extends T>> functions)
Creates an asynchronous function that calls the specified asynchronous functions in series.
The returned function will call the specified functions in the specified order, passing the result of each function to the following one. The first of the specified functions receives the input passed to the returned function and the returned function returns the output returned by the last of the specified functions.
The functions' apply(...)
method is
called through means of the specified executor
, except for
the first of the specified functions which is always called in the thread
that calls the returned function.
This method is similar to
Futures.transform(ListenableFuture, AsyncFunction, Executor)
, but
it works on asynchronous functions instead of futures and can take
several functions. Therefore, it is suitable for building an asynchronous
function that represents the concatenation of other asynchronous
functions before any of those functions are actually supposed to be
called.
Due to limitations of Java generics, this method only supports a list of
functions where the return type of each function is compatible with the
input type of each function. However, this is not a limitation of the
implementation. When raw types are used, this method can work with
arbitrary asynchronous functions as long as the return type of each
function is compatible with the input type of the next function. For
concatenating functions with different types in a type-safe way, consider
using compose(Executor, AsyncFunction, AsyncFunction)
instead.
The asynchronous function returned by this method works in a completely asynchronous way: When called, the function returns a listenable future that completes when all functions have completed or fails if one of the functions has failed. Each function is called asynchronously once the future returned by the previous function has completed.
T
- input and output type of the resulting function. The input and
output types of the supplied functions
must be
compatible with this type.executor
- executor used for calling the functions'
apply(...)
methods.functions
- functions to be encapsulated in the returned function. The
functions are executed in series when the returned function is
called.NullPointerException
- if the specified executor
is null
and if any of the elements returned by the passed iterable
are null
.public static <T> AsyncFunction<T,T> identity()
T
- input and output type of the resulting function.public static <T> AsyncFunction<T,T> ifThen(Predicate<? super T> predicate, AsyncFunction<? super T,T> function)
Creates an asynchronous function that only executes another asynchronous function if a condition is met.
When called, the returned asynchronous function evaluates the specified
predicate. If the predicate returns true
, the specified
function is called and its result is returned. If it is
false
, the input passed to the returned function is returned
directly (wrapping it in an immediate future).
The input passed to the returned function when it is called is passed to both the predicate and the specified function (if the latter is called).
T
- input and output type of the resulting function. This type
must be compatible with the input and output types of the
specified predicate
and function
.predicate
- predicate to be tested when the returned function is called.function
- function to be called when the returned function is called and
the specified predicate
is true
.true
.NullPointerException
- if any of the specified arguments is null
.public static <I,O> AsyncFunction<I,O> ifThenElse(Predicate<? super I> predicate, AsyncFunction<? super I,O> functionIfTrue, AsyncFunction<? super I,O> functionIfFalse)
Creates an asynchronous function that executes either of two asynchronous functions based on a predicate.
When called, the returned asynchronous function evaluates the specified
predicate. If the predicate returns true
, the first of the
specified functions is called and its result is returned. If the
predicate is false
, the second of the specified functions is
called and its result is returned.
The input passed to the returned function when it is called is passed to both the predicate and the function that is selected based on the predicate.
I
- input type of the resulting function. This type must be
compatible with the input types of the specified
predicate
, functionIfTrue
, and
functionIfFalse
.O
- output type of the resulting function. This type must be
compatible with the output types of the specified
functionIfTrue
and functionIfFalse
.predicate
- predicate to be tested when the returned function is called.functionIfTrue
- function to be called when the returned function is called and
the specified predicate
is true
.functionIfFalse
- function to be called when the returned function is called and
the specified predicate
is false
.NullPointerException
- if any of the specified arguments is null
.Copyright © 2011–2017 aquenos GmbH. All rights reserved.