Class FutureUtils

java.lang.Object
org.axonframework.common.FutureUtils

public final class FutureUtils extends Object
Utility class containing reusable functionality for interacting with the CompletableFuture.
Since:
5.0.0
Author:
Allard Buijze
  • Method Details

    • ignoreResult

      public static <T> Void ignoreResult(T toIgnore)
      Utility method that doesn't do anything. Its purpose is to simplify conversion of a CompletableFuture with any generic type to a CompletableFuture<Void>.

      Example: completableFuture.thenApply(FutureUtils::ignoreResult

      Type Parameters:
      T - The declared result of the CompletableFuture to ignore the result of.
      Parameters:
      toIgnore - The actual result of the CompletableFuture.
      Returns:
      null, as that's the only valid value for Void.
    • emptyCompletedFuture

      public static <T> CompletableFuture<T> emptyCompletedFuture()
      Creates a completed CompletableFuture with null as result.
      Type Parameters:
      T - The declared type to return in the CompletableFuture.
      Returns:
      A CompletableFuture that is completed with null.
    • alsoComplete

      public static <T> BiConsumer<T,Throwable> alsoComplete(CompletableFuture<T> future)
      Creates a function that can be passed to a CompletableFuture.whenComplete(BiConsumer) or CompletableFuture.whenCompleteAsync(BiConsumer) invocation, to complete the given future with the same result as the CompletableFuture that this function is passed to.
      Type Parameters:
      T - The declared type of result from the CompletableFuture.
      Parameters:
      future - The CompletableFuture to also complete.
      Returns:
      A function that completes another future with the same results.
    • unwrap

      public static Throwable unwrap(Throwable exception)
      Unwrap given exception from the exception-wrappers added by CompletableFuture.

      More specifically, if the given exception is a CompletionException or ExecutionException, it returns the cause. Otherwise, it will return the exception as-is.

      Parameters:
      exception - The exception to unwrap.
      Returns:
      The unwrapped exception if the given exception is of type CompletionException or ExecutionException. Otherwise, it is returned as is.
    • runFailing

      @Nonnull public static <T> CompletableFuture<T> runFailing(@Nonnull Supplier<CompletableFuture<T>> fn)
      Safely catches exceptions thrown by the given fn and returns a CompletableFuture that completes.
      Type Parameters:
      T - Type of the completable future.
      Parameters:
      fn - A lambda returning a CompletableFuture.
      Returns:
      A completable future that completes exceptionally if the given lambda throws an exception.
    • joinAndUnwrap

      @Nullable public static <T> T joinAndUnwrap(@Nonnull CompletableFuture<T> future)
      Joins a CompletableFuture and unwraps any CompletionException to throw the actual cause, preserving the exact exception type without wrapping checked exceptions.

      This method uses the "sneaky throw" technique to re-throw checked exceptions without declaring them, which preserves the original exception type completely. Use this when you need precise exception type preservation and are certain about the exception handling contract.

      Type Parameters:
      T - The type of the future's result.
      Parameters:
      future - The CompletableFuture to join.
      Returns:
      The result of the future.
      Throws:
      Throwable - the unwrapped cause if the future completed exceptionally (exact type preserved).