public class CollectionModel extends StringModel implements TemplateCollectionModel, TemplateSequenceModel
A special case of BeanModel
that can wrap Java collections
and that implements the TemplateCollectionModel
in order to be usable
in a <foreach> block.
NOTHING
EMPTY_STRING
Constructor and Description |
---|
CollectionModel(java.util.Collection collection,
BeansWrapper wrapper)
Creates a new model that wraps the specified collection object.
|
Modifier and Type | Method and Description |
---|---|
TemplateModel |
get(int index)
Retrieves the i-th object from the collection, wrapped as a TemplateModel.
|
boolean |
getSupportsIndexedAccess()
Tells if
get(int) will always fail for this object. |
TemplateModelIterator |
iterator()
Retrieves a template model iterator that is used to iterate over
the elements in this collection.
|
int |
size() |
getAsString
get, getAdaptedObject, getWrappedObject, hasPlainGetMethod, invokeGenericGet, isEmpty, keys, keySet, toString, unwrap, values, wrap
public CollectionModel(java.util.Collection collection, BeansWrapper wrapper)
collection
- the collection object to wrap into a model.wrapper
- the BeansWrapper
associated with this model.
Every model has to have an associated BeansWrapper
instance. The
model gains many attributes from its wrapper, including the caching
behavior, method exposure level, method-over-item shadowing policy etc.public TemplateModel get(int index) throws TemplateModelException
get
in interface TemplateSequenceModel
null
if
the index is out of bounds. Note that a null
value 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.TemplateModelException
- if the index is out of bounds, or the
underlying collection is not a List.public boolean getSupportsIndexedAccess()
get(int)
will always fail for this object.
As this object implements TemplateSequenceModel
,
get(int)
should always work, but due to a design flaw, for
non-List
wrapped objects get(int)
will always fail.
This method exists to ease working this problem around.public TemplateModelIterator iterator()
TemplateCollectionModel
iterator
in interface TemplateCollectionModel
public int size()
size
in interface TemplateHashModelEx
size
in interface TemplateSequenceModel
size
in class BeanModel