freemarker.ext.beans

Class CollectionModel

Implemented Interfaces:
AdapterTemplateModel, TemplateCollectionModel, TemplateHashModel, TemplateHashModelEx, TemplateModel, TemplateScalarModel, TemplateSequenceModel, WrapperTemplateModel

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.
Version:
$Id: CollectionModel.java,v 1.22 2003/06/03 13:21:32 szegedia Exp $
Author:
Attila Szegedi

Field Summary

Fields inherited from class freemarker.ext.beans.BeanModel

object, wrapper

Fields inherited from interface freemarker.template.TemplateModel

NOTHING

Fields inherited from interface freemarker.template.TemplateScalarModel

EMPTY_STRING

Constructor Summary

CollectionModel(Collection collection, BeansWrapper wrapper)
Creates a new model that wraps the specified collection object.

Method Summary

TemplateModel
get(int index)
Retrieves the i-th object from the collection, wrapped as a TemplateModel.
TemplateModelIterator
iterator()
Retrieves a template model iterator that is used to iterate over the elements in this collection.
int
size()

Methods inherited from class freemarker.ext.beans.StringModel

getAsString

Methods inherited from class freemarker.ext.beans.BeanModel

get, getAdaptedObject, getWrappedObject, hasPlainGetMethod, invokeGenericGet, isEmpty, keySet, keys, size, toString, unwrap, values, wrap

Constructor Details

CollectionModel

public CollectionModel(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 - 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.

Method Details

get

public TemplateModel get(int index)
            throws TemplateModelException
Retrieves the i-th object from the collection, wrapped as a TemplateModel.
Specified by:
get in interface TemplateSequenceModel
Throws:
TemplateModelException - if the index is out of bounds, or the underlying collection is not a List.

iterator

public TemplateModelIterator iterator()
Retrieves a template model iterator that is used to iterate over the elements in this collection.
Specified by:
iterator in interface TemplateCollectionModel

size

public int size()
Specified by:
size in interface TemplateSequenceModel
size in interface TemplateHashModelEx
Overrides:
size in interface BeanModel
Returns:
the number of items in the list.