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

java.lang.Object
  extended by net.sf.mmm.util.context.base.AbstractMutableGenericContext
All Implemented Interfaces:
GenericContext, MutableGenericContext
Direct Known Subclasses:
MutableGenericContextImpl

public abstract class AbstractMutableGenericContext
extends Object
implements MutableGenericContext

This class is the basic implementation of the GenericContext interface.

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

Field Summary
private  GenericContext immutableContext
          the parent context
private  MapFactory<? extends Map> mapFactory
           
private  GenericContext parent
          the parent context
private  Map<String,Object> variableMap
          maps variable names to values
 
Constructor Summary
AbstractMutableGenericContext(MapFactory<? extends Map> mapFactory, GenericContext parentContext)
          The constructor for a sub-context.
 
Method Summary
 GenericContext getImmutableContext()
          This method gets an immutable proxy of this context that can NOT be casted to MutableGenericContext.
protected  MapFactory<? extends Map> getMapFactory()
          This method gets the MapFactory.
<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.
protected  String getVariableName(Class<?> type)
          This method gets the variable-name (key) for 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.
 Object removeVariable(String variableName)
          This method removes (unsets) the variable specified by variableName.
<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.
 Object setVariable(Object value)
          This method sets the variable given by value.
 Object setVariable(String variableName, Object value)
          This method sets the variable specified by variableName to the given value.
 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
 
Methods inherited from interface net.sf.mmm.util.context.api.GenericContext
createChildContext, getVariable
 

Field Detail

variableMap

private final Map<String,Object> variableMap
maps variable names to values


parent

private final GenericContext parent
the parent context


immutableContext

private final GenericContext immutableContext
the parent context


mapFactory

private final MapFactory<? extends Map> mapFactory
See Also:
getMapFactory()
Constructor Detail

AbstractMutableGenericContext

public AbstractMutableGenericContext(MapFactory<? extends Map> mapFactory,
                                     GenericContext parentContext)
The constructor for a sub-context.

Parameters:
mapFactory - is used to create the map for storing variables.
parentContext - is the context the created one will derive from.
Method Detail

getVariableName

protected String getVariableName(Class<?> type)
This method gets the variable-name (key) for the given type.

Parameters:
type - is the Class reflecting the type of some variable.
Returns:
the generic variable-name for the given type.
See Also:
setVariable(Object), getVariable(Class), requireVariable(Class)

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(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)

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)

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.

setVariable

public Object setVariable(String variableName,
                          Object value)
This method sets the variable specified by variableName to the given value. If the variable is already set, it will be overridden.

Specified by:
setVariable in interface MutableGenericContext
Parameters:
variableName - is the name of the variable to set.
value - is the value to assign to the variable.
Returns:
the old value of the variable in this context (excluding inherited variables) or null if the variable was undefined or NOT overridden in this context.
See Also:
GenericContext.getVariable(String)

setVariable

public Object setVariable(Object value)
This method sets the variable given by value. If the variable is already set, it will be overridden. This method 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:
setVariable in interface MutableGenericContext
Parameters:
value - is the value to assign to the variable.
Returns:
the old value of the variable in this context (excluding inherited variables) or null if the variable was undefined or NOT overridden in this context.
See Also:
GenericContext.getVariable(String), GenericContext.getVariable(Class)

removeVariable

public Object removeVariable(String variableName)
This method removes (unsets) the variable specified by variableName. This will only remove a variable defined in this context. A variable inherited from a parent context can not be removed here.
If this method is called with the name of a variable not defined in this context it will have no effect. You might want to set a variable to null instead.

Specified by:
removeVariable in interface MutableGenericContext
Parameters:
variableName - is the name of the variable to unset.
Returns:
the old value of the variable or null if the variable is NOT defined (in this 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.

getImmutableContext

public GenericContext getImmutableContext()
This method gets an immutable proxy of this context that can NOT be casted to MutableGenericContext.

Specified by:
getImmutableContext in interface MutableGenericContext
Returns:
the immutable view on this context.

getMapFactory

protected MapFactory<? extends Map> getMapFactory()
This method gets the MapFactory.

Returns:
the MapFactory to use.


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