Class NameableCompletableFuture<T>

  • All Implemented Interfaces:
    java.util.concurrent.CompletionStage<T>

    public class NameableCompletableFuture<T>
    extends NameableCompletionStage<T>
    Naming API for CompletableFuture (Java 11).

    Note: NameableCompletableFuture wrapper is NOT a CompletableFuture, but extends NameableCompletionStage for fluent naming API. Use getUnderlying or toCompletableFuture to get a CompletableFuture again.

    • Constructor Detail

      • NameableCompletableFuture

        public NameableCompletableFuture​(java.util.concurrent.CompletableFuture<T> underlying)
        Create a new CompletableFuture with nameable methods.
        Parameters:
        underlying - the CompletableFuture to wrap with nameable methods
    • Method Detail

      • currentName

        public static java.lang.String currentName()
        Get the current CompletableFuture / completion name.
        Returns:
        the current name (thread locally)
      • withName

        public static <F> F withName​(java.lang.String name,
                                     java.util.function.Supplier<F> f)
        Add a name around CompletableFuture creation or completion callbacks.
        Type Parameters:
        F - the return type of the supplied block
        Parameters:
        name - the name for the CompletableFuture and/or completion callback
        f - the supplied block to name (with newly created CompletableFutures)
        Returns:
        the result of the supplied block
      • newNamed

        public static <U> java.util.concurrent.CompletableFuture<U> newNamed​(java.lang.String name)
        Named version of new CompletableFuture<T>.
        Type Parameters:
        U - the CompletableFuture's type
        Parameters:
        name - the name for the CompletableFuture
        Returns:
        the named CompletableFuture
      • supplyAsyncNamed

        public static <U> java.util.concurrent.CompletableFuture<U> supplyAsyncNamed​(java.lang.String name,
                                                                                     java.util.function.Supplier<U> supplier)
        Named version of CompletableFuture.supplyAsync(supplier).
        Type Parameters:
        U - the function's return type
        Parameters:
        name - the name for the CompletableFuture
        supplier - the supplier for CompletableFuture.supplyAsync
        Returns:
        the named CompletableFuture
      • supplyAsyncNamed

        public static <U> java.util.concurrent.CompletableFuture<U> supplyAsyncNamed​(java.lang.String name,
                                                                                     java.util.function.Supplier<U> supplier,
                                                                                     java.util.concurrent.Executor executor)
        Named version of CompletableFuture.supplyAsync(supplier, executor).
        Type Parameters:
        U - the function's return type
        Parameters:
        name - the name for the CompletableFuture
        supplier - the supplier for CompletableFuture.supplyAsync
        executor - the executor to use for asynchronous execution
        Returns:
        the named CompletableFuture
      • runAsyncNamed

        public static java.util.concurrent.CompletableFuture<java.lang.Void> runAsyncNamed​(java.lang.String name,
                                                                                           java.lang.Runnable runnable)
        Named version of CompletableFuture.runAsync(runnable).
        Parameters:
        name - the name for the CompletableFuture
        runnable - the runnable for CompletableFuture.runAsync
        Returns:
        the named CompletableFuture
      • runAsyncNamed

        public static java.util.concurrent.CompletableFuture<java.lang.Void> runAsyncNamed​(java.lang.String name,
                                                                                           java.lang.Runnable runnable,
                                                                                           java.util.concurrent.Executor executor)
        Named version of CompletableFuture.runAsync(runnable, executor).
        Parameters:
        name - the name for the CompletableFuture
        runnable - the runnable for CompletableFuture.runAsync
        executor - the executor to use for asynchronous execution
        Returns:
        the named CompletableFuture
      • completedFutureNamed

        public static <U> java.util.concurrent.CompletableFuture<U> completedFutureNamed​(java.lang.String name,
                                                                                         U value)
        Named version of CompletableFuture.completedFuture(value).
        Type Parameters:
        U - the type of the value
        Parameters:
        name - the name for the CompletableFuture
        value - the value for CompletableFuture.completedFuture
        Returns:
        the named and completed CompletableFuture
      • getUnderlying

        public java.util.concurrent.CompletableFuture<T> getUnderlying()
        Return the underlying CompletableFuture wrapped by this NameableCompletableFuture.
        Overrides:
        getUnderlying in class NameableCompletionStage<T>
        Returns:
        the underlying CompletableFuture
      • nameable

        public static <U> NameableCompletableFuture<U> nameable​(java.util.concurrent.CompletableFuture<U> future)
        Create a NameableCompletableFuture wrapper with nameable methods.
        Type Parameters:
        U - the underlying CompletableFuture's type
        Parameters:
        future - the CompletableFuture to extend with named variations
        Returns:
        a NameableCompletableFuture with nameable completion methods
      • newIncompleteFuture

        public <U> NameableCompletableFuture<U> newIncompleteFuture()
        Original version of CompletableFuture.newIncompleteFuture, but returns a NameableCompletableFuture where subsequent method calls can use named variations.
        Type Parameters:
        U - the type of the value
        Returns:
        the new (unnamed) incomplete CompletableFuture
      • newIncompleteFutureNamed

        public <U> NameableCompletableFuture<U> newIncompleteFutureNamed​(java.lang.String name)
        Named version of CompletableFuture.newIncompleteFuture.
        Type Parameters:
        U - the type of the value
        Returns:
        the new (named) incomplete CompletableFuture
      • copy

        public NameableCompletableFuture<T> copy()
        Original version of CompletableFuture.copy, but returns a NameableCompletableFuture where subsequent method calls can use named variations.
        Returns:
        the new (unnamed) copied CompletableFuture
      • copyNamed

        public NameableCompletableFuture<T> copyNamed​(java.lang.String name)
        Named version of CompletableFuture.copy.
        Returns:
        the new (named) copied CompletableFuture
      • minimalCompletionStage

        public NameableCompletionStage<T> minimalCompletionStage()
        Original version of CompletableFuture.minimalCompletionStage, but returns a NameableCompletionStage where subsequent method calls can use named variations.
        Returns:
        the new (unnamed) copied CompletableFuture
      • minimalCompletionStageNamed

        public NameableCompletionStage<T> minimalCompletionStageNamed​(java.lang.String name)
        Named version of CompletableFuture.minimalCompletionStage.
        Returns:
        the new (named) copied CompletableFuture
      • completedStageNamed

        public static <U> java.util.concurrent.CompletionStage<U> completedStageNamed​(java.lang.String name,
                                                                                      U value)
        Named version of CompletableFuture.completedStage(value).
        Type Parameters:
        U - the type of the value
        Parameters:
        name - the name for the CompletionStage
        value - the value for CompletableFuture.completedStage
        Returns:
        the named and completed CompletionStage
      • failedFutureNamed

        public static <U> java.util.concurrent.CompletableFuture<U> failedFutureNamed​(java.lang.String name,
                                                                                      java.lang.Throwable ex)
        Named version of CompletableFuture.failedFuture(ex).
        Type Parameters:
        U - the type of the value
        Parameters:
        name - the name for the CompletableFuture
        ex - the exception for CompletableFuture.failedFuture
        Returns:
        the named and exceptionally completed CompletableFuture
      • failedStageNamed

        public static <U> java.util.concurrent.CompletionStage<U> failedStageNamed​(java.lang.String name,
                                                                                   java.lang.Throwable ex)
        Named version of CompletableFuture.failedStage(ex).
        Type Parameters:
        U - the type of the value
        Parameters:
        name - the name for the CompletionStage
        ex - the exception for CompletableFuture.failedStage
        Returns:
        the named and exceptionally completed CompletionStage
      • disableContext

        public static <T> java.util.concurrent.CompletableFuture<T> disableContext​(java.util.concurrent.CompletableFuture<T> future)
        Disable any Cinnamon attached context on the given CompletableFuture.

        Can be used if the CompletableFuture will be cached.

        Type Parameters:
        T - the type of the value
        Parameters:
        future - the CompletableFuture to disable contexts for
        Returns:
        the original CompletableFuture with context disabled