Interface AsyncFunction<VIN,​VOUT,​E extends Exception>

  • Type Parameters:
    VIN - The type of the function parameter, or Void if the function does not expect a parameter.
    VOUT - The type of the function result, or Void if the function does not return anything (i.e. it only has side-effects).
    E - The type of the exception thrown by the function, or NeverThrowsException 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 asynchronous Function 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 Detail

      • apply

        Promise<VOUT,​E> apply​(VIN value)
                             throws E extends Exception
        Asynchronously applies this function to the input parameter value and returns a Promise for the result.
        Parameters:
        value - The input parameter.
        Returns:
        The Promise representing the result of applying this function to value.
        Throws:
        E - If this function cannot be applied to value, an exception can be thrown instead of returned as as failed promise.
        E extends Exception