public interface ContextManager
A facility to register and unregister global and local DCI contexts.
- Author:
- Fabrizio Giudici
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classA binder of local contexts, created bybinder(Object...)and meant to be used in a try-with-resources: it binds the contexts to the current thread when it's created and unbinds them when it's closed.static classA locator for theContextManagerwhich uses theServiceLoaderfacility to be independent of any DI framework.static interfaceA variant ofRunnablethat can throw a checked exception.static interfaceContextManager.SupplierWithException<T,E extends Throwable> A variant ofSupplierthat can throw a checked exception. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddGlobalContext(Object context) Adds a global context.voidaddLocalContext(Object context) Adds a local context.default ContextManager.BinderCreates 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 ContextManagerReturns a singleton instance.static booleanReturnstrueif the singleton has already been instantiated.voidremoveGlobalContext(Object context) Removes a global context.voidremoveLocalContext(Object context) Removes a local context.static voidreset()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> VrunWithContext(Object context, Supplier<V> task) Deprecated.default voidrunWithContexts(Runnable runnable, Object... contexts) Calls a runnable with some local contexts.default <T> TrunWithContexts(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> VrunWithContexts(List<Object> contexts, Supplier<V> task) Deprecated.static voidset(ContextManagerProvider provider) This method is for testing only. Sets the globalContextManagerProvider.
-
Method Details
-
getInstance
Returns a singleton instance.- Returns:
- the singleton instance
-
isInstantiated
static boolean isInstantiated()Returnstrueif the singleton has already been instantiated.. This method never instantiates it.- Returns:
trueif the singleton has already been instantiated- Since:
- 5.0-ALPHA-4
-
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 aTaskassociated 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 aTaskassociated 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...)