Package it.tidalwave.role.spi
Class DefaultContextManager
- java.lang.Object
-
- it.tidalwave.role.spi.DefaultContextManager
-
- All Implemented Interfaces:
ContextManager
public class DefaultContextManager extends java.lang.Object implements ContextManager
- Author:
- Fabrizio Giudici
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface it.tidalwave.role.ContextManager
ContextManager.Locator, ContextManager.Supplier<V>
-
-
Constructor Summary
Constructors Constructor Description DefaultContextManager()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddGlobalContext(java.lang.Object context)Adds a global context.voidaddLocalContext(java.lang.Object context)Adds a local context.<T> TfindContextOfType(java.lang.Class<T> contextType)Finds a current context instance of the given type.java.util.List<java.lang.Object>getContexts()Returns the list of current contexts, ordered by their priority.voidremoveGlobalContext(java.lang.Object context)Removes a global context.voidremoveLocalContext(java.lang.Object context)Removes a local context.<V> VrunWithContext(java.lang.Object context, ContextManager.Supplier<V> task)Runs a task associated with a new local context.<V,T extends java.lang.Throwable>
VrunWithContext(java.lang.Object context, Task<V,T> task)Runs aTaskassociated with a new local context.<V> VrunWithContexts(java.util.List<java.lang.Object> contexts, ContextManager.Supplier<V> task)Runs a task associated with a new bunch of local contexts.<V,T extends java.lang.Throwable>
VrunWithContexts(java.util.List<java.lang.Object> contexts, Task<V,T> task)Runs aTaskassociated with a new bunch of local contexts.
-
-
-
Method Detail
-
getContexts
@Nonnull public java.util.List<java.lang.Object> getContexts()
Returns the list of current contexts, ordered by their priority.- Specified by:
getContextsin interfaceContextManager- Returns:
- the list of current contexts
-
findContextOfType
@Nonnull public <T> T findContextOfType(@Nonnull java.lang.Class<T> contextType) throws NotFoundExceptionFinds a current context instance of the given type.- Specified by:
findContextOfTypein interfaceContextManager- 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:
addGlobalContextin interfaceContextManager- Parameters:
context- the new context
-
removeGlobalContext
public void removeGlobalContext(@Nonnull java.lang.Object context)Removes a global context.- Specified by:
removeGlobalContextin interfaceContextManager- Parameters:
context- the context
-
addLocalContext
public void addLocalContext(@Nonnull java.lang.Object context)Adds a local context.- Specified by:
addLocalContextin interfaceContextManager- Parameters:
context- the new context
-
removeLocalContext
public void removeLocalContext(@Nonnull java.lang.Object context)Removes a local context.- Specified by:
removeLocalContextin interfaceContextManager- 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.ThrowableRuns aTaskassociated with a new local context.- Specified by:
runWithContextin interfaceContextManager- Type Parameters:
V- the type of the returned valueT- the type of the exception that can be thrown- Parameters:
context- the contexttask- the task- Returns:
- the value produced by the task
- Throws:
T- the exception(s) thrown by the taskT 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.ThrowableRuns aTaskassociated with a new bunch of local contexts.- Specified by:
runWithContextsin interfaceContextManager- Type Parameters:
V- the type of the returned valueT- the type of the exception that can be thrown- Parameters:
contexts- the contextstask- the task- Returns:
- the value produced by the task
- Throws:
T- the exception(s) thrown by the taskT 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:
runWithContextin interfaceContextManager- Type Parameters:
V- the type of the returned value of the task- Parameters:
context- the contexttask- 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:
runWithContextsin interfaceContextManager- Type Parameters:
V- the type of the returned value- Parameters:
contexts- the contexttask- the task- Returns:
- the value produced by the task
-
-