Class HookAdapters

java.lang.Object
com.google.genai.gaos.utils.HookAdapters

public final class HookAdapters extends Object
Utility class for adapting synchronous hooks to asynchronous hooks.

This class provides adapter methods that convert synchronous hook implementations (Hook.BeforeRequest, Hook.AfterSuccess, Hook.AfterError) to their asynchronous counterparts (AsyncHook.BeforeRequest, AsyncHook.AfterSuccess, AsyncHook.AfterError).

Performance Note: The execution of synchronous hooks is offloaded to the default asynchronous execution facility used by CompletableFuture. For better performance in high-throughput scenarios, consider re-implementing hooks using non-blocking I/O (NIO) patterns instead of relying on these adapters.

Thread Safety: All adapter methods are thread-safe and can be called concurrently from multiple threads.

See Also:
  • Method Details

    • toAsync

      public static AsyncHook.BeforeRequest toAsync(Hook.BeforeRequest beforeRequestHook)
      Adapts a synchronous Hook.BeforeRequest to an asynchronous AsyncHook.BeforeRequest.

      The synchronous hook execution is offloaded using CompletableFuture's default asynchronous execution facility. Any exceptions thrown by the synchronous hook are wrapped as unchecked exceptions and propagated through the returned CompletableFuture.

      Performance Consideration: For high-throughput applications, consider implementing the hook directly using NIO patterns rather than using this adapter, as it avoids thread pool overhead and blocking operations.

      Parameters:
      beforeRequestHook - the synchronous before-request hook to adapt
      Returns:
      an asynchronous before-request hook that executes the synchronous hook asynchronously
      Throws:
      NullPointerException - if beforeRequestHook is null
    • toAsync

      public static AsyncHook.AfterError toAsync(Hook.AfterError afterErrorHook)
      Adapts a synchronous Hook.AfterError to an asynchronous AsyncHook.AfterError.

      The synchronous hook execution is offloaded using CompletableFuture's default asynchronous execution facility. Any exceptions thrown by the synchronous hook are wrapped as unchecked exceptions and propagated through the returned CompletableFuture.

      Performance Consideration: For high-throughput applications, consider implementing the hook directly using NIO patterns rather than using this adapter, as it avoids thread pool overhead and blocking I/O operations.

      Parameters:
      afterErrorHook - the synchronous after-error hook to adapt
      Returns:
      an asynchronous after-error hook that executes the synchronous hook asynchronously
      Throws:
      NullPointerException - if afterErrorHook is null
    • toAsync

      public static AsyncHook.AfterSuccess toAsync(Hook.AfterSuccess afterSuccessHook)
      Adapts a synchronous Hook.AfterSuccess to an asynchronous AsyncHook.AfterSuccess.

      The synchronous hook execution is offloaded using CompletableFuture's default asynchronous execution facility. Any exceptions thrown by the synchronous hook are wrapped as unchecked exceptions and propagated through the returned CompletableFuture.

      Performance Consideration: For high-throughput applications, consider implementing the hook directly using NIO patterns rather than using this adapter, as it avoids thread pool overhead and blocking I/O operations.

      Parameters:
      afterSuccessHook - the synchronous after-success hook to adapt
      Returns:
      an asynchronous after-success hook that executes the synchronous hook asynchronously
      Throws:
      NullPointerException - if afterSuccessHook is null