Package freemarker.ext.beans
Class MapModel
- java.lang.Object
-
- freemarker.ext.beans.BeanModel
-
- freemarker.ext.beans.StringModel
-
- freemarker.ext.beans.MapModel
-
- All Implemented Interfaces:
WrapperTemplateModel,AdapterTemplateModel,TemplateHashModel,TemplateHashModelEx,TemplateMethodModel,TemplateMethodModelEx,TemplateModel,TemplateModelWithAPISupport,TemplateScalarModel
public class MapModel extends StringModel implements TemplateMethodModelEx
A special case of
BeanModelthat adds implementation forTemplateMethodModelExon 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 fromBeanModelas 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 obtainTemplateCollectionModelinstances for various aspects of the map.
-
-
Field Summary
-
Fields inherited from interface freemarker.template.TemplateModel
NOTHING
-
Fields inherited from interface freemarker.template.TemplateScalarModel
EMPTY_STRING
-
-
Constructor Summary
Constructors Constructor Description MapModel(java.util.Map map, BeansWrapper wrapper)Creates a new model that wraps the specified map object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectexec(java.util.List arguments)The first argument is used as a key to call the map's get method.protected TemplateModelinvokeGenericGet(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.booleanisEmpty()Tells whether the model is empty.protected java.util.SetkeySet()Helper method to support TemplateHashModelEx.intsize()-
Methods inherited from class freemarker.ext.beans.StringModel
getAsString
-
Methods inherited from class freemarker.ext.beans.BeanModel
get, getAdaptedObject, getAPI, getWrappedObject, hasPlainGetMethod, keys, toString, unwrap, values, wrap
-
-
-
-
Constructor Detail
-
MapModel
public MapModel(java.util.Map map, BeansWrapper wrapper)Creates a new model that wraps the specified map object.- Parameters:
map- the map object to wrap into a model.wrapper- theBeansWrapperassociated with this model. Every model has to have an associatedBeansWrapperinstance. The model gains many attributes from its wrapper, including the caching behavior, method exposure level, method-over-item shadowing policy etc.
-
-
Method Detail
-
exec
public java.lang.Object exec(java.util.List arguments) throws TemplateModelExceptionThe first argument is used as a key to call the map's get method.- Specified by:
execin interfaceTemplateMethodModel- Specified by:
execin interfaceTemplateMethodModelEx- Parameters:
arguments- aListofTemplateModel-s, containing the arguments passed to the method. If the implementation absolutely wants to operate on POJOs, it can use the static utility methods in theDeepUnwrapclass to easily obtain them. However, unwrapping is not always possible (or not perfectly), and isn't always efficient, so it's recommended to use the originalTemplateModelvalue as much as possible.- Returns:
- the return value of the method, or
null. If the returned value does not implementTemplateModel, it will be automatically wrapped using theenvironment's object wrapper. - Throws:
TemplateModelException
-
invokeGenericGet
protected TemplateModel invokeGenericGet(java.util.Map keyMap, java.lang.Class clazz, java.lang.String key) throws TemplateModelException
Overridden to invoke the generic get method by casting to Map instead of through reflection - should yield better performance.- Overrides:
invokeGenericGetin classBeanModel- Throws:
TemplateModelException
-
isEmpty
public boolean isEmpty()
Description copied from class:BeanModelTells whether the model is empty. It is empty if either the wrapped object is null, or it's a Boolean with false value.- Specified by:
isEmptyin interfaceTemplateHashModel- Overrides:
isEmptyin classBeanModel
-
size
public int size()
- Specified by:
sizein interfaceTemplateHashModelEx- Overrides:
sizein classBeanModel- Returns:
- the number of key/value mappings in the hash.
-
keySet
protected java.util.Set keySet()
Description copied from class:BeanModelHelper method to support TemplateHashModelEx. Returns the Set of Strings which are available via the TemplateHashModel interface. Subclasses that override invokeGenericGet to provide additional hash keys should also override this method.
-
-