Package it.tidalwave.role
Interface ContextManager
-
- All Known Implementing Classes:
DefaultContextManager
public interface ContextManager
A facility to register and unregister global and local DCI contexts.- Author:
- Fabrizio Giudici
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
ContextManager.Locator
A locator for theContextManager
which uses theServiceLoader
facility to be independent of any DI framework.static interface
ContextManager.Supplier<V>
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
addGlobalContext(java.lang.Object context)
Adds a global context.void
addLocalContext(java.lang.Object context)
Adds a local context.<T> T
findContextOfType(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.void
removeGlobalContext(java.lang.Object context)
Removes a global context.void
removeLocalContext(java.lang.Object context)
Removes a local context.<V> V
runWithContext(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 aTask
associated with a new local context.<V> V
runWithContexts(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 aTask
associated with a new bunch of local contexts.
-
-
-
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> T findContextOfType(@Nonnull java.lang.Class<T> contextType) throws NotFoundException
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
- Throws:
NotFoundException
- if no context of that type is found
-
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
<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 aTask
associated with a new local context.- 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
<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 aTask
associated with a new bunch of local contexts.- 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
<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.- 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
<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.- Type Parameters:
V
- the type of the returned value- Parameters:
contexts
- the contexttask
- the task- Returns:
- the value produced by the task
-
-