Class DefaultContextManager

  • All Implemented Interfaces:
    ContextManager

    public class DefaultContextManager
    extends java.lang.Object
    implements ContextManager
    Author:
    Fabrizio Giudici
    • Constructor Detail

      • DefaultContextManager

        public DefaultContextManager()
    • Method Detail

      • getContexts

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

        @Nonnull
        public <T> T findContextOfType​(@Nonnull
                                       java.lang.Class<T> contextType)
                                throws NotFoundException
        Finds a current context instance of the given type.
        Specified by:
        findContextOfType in interface ContextManager
        Type Parameters:
        T - the static context type
        Parameters:
        contextType - the dynamic context type
        Returns:
        the requested context
        Throws:
        NotFoundException - if no context of that type is found
      • addGlobalContext

        public void addGlobalContext​(@Nonnull
                                     java.lang.Object context)
        Adds a global context.
        Specified by:
        addGlobalContext in interface ContextManager
        Parameters:
        context - the new context
      • removeGlobalContext

        public void removeGlobalContext​(@Nonnull
                                        java.lang.Object context)
        Removes a global context.
        Specified by:
        removeGlobalContext in interface ContextManager
        Parameters:
        context - the context
      • addLocalContext

        public void addLocalContext​(@Nonnull
                                    java.lang.Object context)
        Adds a local context.
        Specified by:
        addLocalContext in interface ContextManager
        Parameters:
        context - the new context
      • removeLocalContext

        public void removeLocalContext​(@Nonnull
                                       java.lang.Object context)
        Removes a local context.
        Specified by:
        removeLocalContext in interface ContextManager
        Parameters:
        context - the context
      • runWithContext

        @Nonnull
        public <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.
        Specified by:
        runWithContext in interface ContextManager
        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

        @Nonnull
        public <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.
        Specified by:
        runWithContexts in interface ContextManager
        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

        public <V> V runWithContext​(@Nonnull
                                    java.lang.Object context,
                                    @Nonnull
                                    ContextManager.Supplier<V> task)
        Runs a task associated with a new local context. This variant fits functional interfaces.
        Specified by:
        runWithContext in interface ContextManager
        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

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