Package freemarker.ext.beans
Class CollectionModel
- java.lang.Object
-
- freemarker.ext.beans.BeanModel
-
- freemarker.ext.beans.StringModel
-
- freemarker.ext.beans.CollectionModel
-
- All Implemented Interfaces:
WrapperTemplateModel,AdapterTemplateModel,TemplateCollectionModel,TemplateHashModel,TemplateHashModelEx,TemplateModel,TemplateModelWithAPISupport,TemplateScalarModel,TemplateSequenceModel
public class CollectionModel extends StringModel implements TemplateCollectionModel, TemplateSequenceModel
A special case of
BeanModelthat can wrap Java collections and that implements theTemplateCollectionModelin order to be usable in a <#list> block.
-
-
Field Summary
-
Fields inherited from interface freemarker.template.TemplateModel
NOTHING
-
Fields inherited from interface freemarker.template.TemplateScalarModel
EMPTY_STRING
-
-
Constructor Summary
Constructors Constructor Description CollectionModel(java.util.Collection collection, BeansWrapper wrapper)Creates a new model that wraps the specified collection object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TemplateModelget(int index)Retrieves the i-th object from the collection, wrapped as a TemplateModel.booleangetSupportsIndexedAccess()Tells ifget(int)will always fail for this object.TemplateModelIteratoriterator()Retrieves a template model iterator that is used to iterate over the elements in this collection.intsize()-
Methods inherited from class freemarker.ext.beans.StringModel
getAsString
-
Methods inherited from class freemarker.ext.beans.BeanModel
get, getAdaptedObject, getAPI, getWrappedObject, hasPlainGetMethod, invokeGenericGet, isEmpty, keys, keySet, toString, unwrap, values, wrap
-
-
-
-
Constructor Detail
-
CollectionModel
public CollectionModel(java.util.Collection collection, BeansWrapper wrapper)Creates a new model that wraps the specified collection object.- Parameters:
collection- the collection 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
-
get
public TemplateModel get(int index) throws TemplateModelException
Retrieves the i-th object from the collection, wrapped as a TemplateModel.- Specified by:
getin interfaceTemplateSequenceModel- Returns:
- the item at the specified index, or
nullif the index is out of bounds. Note that anullvalue is interpreted by FreeMarker as "variable does not exist", and accessing a missing variables is usually considered as an error in the FreeMarker Template Language, so the usage of a bad index will not remain hidden, unless the default value for that case was also specified in the template. - Throws:
TemplateModelException- if the index is out of bounds, or the underlying collection is not a List.
-
getSupportsIndexedAccess
public boolean getSupportsIndexedAccess()
Tells ifget(int)will always fail for this object. As this object implementsTemplateSequenceModel,get(int)should always work, but due to a design flaw, for non-Listwrapped objectsget(int)will always fail. This method exists to ease working this problem around.- Since:
- 2.3.17
-
iterator
public TemplateModelIterator iterator()
Description copied from interface:TemplateCollectionModelRetrieves a template model iterator that is used to iterate over the elements in this collection.- Specified by:
iteratorin interfaceTemplateCollectionModel
-
size
public int size()
- Specified by:
sizein interfaceTemplateHashModelEx- Specified by:
sizein interfaceTemplateSequenceModel- Overrides:
sizein classBeanModel- Returns:
- the number of key/value mappings in the hash.
-
-