freemarker.ext.beans
Class MapModel
java.lang.Object
|
+--freemarker.ext.beans.BeanModelBase
|
+--freemarker.ext.beans.BeanModel
|
+--freemarker.ext.beans.MapModel
- All Implemented Interfaces:
- TemplateHashModel, TemplateMethodModel, TemplateMethodModelEx, TemplateModel, TemplateScalarModel
- public class MapModel
- extends BeanModel
- implements TemplateMethodModelEx
A special case of BeanModel
that adds implementation
for TemplateMethodModelEx
on map objects that is a shortcut for the
Map.get() method. Note that if the passed argument itself is a
reflection-wrapper model, then the map lookup will be performed using the
wrapped object as the key. Note that you can call get() using the
map.key syntax inherited from BeanModel
as well,
however in that case the key is always a string.
The class itself does not implement the TemplateCollectionModel
.
You can, however use map.entrySet(), map.keySet(), or
map.values() to obtain TemplateCollectionModel
instances for
various aspects of the map.
- Version:
- 1.0
- Author:
- Attila Szegedi, szegedia@freemail.hu
Constructor Summary |
MapModel(java.util.Map map)
Creates a new model that wraps the specified map object. |
Method Summary |
TemplateModel |
exec(java.util.List arguments)
The first argument is used as a key to call the map's get method. |
static MapModel |
getInstance(java.util.Map object)
Returns a model wrapping the specified map object. |
int |
getType()
Returns the type of this object (which is TYPE_MAP) |
protected TemplateModel |
invokeGenericGet(java.util.Map keyMap,
java.lang.Class clazz,
java.lang.String key)
Overridden to invoke the generic get method by casting to Map instead of
through reflection - should yield better performance. |
boolean |
isEmpty()
Returns true if the underlying map is empty. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
MapModel
public MapModel(java.util.Map map)
- Creates a new model that wraps the specified map object.
- Parameters:
object
- the map object to wrap into a model.
getInstance
public static final MapModel getInstance(java.util.Map object)
- Returns a model wrapping the specified map object. If there is already
a cached model instance for this map, returns the cached model instance.
Models are cached using
WeakReference
objects. The caching can be turned
off by setting the freemarker.beans.nocache system property to
true. In this case calling this method is equivalent to constructing a new model.
- Parameters:
object
- the map to wrap into a model.- Returns:
- the model for the map
getType
public int getType()
- Returns the type of this object (which is TYPE_MAP)
- Overrides:
getType
in class BeanModel
exec
public TemplateModel exec(java.util.List arguments)
throws TemplateModelException
- The first argument is used as a key to call the map's get method.
- Specified by:
exec
in interface TemplateMethodModel
- Following copied from interface:
freemarker.template.TemplateMethodModel
- Parameters:
arguments
- a List of String objects
containing the values of the arguments passed to the method.- Returns:
- the TemplateModel produced by the method, or null.
invokeGenericGet
protected TemplateModel invokeGenericGet(java.util.Map keyMap,
java.lang.Class clazz,
java.lang.String key)
- Overridden to invoke the generic get method by casting to Map instead of
through reflection - should yield better performance.
- Overrides:
invokeGenericGet
in class BeanModel
isEmpty
public boolean isEmpty()
- Returns true if the underlying map is empty.
- Specified by:
isEmpty
in interface TemplateModel
- Overrides:
isEmpty
in class freemarker.ext.beans.BeanModelBase
- Following copied from interface:
freemarker.template.TemplateModel
- Returns:
- true if this object is empty.