net.sf.mmm.util.context.base
Class AbstractGenericContextProxy

java.lang.Object
  extended by net.sf.mmm.util.context.base.AbstractGenericContextProxy
All Implemented Interfaces:
GenericContext
Direct Known Subclasses:
ImmutableGenericContext

public abstract class AbstractGenericContextProxy
extends Object
implements GenericContext

This is an abstract base implementation of the GenericContext interface that delegates to another instance.

Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Constructor Summary
AbstractGenericContextProxy()
          The constructor.
 
Method Summary
 MutableGenericContext createChildContext()
          This method creates a new context that inherits all variables from this context (and its parent contexts).
protected abstract  GenericContext getContext()
          This method gets the delegate instance this proxy points to.
<T> T
getVariable(Class<T> type)
          This method gets the variable associated with the given type.
 Object getVariable(String variableName)
          This method gets the variable associated with the given variableName.
<T> T
getVariable(String variableName, Class<T> type)
          This method gets the variable associated with the given variableName as the given type.
 Set<String> getVariableNames()
          This method returns the names of all defined variables.
 boolean hasVariable(String variableName)
          This method determines if the variable for the given variableName exists.
<T> T
requireVariable(Class<T> type)
          This method gets the variable associated with the given type.
 Object requireVariable(String variableName)
          This method gets the variable associated with the given variableName.
<T> T
requireVariable(String variableName, Class<T> type)
          This method gets the variable associated with the given variableName.
 Map<String,Object> toMap()
          This method allows to create a Map representing the variables of this context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractGenericContextProxy

public AbstractGenericContextProxy()
The constructor.

Method Detail

getContext

protected abstract GenericContext getContext()
This method gets the delegate instance this proxy points to.

Returns:
the real environment behind this proxy.

requireVariable

public Object requireVariable(String variableName)
                       throws ValueNotSetException
This method gets the variable associated with the given variableName.

Specified by:
requireVariable in interface GenericContext
Parameters:
variableName - is the name of the requested variable.
Returns:
the value of the variable.
Throws:
ValueNotSetException - if the requested variable is NOT set.

requireVariable

public <T> T requireVariable(String variableName,
                             Class<T> type)
                  throws ValueNotSetException
This method gets the variable associated with the given variableName.

Specified by:
requireVariable in interface GenericContext
Type Parameters:
T - the generic type of the variable.
Parameters:
variableName - is the name of the requested variable.
type - is the class reflecting the type of the variable.
Returns:
the value of the variable.
Throws:
ValueNotSetException - if the requested variable is NOT set.

requireVariable

public <T> T requireVariable(Class<T> type)
                  throws ValueNotSetException
This method gets the variable associated with the given type. It will use the classname as variable-name.
ATTENTION:
Only use this method in combination with expressive types. E.g. types like String or Integer are bad candidates while MySpecificSingletonComponentInterface might be a good option.

Specified by:
requireVariable in interface GenericContext
Type Parameters:
T - the generic type of the variable.
Parameters:
type - is the class reflecting the type of the variable.
Returns:
the value of the variable.
Throws:
ValueNotSetException - if the requested variable is NOT set.
See Also:
MutableGenericContext.setVariable(String, Object)

getVariable

public Object getVariable(String variableName)
This method gets the variable associated with the given variableName.

Specified by:
getVariable in interface GenericContext
Parameters:
variableName - is the name of the requested variable.
Returns:
the value of the variable or null if the variable is NOT set.

getVariable

public <T> T getVariable(String variableName,
                         Class<T> type)
This method gets the variable associated with the given variableName as the given type. If the type does NOT match the actual type of the variable it may automatically be converted as possible.

Specified by:
getVariable in interface GenericContext
Type Parameters:
T - the generic type of the variable.
Parameters:
variableName - is the name of the requested variable.
type - is the class reflecting the type of the variable.
Returns:
the value of the variable or null if the variable is NOT set.

getVariable

public <T> T getVariable(Class<T> type)
This method gets the variable associated with the given type. It will use the classname as variable-name.
ATTENTION:
Only use this method in combination with expressive types. E.g. types like String or Integer are bad candidates while MySpecificSingletonComponentInterface might be a good option.

Specified by:
getVariable in interface GenericContext
Type Parameters:
T - the generic type of the variable.
Parameters:
type - is the class reflecting the type of the variable.
Returns:
the value of the variable or null if the variable is NOT set.
See Also:
MutableGenericContext.setVariable(String, Object)

hasVariable

public boolean hasVariable(String variableName)
This method determines if the variable for the given variableName exists.

Specified by:
hasVariable in interface GenericContext
Parameters:
variableName - is the name of the requested variable.
Returns:
true if a value exists for the given variableName, false otherwise.

getVariableNames

public Set<String> getVariableNames()
This method returns the names of all defined variables.
ATTENTION:
Please note that this is an expensive operation so prefer GenericContext.hasVariable(String) where possible.

Specified by:
getVariableNames in interface GenericContext
Returns:
a set of all variable names.

createChildContext

public MutableGenericContext createChildContext()
This method creates a new context that inherits all variables from this context (and its parent contexts). Variables that are set in the child-context override inherited variables. However changes to the child-context do NOT modify this context.
ATTENTION:
Typically the child-context will be a cheap-copy connected to this context. So you should be aware of this when creating many child-contexts recursively as their parent-contexts can NOT be collected by the garbage-collector.

Specified by:
createChildContext in interface GenericContext
Returns:
the new created sub-context.

toMap

public Map<String,Object> toMap()
This method allows to create a Map representing the variables of this context. This can be useful to pass this context to an external component (e.g. a template-engine) that typically accepts a Map.
ATTENTION:
This method will create a fresh map with all variables filled in. Therefore it is NOT a cheap operation. Further the Map will NOT reflect changes of this context and vice versa.

Specified by:
toMap in interface GenericContext
Returns:
this context as map.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.