Class DefaultContext

java.lang.Object
org.smartparam.engine.core.context.DefaultContext
All Implemented Interfaces:
ParamContext
Direct Known Subclasses:
HyperonContext, LevelValues

public class DefaultContext extends Object implements ParamContext

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(java.lang.Object[])), that is later translated into string array of level values (with help of level creators) returned by ParamContext.getLevelValues().

DefaultContext should be extended to form specialized user contexts, which can provide support for application domain objects. Thanks to DefaultContext initialization algorithm, it is enough to pass an object to the constructor and it will take care of running a setter in user implemented context to set the value.
Since:
1.0.0
Author:
Przemek Hertel
  • Constructor Details

    • DefaultContext

      public DefaultContext(Object... args)

      Puts provided values into context using algorithm:

      1. if args[i] is String[] level values are set using [link setLevelValues(String[])]
      2. if args[i] is Object[] level values are set using [link setLevelValues(Object[])]
      3. if args[i] is String args[i+1] value is taken and put into context under args[i] key using [set(String, Object)]
      4. if args[i] is Locale, it is set as a locale used for lowercase operations in [set(String, Object, boolean)]
      5. else, setter lookup is performed using (ReflectionSetterFinder) to find any setter of current context object that accepts args[i]
      6. eventually, args[i] is put into context under its class name using set(java.lang.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 ReflectionSetterInvoker.findSetter(java.lang.Class, java.lang.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).

      There is one more, power-user property. It is possible to substitute default implementation of 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 of Object
    • DefaultContext

      public DefaultContext()
      Create empty context, use setter methods to initialize it.
      See Also:
  • Method Details

    • initialize

      protected final void initialize(Object... args)
      Implementation of value initializing algorithm.
      Parameters:
      args - args
      See Also:
    • with

      public final DefaultContext with(String key, Object value)
      Put value under lowercase(key). Will throw a DuplicateContextItemException if there was value registered already.
      Parameters:
      key - key value
      value - object of Object class
      Returns:
      calling the with method
      See Also:
    • with

      public final DefaultContext with(String key, Object value, boolean allowOverwrite)
      Put value under key lowercase(key). allowOverwrite flag determines what happens in case of key collision. If overwriting is allowed, new value replaces old one, otherwise DuplicateContextItemException is thrown. Lowercase function uses default JVM locale, if none other specified.
      Parameters:
      key - key value
      value - object of Object class
      allowOverwrite - boolean parameter
      Returns:
      object of DefaultContext object
      See Also:
    • modifyKey

      protected String modifyKey(String key)
      Modifies key, that will be used in the userContext. It is lowercased by default, can be overwritten.
      Parameters:
      key - context path key
      Returns:
      modified key
    • set

      public final DefaultContext set(Object value)
      Put value under lowercase(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

      public Object get(String key)
      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

      protected DefaultContext adhoc(DefaultContext ctx)
    • get

      public <T> T get(String key, Class<T> targetClass)
      Return object stored under key and cast it to given class.
      Type Parameters:
      T - type T
      Parameters:
      key - key value
      targetClass - object of T Class
      Returns:
      object stored under key and cast it to given class.
    • getString

      public String getString(String key)
      Return object stored under key as String.
      Parameters:
      key - key value
      Returns:
      object stored under key as String.
      See Also:
    • get

      public <T> T get(Class<T> clazz)
      Looks for object of class clazz (or object which class is assignable from clazz. Algorithm:
      1. look for object stored under clazz.getSimpleName(), return if not null and class matches
      2. 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
    • getLevelValues

      public Object[][] getLevelValues()
      Specified by:
      getLevelValues in interface ParamContext
      Returns:
      2 dimensional array of values that form parameter query, has length equal to queried parameter input level count
    • setLevelValues

      public final void setLevelValues(Object... levelValues)
      Set level values directly as objects. Method is null safe, puts null value into level values.
      Specified by:
      setLevelValues in interface ParamContext
      Parameters:
      levelValues - array of values that form parameter query, should have length equal to queried parameter input level count
    • withLevelValues

      public DefaultContext withLevelValues(String... levelValues)
    • withLevelValues

      public DefaultContext withLevelValues(Object... levelValues)
    • getUserContext

      protected Map<String,Object> getUserContext()
      Return map representing parameter evaluation context.
      Returns:
      map representing parameter evaluation context.
    • toString

      public String toString()
      Overrides:
      toString in class Object