Interface ContextManager


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

    • getInstance

      @Nonnull static ContextManager getInstance()
    • set

      static void set(@Nonnull ContextManagerProvider provider)
      This method is for testing only. Sets the global ContextManagerProvider. See note about reset().
      Parameters:
      provider - the provider
      See Also:
    • reset

      static void reset()
      This method is for testing only. Resets the global ContextManagerProvider; it must be called at the test completion whenever set(ContextManagerProvider) has been called, to avoid polluting the context of further tests.
      See Also:
    • getContexts

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

      @Nonnull <T> Optional<T> findContextOfType(@Nonnull 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 Object context)
      Adds a global context.
      Parameters:
      context - the new context
    • removeGlobalContext

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

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

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

      @Deprecated default <V, T extends Throwable> V runWithContext(@Nonnull Object context, @Nonnull Task<V,T> task) throws T
      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
    • runWithContexts

      @Deprecated default <V, T extends Throwable> V runWithContexts(@Nonnull List<Object> contexts, @Nonnull Task<V,T> task) throws T
      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
    • runWithContext

      @Deprecated default <V> V runWithContext(@Nonnull Object context, @Nonnull 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 List<Object> contexts, @Nonnull 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 Runnable runnable, @Nonnull 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 Supplier<T> supplier, @Nonnull 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 Throwable> void runEWithContexts(@Nonnull ContextManager.RunnableWithException<E> runnable, @Nonnull Object... contexts) throws E
      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
      Since:
      3.2-ALPHA-12
    • runEWithContexts

      @Nonnull <T, E extends Throwable> T runEWithContexts(@Nonnull ContextManager.SupplierWithException<T,E> task, @Nonnull Object... contexts) throws E
      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
      Since:
      3.2-ALPHA-12
    • binder

      @Nonnull default ContextManager.Binder binder(@Nonnull 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