Package it.tidalwave.util
Interface ContextManager
public interface ContextManager
A facility to register and unregister global and local DCI contexts.
- Author:
- Fabrizio Giudici
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic class
Used bystatic class
A locator for theContextManager
which uses theServiceLoader
facility to be independent of any DI framework.static interface
static interface
ContextManager.SupplierWithException<T,
E extends Throwable> -
Method Summary
Modifier and TypeMethodDescriptionvoid
addGlobalContext
(Object context) Adds a global context.void
addLocalContext
(Object context) Adds a local context.default ContextManager.Binder
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> Optional
<T> findContextOfType
(Class<T> contextType) Finds a current context instance of the given type.Returns the list of current contexts, ordered by their priority.static ContextManager
void
removeGlobalContext
(Object context) Removes a global context.void
removeLocalContext
(Object context) Removes a local context.static void
reset()
This method is for testing only. Resets the globalContextManagerProvider
; it must be called at the test completion wheneverset(ContextManagerProvider)
has been called, to avoid polluting the context of further tests.default <E extends Throwable>
voidrunEWithContexts
(ContextManager.RunnableWithException<E> runnable, Object... contexts) Calls a runnable with some local contexts.<T,
E extends Throwable>
TrunEWithContexts
(ContextManager.SupplierWithException<T, E> task, Object... contexts) Calls a task with some local contexts.default <V,
T extends Throwable>
VrunWithContext
(Object context, Task<V, T> task) Deprecated.default <V> V
runWithContext
(Object context, Supplier<V> task) Deprecated.default void
runWithContexts
(Runnable runnable, Object... contexts) Calls a runnable with some local contexts.default <T> T
runWithContexts
(Supplier<T> supplier, Object... contexts) Calls a supplier with some local contexts.default <V,
T extends Throwable>
VrunWithContexts
(List<Object> contexts, Task<V, T> task) Deprecated.default <V> V
runWithContexts
(List<Object> contexts, Supplier<V> task) Deprecated.static void
set
(ContextManagerProvider provider) This method is for testing only. Sets the globalContextManagerProvider
.
-
Method Details
-
getInstance
-
set
This method is for testing only. Sets the globalContextManagerProvider
. See note aboutreset()
.- Parameters:
provider
- the provider- See Also:
-
reset
static void reset()This method is for testing only. Resets the globalContextManagerProvider
; it must be called at the test completion wheneverset(ContextManagerProvider)
has been called, to avoid polluting the context of further tests.- See Also:
-
getContexts
Returns the list of current contexts, ordered by their priority.- Returns:
- the list of current contexts
-
findContextOfType
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
Adds a global context.- Parameters:
context
- the new context
-
removeGlobalContext
Removes a global context.- Parameters:
context
- the context
-
addLocalContext
Adds a local context.- Parameters:
context
- the new context
-
removeLocalContext
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 TDeprecated.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 task
-
runWithContexts
@Deprecated default <V,T extends Throwable> V runWithContexts(@Nonnull List<Object> contexts, @Nonnull Task<V, T> task) throws TDeprecated.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 task
-
runWithContext
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 List<Object> contexts, @Nonnull 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
Calls a runnable with some local contexts. This method fits functional interfaces.- Parameters:
runnable
- the runnablecontexts
- the contexts- Since:
- 3.2-ALPHA-12
-
runWithContexts
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 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 callcontexts
- 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 ECalls 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 task- Since:
- 3.2-ALPHA-12
-
binder
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
-
runWithContexts(Runnable, Object...)
orrunWithContexts(Supplier, Object...)