Class DefaultContext
- All Implemented Interfaces:
ParamContext
- Direct Known Subclasses:
HigsonContext,LevelValues
Implementation of dynamic ParamContext.
Core of default context is the initialization algorithm, that can turn list of loosely provided
values into a meaningful context (DefaultContext(Object[])),
that is later translated into string array of level values (with help of level creators)
returned by ParamContext.getLevelValues().
- Since:
- 1.0.0
- Author:
- Przemek Hertel
-
Constructor Summary
ConstructorsConstructorDescriptionCreate empty context, use setter methods to initialize it.DefaultContext(Object... args) Puts provided values into context using algorithm: -
Method Summary
Modifier and TypeMethodDescriptionprotected DefaultContextadhoc(DefaultContext ctx) <T> TLooks for object of classclazz(or object which class is assignable fromclazz.Return object stored under key.<T> TReturn object stored under key and cast it to given class.Object[][]Return object stored under key as String.Return map representing parameter evaluation context.booleanprotected final voidinitialize(Object... args) Implementation of value initializing algorithm.protected StringModifies key, that will be used in theuserContext.final DefaultContextPut value underlowercase(value.class.getSimpleName())in user context map.final voidsetLevelValues(Object... levelValues) Set level values directly as objects.toString()final DefaultContextPutvalueunderlowercase(key).final DefaultContextPutvalueunder keylowercase(key). allowOverwrite flag determines what happens in case of key collision.withLevelValues(Object... levelValues) withLevelValues(String... levelValues)
-
Constructor Details
-
DefaultContext
Puts provided values into context using algorithm:
- if args[i] is
String[]level values are set using [link setLevelValues(String[])] - if args[i] is
Object[]level values are set using [link setLevelValues(Object[])] - if args[i] is
Stringargs[i+1]value is taken and put into context underargs[i]key using [set(String, Object)] - if args[i] is
Locale, it is set as a locale used for lowercase operations in [set(String, Object, boolean)] - else, setter lookup is performed using (ReflectionSetterFinder) to find any setter of current context object that accepts
args[i] - eventually, args[i] is put into context under its class name using
set(Object)
This mechanism should be used with caution, as sometimes it can produce unexpected (although perfectly valid and deterministic) results. Biggest pitfall is hidden in implementation of
There is one more, power-user property. It is possible to substitute default implementation ofReflectionSetterInvoker.findSetter(Class, Object), which may lead to nondeterministic behavior if used incorrectly. In short, make sure you don't use automatic setter invocation if you need to define two setters that accept same type of object (i.e. two setters for different Date objects).ReflectionSetterInvoker, utility responsible for efficient setter invoking (includes inner cache). DefaultContext keeps default setter invoker as a static property, to share its caching abilities among all instances of DeaultContext. To substitute it with own implementation, pass ReflectionSetterInvoker object as first argument. Passing setter invoker on any other position will have no effect, as it will be treated as a normal argument.- Parameters:
args- object ofObject
- if args[i] is
-
DefaultContext
public DefaultContext()Create empty context, use setter methods to initialize it.
-
-
Method Details
-
initialize
Implementation of value initializing algorithm.- Parameters:
args- args- See Also:
-
with
Putvalueunderlowercase(key). Will throw aDuplicateContextItemExceptionif there was value registered already.- Parameters:
key- key valuevalue- object of Object class- Returns:
- calling the with method
- See Also:
-
with
Putvalueunder keylowercase(key). allowOverwrite flag determines what happens in case of key collision. If overwriting is allowed, new value replaces old one, otherwiseDuplicateContextItemExceptionis thrown. Lowercase function uses default JVM locale, if none other specified.- Parameters:
key- key valuevalue- object of Object classallowOverwrite- boolean parameter- Returns:
- object of DefaultContext object
- See Also:
-
modifyKey
Modifies key, that will be used in theuserContext. It is lowercased by default, can be overwritten.- Parameters:
key- context path key- Returns:
- modified key
-
set
Put value underlowercase(value.class.getSimpleName())in user context map. Internally calls set(java.lang.Object).- Parameters:
value- object of Object class- Returns:
- calling of with method
-
get
Return object stored under key. Lowercase function uses default JVM locale, if none other specified.- Parameters:
key- key value- Returns:
- object stored under key.
-
adhoc
-
get
Return object stored under key and cast it to given class.- Type Parameters:
T- type T- Parameters:
key- key valuetargetClass- object of T Class- Returns:
- object stored under key and cast it to given class.
-
getString
Return object stored under key as String.- Parameters:
key- key value- Returns:
- object stored under key as String.
- See Also:
-
get
Looks for object of classclazz(or object which class is assignable fromclazz. Algorithm:- look for object stored under
clazz.getSimpleName(), return if not null and class matches - iterate through all context values to look for first object that matches provided clazz
- Type Parameters:
T- type T- Parameters:
clazz- object of T Class- Returns:
- If userContext is null return null otherwise return object
- look for object stored under
-
getLevelValues
- Specified by:
getLevelValuesin interfaceParamContext- Returns:
- 2 dimensional array of values that form parameter query, has length equal to queried parameter input level count
-
setLevelValues
Set level values directly as objects. Method is null safe, puts null value into level values.- Specified by:
setLevelValuesin interfaceParamContext- Parameters:
levelValues- array of values that form parameter query, should have length equal to queried parameter input level count
-
withLevelValues
-
withLevelValues
-
getUserContext
Return map representing parameter evaluation context.- Returns:
- map representing parameter evaluation context.
-
has
-
toString
-