Interface ContextManager


  • public interface ContextManager
    A facility to register and unregister global and local DCI contexts.
    Author:
    Fabrizio Giudici
    • Method Detail

      • getContexts

        @Nonnull
        java.util.List<java.lang.Object> getContexts()
        Returns the list of current contexts, ordered by their priority.
        Returns:
        the list of current contexts
      • findContextOfType

        @Nonnull
        <T> java.util.Optional<T> findContextOfType​(@Nonnull
                                                    java.lang.Class<T> contextType)
        Finds a current context instance of the given type.
        Type Parameters:
        T - the static context type
        Parameters:
        contextType - the dynamic context type
        Returns:
        the requested context
      • addGlobalContext

        void addGlobalContext​(@Nonnull
                              java.lang.Object context)
        Adds a global context.
        Parameters:
        context - the new context
      • removeGlobalContext

        void removeGlobalContext​(@Nonnull
                                 java.lang.Object context)
        Removes a global context.
        Parameters:
        context - the context
      • addLocalContext

        void addLocalContext​(@Nonnull
                             java.lang.Object context)
        Adds a local context.
        Parameters:
        context - the new context
      • removeLocalContext

        void removeLocalContext​(@Nonnull
                                java.lang.Object context)
        Removes a local context.
        Parameters:
        context - the context
      • runWithContext

        @Deprecated
        default <V,​T extends java.lang.Throwable> V runWithContext​(@Nonnull
                                                                         java.lang.Object context,
                                                                         @Nonnull
                                                                         Task<V,​T> task)
                                                                  throws T extends java.lang.Throwable
        Runs a Task associated with a new local context.
        Type Parameters:
        V - the type of the returned value
        T - the type of the exception that can be thrown
        Parameters:
        context - the context
        task - the task
        Returns:
        the value produced by the task
        Throws:
        T - the exception(s) thrown by the task
        T extends java.lang.Throwable
      • runWithContexts

        @Deprecated
        default <V,​T extends java.lang.Throwable> V runWithContexts​(@Nonnull
                                                                          java.util.List<java.lang.Object> contexts,
                                                                          @Nonnull
                                                                          Task<V,​T> task)
                                                                   throws T extends java.lang.Throwable
        Runs a Task associated with a new bunch of local contexts.
        Type Parameters:
        V - the type of the returned value
        T - the type of the exception that can be thrown
        Parameters:
        contexts - the contexts
        task - the task
        Returns:
        the value produced by the task
        Throws:
        T - the exception(s) thrown by the task
        T extends java.lang.Throwable
      • runWithContext

        @Deprecated
        default <V> V runWithContext​(@Nonnull
                                     java.lang.Object context,
                                     @Nonnull
                                     java.util.function.Supplier<V> task)
        Runs a task associated with a new local context. This variant fits functional interfaces.
        Type Parameters:
        V - the type of the returned value of the task
        Parameters:
        context - the context
        task - the task
        Returns:
        the value produced by the task
      • runWithContexts

        @Deprecated
        default <V> V runWithContexts​(@Nonnull
                                      java.util.List<java.lang.Object> contexts,
                                      @Nonnull
                                      java.util.function.Supplier<V> task)
        Runs a task associated with a new bunch of local contexts. This variant fits functional interfaces.
        Type Parameters:
        V - the type of the returned value
        Parameters:
        contexts - the contexts
        task - the task
        Returns:
        the value produced by the task
      • runWithContexts

        default void runWithContexts​(@Nonnull
                                     java.lang.Runnable runnable,
                                     @Nonnull
                                     java.lang.Object... contexts)
        Calls a runnable with some local contexts. This method fits functional interfaces.
        Parameters:
        runnable - the runnable
        contexts - the contexts
        Since:
        3.2-ALPHA-12
      • runWithContexts

        @Nonnull
        default <T> T runWithContexts​(@Nonnull
                                      java.util.function.Supplier<T> supplier,
                                      @Nonnull
                                      java.lang.Object... contexts)
        Calls a supplier with some local contexts. This method fits functional interfaces.
        Type Parameters:
        T - the type of the result
        Parameters:
        supplier - the supplier
        contexts - the contexts
        Returns:
        the value returned by the supplier
        Since:
        3.2-ALPHA-12
      • runEWithContexts

        default <E extends java.lang.Throwable> void runEWithContexts​(@Nonnull
                                                                      ContextManager.RunnableWithException<E> runnable,
                                                                      @Nonnull
                                                                      java.lang.Object... contexts)
                                                               throws E extends java.lang.Throwable
        Calls a runnable with some local contexts. This method fits functional interfaces.
        Type Parameters:
        E - the type of the thrown exception
        Parameters:
        runnable - the runnable to call
        contexts - the contexts
        Throws:
        E - the original exception thrown by task
        E extends java.lang.Throwable
        Since:
        3.2-ALPHA-12
      • runEWithContexts

        @Nonnull
        <T,​E extends java.lang.Throwable> T runEWithContexts​(@Nonnull
                                                                   ContextManager.SupplierWithException<T,​E> task,
                                                                   @Nonnull
                                                                   java.lang.Object... contexts)
                                                            throws E extends java.lang.Throwable
        Calls a task with some local contexts. This method fits functional interfaces.
        Type Parameters:
        T - the type of the returned value
        E - the type of the thrown exception
        Parameters:
        task - the task to call
        contexts - the contexts
        Returns:
        the value returned by the supplier
        Throws:
        E - the original exception thrown by task
        E extends java.lang.Throwable
        Since:
        3.2-ALPHA-12
      • binder

        @Nonnull
        default ContextManager.Binder binder​(@Nonnull
                                             java.lang.Object... contexts)
        Creates a binder that makes it possible to bind a local context by means of a try-with-resources instead of a try/finally.
         try (final ContextManager.Binder binder = contextManager.binder(context))
           {
             ...
           }
         
        Parameters:
        contexts - the contexts
        Returns:
        a binder that can be used in try-with-resources
        Since:
        3.2-ALPHA-12