Package org.forgerock.util
Interface AsyncFunction<VIN,VOUT,E extends Exception>
-
- Type Parameters:
VIN
- The type of the function parameter, orVoid
if the function does not expect a parameter.VOUT
- The type of the function result, orVoid
if the function does not return anything (i.e. it only has side-effects).E
- The type of the exception thrown by the function, orNeverThrowsException
if no exception is thrown by the function.
- All Known Implementing Classes:
CloseSilentlyAsyncFunction
,ExpressionRequestAsyncFunction
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface AsyncFunction<VIN,VOUT,E extends Exception>
An asynchronousFunction
which returns a result at some point in the future.Exception handling: implementations may fail immediately if an error is encountered before asynchronous processing begins (e.g. if the function parameter is invalid). Implementations which do not throw any exceptions should declare that they throw an exception of type
NeverThrowsException
.Example usage:
public class IsPossiblePrime implements AsyncFunction<String, Boolean, IllegalArgumentException> { // Executor for asynchronously computing primeness. private ExecutorService executor = Executors.newCachedThreadPool(); public Promise<Boolean, IllegalArgumentException> apply(String value) throws IllegalArgumentException { // Create a promise which will hold the asynchronous result. final PromiseImpl<Boolean, IllegalArgumentException> promise = PromiseImpl.create(); // Parse the parameter now and potentially immediately throw an // exception. Parsing could be deferred to the executor in which // case the exception should be trapped and promise.handleException() // invoked. final BigInteger possiblePrime = new BigInteger(value); // Use an executor to asynchronously determine if the parameter is a // prime number. executor.execute(new Runnable() { @Override public void run() { // Set the promised result. promise.handleResult(possiblePrime.isProbablePrime(1000)); } }); return promise; } }
- See Also:
Function
,NeverThrowsException
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Promise<VOUT,E>
apply(VIN value)
Asynchronously applies this function to the input parametervalue
and returns aPromise
for the result.
-