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.Binder
Used bystatic class
ContextManager.Locator
A locator for theContextManager
which uses theServiceLoader
facility to be independent of any DI framework.static interface
ContextManager.RunnableWithException<E extends java.lang.Throwable>
static interface
ContextManager.SupplierWithException<T,E extends java.lang.Throwable>
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated 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.default ContextManager.Binder
binder(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.<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.default <E extends java.lang.Throwable>
voidrunEWithContexts(ContextManager.RunnableWithException<E> runnable, java.lang.Object... contexts)
Calls a runnable with some local contexts.<T,E extends java.lang.Throwable>
TrunEWithContexts(ContextManager.SupplierWithException<T,E> task, java.lang.Object... contexts)
Calls a task with some local contexts.default <V,T extends java.lang.Throwable>
VrunWithContext(java.lang.Object context, Task<V,T> task)
Deprecated.default <V> V
runWithContext(java.lang.Object context, java.util.function.Supplier<V> task)
Deprecated.default void
runWithContexts(java.lang.Runnable runnable, java.lang.Object... contexts)
Calls a runnable with some local contexts.default <T> T
runWithContexts(java.util.function.Supplier<T> supplier, java.lang.Object... contexts)
Calls a supplier with some local contexts.default <V,T extends java.lang.Throwable>
VrunWithContexts(java.util.List<java.lang.Object> contexts, Task<V,T> task)
Deprecated.default <V> V
runWithContexts(java.util.List<java.lang.Object> contexts, java.util.function.Supplier<V> task)
Deprecated.
-
-
-
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
@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
Deprecated.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
@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
Deprecated.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
@Deprecated default <V> V runWithContext(@Nonnull java.lang.Object context, @Nonnull java.util.function.Supplier<V> task)
Deprecated.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
@Deprecated default <V> V runWithContexts(@Nonnull java.util.List<java.lang.Object> contexts, @Nonnull java.util.function.Supplier<V> task)
Deprecated.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 contextstask
- 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 runnablecontexts
- 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 suppliercontexts
- 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 callcontexts
- the contexts- Throws:
E
- the original exception thrown by taskE 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 valueE
- the type of the thrown exception- Parameters:
task
- the task to callcontexts
- the contexts- Returns:
- the value returned by the supplier
- Throws:
E
- the original exception thrown by taskE 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
-
-