Interface AsyncApplyFunction<T,R>

Type Parameters:
T - the type of the input to the function
R - the type of the result of the function
All Superinterfaces:
ApplyFunction<T,R>, Async<R>
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 AsyncApplyFunction<T,R> extends ApplyFunction<T,R>
The AsyncApplyFunction interface represents a function that asynchronously accepts a value of type T and produces a result of type R. This interface extends ApplyFunction and is designed to be used with asynchronous computation frameworks, such as Java's CompletableFuture.

An implementation of this interface is expected to perform an asynchronous operation and return a result, which is typically represented as a CompletableFuture<R>. This allows for non-blocking execution of tasks and is particularly useful for I/O operations or any operation that may take a significant amount of time to complete.

AsyncApplyFunction is used to implement the following semantics:

 
    T res = doAsync1(input);
    // Can use AsyncApplyFunction
    R result = doAsync2(res);
 
 
See Also:
  • Field Summary

    Fields inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async

    CUR_COMPLETABLE_FUTURE
  • Method Summary

    Modifier and Type
    Method
    Description
    Asynchronously applies this function to the result of the given CompletableFuture.
    Asynchronously applies this function to the result of the given CompletableFuture, using the specified executor for the asynchronous computation.
    default R
    apply(T t)
    Synchronously applies this function to the given argument and returns the result.
    void
    Asynchronously applies this function to the given argument.
    async(T t)
    Initiates the asynchronous application of this function to the given result.

    Methods inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async

    getCurCompletableFuture, result, setCurCompletableFuture
  • Method Details

    • applyAsync

      void applyAsync(T t) throws IOException
      Asynchronously applies this function to the given argument.

      This method is intended to initiate the function application without waiting for the result. It is typically used when the result of the operation is not required immediately or when the operation is part of a larger asynchronous workflow.

      Parameters:
      t - the function argument
      Throws:
      IOException - if an I/O error occurs during the application of the function
    • apply

      default R apply(T t) throws IOException
      Synchronously applies this function to the given argument and returns the result.

      This method waits for the asynchronous operation to complete and returns its result. It is useful when the result is needed immediately and the calling code cannot proceed without it.

      Specified by:
      apply in interface ApplyFunction<T,R>
      Parameters:
      t - the function argument
      Returns:
      the result of applying the function to the argument
      Throws:
      IOException - if an I/O error occurs during the application of the function
    • async

      default CompletableFuture<R> async(T t) throws IOException
      Initiates the asynchronous application of this function to the given result.

      This method calls applyAsync to start the asynchronous operation and then retrieves the current thread's CompletableFuture using getCurCompletableFuture. It returns this CompletableFuture, which will be completed with the result of the asynchronous operation once it is finished.

      This method is useful for chaining with other asynchronous operations, as it allows the current operation to be part of a larger asynchronous workflow.

      Parameters:
      t - the function argument
      Returns:
      a CompletableFuture that will be completed with the result of the asynchronous operation
      Throws:
      IOException - if an I/O error occurs during the initiation of the asynchronous operation
    • apply

      default CompletableFuture<R> apply(CompletableFuture<T> in)
      Asynchronously applies this function to the result of the given CompletableFuture.

      This method chains the function application to the completion of the input future. It returns a new CompletableFuture that completes with the function's result when the input future completes.

      Specified by:
      apply in interface ApplyFunction<T,R>
      Parameters:
      in - the input future
      Returns:
      a new CompletableFuture that holds the result of the function application
    • apply

      default CompletableFuture<R> apply(CompletableFuture<T> in, Executor executor)
      Asynchronously applies this function to the result of the given CompletableFuture, using the specified executor for the asynchronous computation.

      This method allows for more control over the execution context of the asynchronous operation, such as running the operation in a separate thread or thread pool.

      Specified by:
      apply in interface ApplyFunction<T,R>
      Parameters:
      in - the input future
      executor - the executor to use for the asynchronous computation
      Returns:
      a new CompletableFuture that holds the result of the function application