Package dev.failsafe

Class Functions


  • final class Functions
    extends java.lang.Object
    Utilities for creating and applying Failsafe executable functions.
    • Constructor Detail

      • Functions

        Functions()
    • Method Detail

      • get

        static <R> java.util.function.Function<SyncExecutionImpl<R>,​ExecutionResult<R>> get​(ContextualSupplier<R,​R> supplier,
                                                                                                  java.util.concurrent.Executor executor)
        Returns a Supplier for synchronous executions that pre-executes the execution, applies the supplier, records the result and returns the result. This implementation also handles Thread interrupts.
        Type Parameters:
        R - result type
      • getPromise

        static <R> java.util.function.Function<AsyncExecutionInternal<R>,​java.util.concurrent.CompletableFuture<ExecutionResult<R>>> getPromise​(ContextualSupplier<R,​R> supplier,
                                                                                                                                                      java.util.concurrent.Executor executor)
        Returns a Function for asynchronous executions that pre-executes the execution, applies the supplier, records the result and returns a promise containing the result.
        Type Parameters:
        R - result type
      • getPromiseExecution

        static <R> java.util.function.Function<AsyncExecutionInternal<R>,​java.util.concurrent.CompletableFuture<ExecutionResult<R>>> getPromiseExecution​(AsyncRunnable<R> runnable,
                                                                                                                                                               java.util.concurrent.Executor executor)
        Returns a Function for asynchronous executions that pre-executes the execution, runs the runnable, and attempts to complete the execution if a failure occurs. Locks to ensure the resulting supplier cannot be applied multiple times concurrently.
        Type Parameters:
        R - result type
      • getPromiseOfStage

        static <R> java.util.function.Function<AsyncExecutionInternal<R>,​java.util.concurrent.CompletableFuture<ExecutionResult<R>>> getPromiseOfStage​(ContextualSupplier<R,​? extends java.util.concurrent.CompletionStage<? extends R>> supplier,
                                                                                                                                                             FailsafeFuture<R> future,
                                                                                                                                                             java.util.concurrent.Executor executor)
        Returns a Function that for asynchronous executions that pre-executes the execution, applies the supplier, records the result and returns a promise containing the result.
        Type Parameters:
        R - result type
        Throws:
        java.lang.UnsupportedOperationException - when using
      • toExecutionAware

        static <R> java.util.function.Function<AsyncExecutionInternal<R>,​java.util.concurrent.CompletableFuture<ExecutionResult<R>>> toExecutionAware​(java.util.function.Function<AsyncExecutionInternal<R>,​java.util.concurrent.CompletableFuture<ExecutionResult<R>>> innerFn)
        Returns a Function that returns an execution result if one was previously recorded, else applies the innerFn.
        Type Parameters:
        R - result type
      • toAsync

        static <R> java.util.function.Function<AsyncExecutionInternal<R>,​java.util.concurrent.CompletableFuture<ExecutionResult<R>>> toAsync​(java.util.function.Function<AsyncExecutionInternal<R>,​java.util.concurrent.CompletableFuture<ExecutionResult<R>>> innerFn,
                                                                                                                                                   Scheduler scheduler,
                                                                                                                                                   FailsafeFuture<R> future)
        Returns a Function that asynchronously applies the innerFn on the first call, synchronously on subsequent calls, and returns a promise containing the result.
        Type Parameters:
        R - result type
      • withExecutor

        static <R> AsyncRunnable<R> withExecutor​(AsyncRunnable<R> runnable,
                                                 java.util.concurrent.Executor executor)
      • handleExecutorThrowable

        private static void handleExecutorThrowable​(java.lang.Throwable e)