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
Nested Classes Modifier and Type Interface Description static class
ContextManager.Binder
Used bystatic class
ContextManager.Inner
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 Static 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> java.util.Optional<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.static ContextManager
getInstance()
void
removeGlobalContext(java.lang.Object context)
Removes a global context.void
removeLocalContext(java.lang.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 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.static void
set(ContextManagerProvider provider)
This method is for testing only. Sets the globalContextManagerProvider
.
-
-
-
Method Detail
-
getInstance
@Nonnull static ContextManager getInstance()
-
set
static void set(@Nonnull ContextManagerProvider provider)
This method is for testing only. Sets the globalContextManagerProvider
. See note aboutreset()
.- Parameters:
provider
- the provider- See Also:
reset()
-
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:
set(ContextManagerProvider)
-
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> java.util.Optional<T> findContextOfType(@Nonnull java.lang.Class<T> contextType)
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
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
-
-