Class CompletableFutures

java.lang.Object
com.ocient.util.CompletableFutures

public class CompletableFutures extends Object
  • Method Details

    • voidfuture

      public static CompletableFuture<Void> voidfuture()
    • success

      public static <T, E extends Exception> CompletableFutures.Result<T,E> success(T item)
      Returns a new instance of CompletableFutures.Result that represents a successful computation with the specified result.
      Type Parameters:
      T - the type of the result
      E - the type of the exception that may be thrown during the computation
      Parameters:
      item - the result of the computation
      Returns:
      a new instance of Result that represents a successful computation with the specified result
    • fail

      public static <T, E extends Exception> CompletableFutures.Result<T,E> fail(E e)
      Returns a new instance of CompletableFutures.Result that represents a failed computation with the specified exception.
      Type Parameters:
      T - the type of the result
      E - the type of the exception that caused the computation to fail
      Parameters:
      e - the exception that caused the computation to fail
      Returns:
      a new instance of Result that represents a failed computation with the specified exception
    • marshalSQLException

      public static SQLException marshalSQLException(Throwable e)
      Marshals a given Throwable into an SQLException. If the given Throwable is already an SQLException, it is returned as-is. Otherwise, a new SQLException is created with the given Throwable as its cause.
      Parameters:
      e - the Throwable to marshal into an SQLException
      Returns:
      the SQLException marshaled from the given Throwable
    • marshalThrowable

      public static <E extends Exception> E marshalThrowable(Throwable e, Class<? extends E> clazz, Function<Throwable,E> f)
      Marshals a given Throwable into an E. If the given Throwable is already an E, it is returned as-is. Otherwise, a new E is created with the given Throwable as its cause.
      Parameters:
      e - the Throwable to marshal into an Exception
      Returns:
      the Exception marshaled from the given Throwable
    • unwrapThrowable

      public static Throwable unwrapThrowable(Throwable t, Class<?>... clazz)
      Unwraps the Throwable t, returning the underlying root cause.

      Returns t if:

      • t is not wrapped in a clazz throwable
      • OR no root cause is found
      Parameters:
      t - the throwable to unwrap
      clazz - the class representing the wrapping throwable type
      Returns:
      the unwrapped
    • blockingGet

      public static <T> T blockingGet(CompletableFuture<T> future, long timeout, TimeUnit unit) throws IOException, SQLException
      Returns the result value when complete. If the future completes exceptionally, an IOException is thrown.
      Type Parameters:
      T - the result type
      Parameters:
      future - the future
      timeout - the timeout value
      unit - the timeout unit
      Returns:
      the result
      Throws:
      IOException
      CompletionException
      CancellationException
      SQLException
    • blockingGet

      public static <T> T blockingGet(CompletionStage<T> stage) throws IOException, SQLException
      Throws:
      IOException
      SQLException
    • blockingGet

      public static <T> T blockingGet(CompletableFuture<T> future) throws IOException, SQLException
      Returns the result value when complete. If the future completes exceptionally, an IOException is thrown.
      Type Parameters:
      T - the result type
      Parameters:
      future - the future
      Returns:
      the result
      Throws:
      IOException
      CompletionException
      CancellationException
      SQLException
    • blockingGetResult

      public static <T, E extends Exception> CompletableFutures.Result<T,E> blockingGetResult(CompletionStage<? extends CompletableFutures.Result<T,E>> stage, Function<Throwable,E> onMarshal)
      Blocks until the specified CompletionStage completes and returns its result as a CompletableFutures.Result instance. If the CompletionStage results in an exception, the exception is propagated as the cause of a failed result.
      Type Parameters:
      T - the type of the result
      E - the type of the exception
      Parameters:
      stage - the CompletionStage to block on
      onMarshal - a function that takes the caught exception and returns an instance of E
      Returns:
      the result of the CompletionStage as a Result instance, or a failed Result instance if the completion of the CompletionStage resulted in an exception
    • blockingGetSQLResult

      public static <T> CompletableFutures.SQLResult<T> blockingGetSQLResult(CompletionStage<CompletableFutures.SQLResult<T>> stage)
      Blocks until the specified CompletionStage completes and returns its result as a CompletableFutures.SQLResult instance. If the completion of the CompletionStage results in an exception, the exception is caught and used as the cause of a failed SQLResult instance.
      Type Parameters:
      T - the type of the result
      Parameters:
      stage - the CompletionStage to block on
      Returns:
      the result of the CompletionStage as a SQLResult instance, or a failed SQLResult instance if the completion of the CompletionStage resulted in an exception
    • blockingGetIOResult

      public static <T> CompletableFutures.IOResult<T> blockingGetIOResult(CompletionStage<CompletableFutures.IOResult<T>> stage)
      Blocks until the specified CompletionStage completes and returns its result as an CompletableFutures.IOResult instance. If the completion of the CompletionStage results in an exception, the exception is caught and used as the cause of a failed IOResult instance.
      Type Parameters:
      T - the type of the result
      Parameters:
      stage - the CompletionStage to block on
      Returns:
      the result of the CompletionStage as an IOResult instance, or a failed IOResult instance if the completion of the CompletionStage resulted in an exception
    • blockingGetTransportResult

      public static <T> TransportResult<T> blockingGetTransportResult(CompletionStage<TransportResult<T>> stage)
      Blocks until the specified CompletionStage completes and returns its result as an TransportResult instance. If the completion of the CompletionStage results in an exception, the exception is caught and used as the cause of a failed TransportResult instance.
      Type Parameters:
      T - the type of the result
      Parameters:
      stage - the CompletionStage to block on
      Returns:
      the result of the CompletionStage as an TransportResult instance, or a failed TransportResult instance if the completion of the CompletionStage resulted in an exception
    • sleep

      public static CompletionStage<Void> sleep(long timeout, TimeUnit unit)
      Returns a future that will complete after the specified amount of time.