Interface Context
-
- All Known Subinterfaces:
ContextualRegistry
public interface ContextA registry for context objects. Enables instance localization between several services / components / ... integrated in a particular known scope. ContextualRegistry instance is intended to be associated with a scope aware object such as WebServer, ServerRequest or ClientRequest.Context contains also a notion of classifiers. Classifier is any object defining additional key for registered objects. To obtain such registered object, the same classifier (precisely, any equal object) has to be used.
Classifiers can be used as follows:
- As an additional identifier for registered objects of common types, like a
String, ...
// User detail provider service registry.register("NAME_PARAM_ID", "Smith"); registry.register("GENDER_PARAM_ID", "male"); ... // User consumer service String name = registry.get("name", String.class); - As an access control mechanism where only owners of the classifier can retrieve such contextual instance.
// In some central security service. registry.register(securityFrameworkInternalInstance, new AuthenticatedInternalIdentity(...)); ... // In some authorization filter known by a central security service AuthenticatedInternalIdentity auth = registry.get(securityFrameworkInternalInstance, AuthenticatedInternalIdentity.class);
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classContext.BuilderFluent API builder forContext.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description static Context.Builderbuilder()Fluent API builder for advanced configuration.static Contextcreate()Creates a new empty instance.static Contextcreate(Context parent)Creates a new empty instance backed by its parent read-throughContext.<T> Optional<T>get(Class<T> type)Optionally gets registered instance by its type.<T> Optional<T>get(Object classifier, Class<T> type)Optionally gets a registered instance by its type.Stringid()A unique id of this context within this runtime.<T> voidregister(Object classifier, T instance)Register a new instance with specified classifier.<T> voidregister(T instance)Register a new instance.<T> voidsupply(Class<T> type, Supplier<T> supplier)Register a new instance using a provided supplier.<T> voidsupply(Object classifier, Class<T> type, Supplier<T> supplier)Registers a new instance using a provided supplier.
-
-
-
Method Detail
-
create
static Context create()
Creates a new empty instance.- Returns:
- new instance
-
create
static Context create(Context parent)
Creates a new empty instance backed by its parent read-throughContext.Parent
registryis used only for get methods and only if this registry doesn't have registered required type.- Parameters:
parent- a parent registry- Returns:
- new instance
-
builder
static Context.Builder builder()
Fluent API builder for advanced configuration.- Returns:
- a new builder
-
register
<T> void register(T instance)
Register a new instance.- Type Parameters:
T- a type of the registered instance- Parameters:
instance- an instance to register- Throws:
NullPointerException- if the registered object isnull
-
supply
<T> void supply(Class<T> type, Supplier<T> supplier)
Register a new instance using a provided supplier. The supplier is guaranteed to be called at most once when it's requested by theget(Class)method. The returned value is then registered and the supplier is never used again.- Type Parameters:
T- a type of supplied object- Parameters:
type- a type of supplied instancesupplier- a supplier of the instance to register- Throws:
NullPointerException- if thetypeor thesupplierisnull
-
get
<T> Optional<T> get(Class<T> type)
Optionally gets registered instance by its type.More specifically, it returns the last registered instance without specified classifier which can be cast to the requested type.
- Type Parameters:
T- a type of requested instance- Parameters:
type- a type of requested instance- Returns:
- The last registered instance compatible with the specified type
-
register
<T> void register(Object classifier, T instance)
Register a new instance with specified classifier.Registered instance can be obtained only using
get(Object, Class)method with aclassifierequal with the one used during registration.- Type Parameters:
T- a type of the registered instance- Parameters:
classifier- an additional registered instance classifierinstance- an instance to register- Throws:
NullPointerException- ifclassifieror registered object isnull
-
supply
<T> void supply(Object classifier, Class<T> type, Supplier<T> supplier)
Registers a new instance using a provided supplier. The supplier is guarantied to be called at most once when it's requested by theget(Object, Class)method. The returned value gets registered and the supplier is never called again.Registered instance can be obtained only using
get(Object, Class)method with aclassifierequal with the one used during registration.- Type Parameters:
T- a type of supplied object- Parameters:
classifier- an additional registered instance classifiertype- a type of requested instancesupplier- a supplier of the instance to register- Throws:
NullPointerException- If any parameter isnull.
-
get
<T> Optional<T> get(Object classifier, Class<T> type)
Optionally gets a registered instance by its type.More specifically, it returns the last registered instance with equal classifier which can be cast to the requested type.
- Type Parameters:
T- a type of requested instance- Parameters:
classifier- an additional registered instance classifiertype- a type of requested instance- Returns:
- the last registered instance compatible with the specified type
- Throws:
NullPointerException- Ifclassifieris null.
-
id
String id()
A unique id of this context within this runtime.- Returns:
- id of this context
-
-