freemarker.ext.beans
Class CollectionModel

java.lang.Object
  |
  +--freemarker.ext.beans.BeanModelBase
        |
        +--freemarker.ext.beans.BeanModel
              |
              +--freemarker.ext.beans.CollectionModel
All Implemented Interfaces:
TemplateCollectionModel, TemplateHashModel, TemplateModel, TemplateScalarModel, TemplateSequenceModel

public final class CollectionModel
extends BeanModel
implements TemplateCollectionModel, TemplateSequenceModel

A special case of BeanModel that can wrap Java collections and that implements the TemplateCollectionModel in order to be iterable in a <list> block.

Version:
1.0
Author:
Attila Szegedi, szegedia@freemail.hu

Fields inherited from class freemarker.ext.beans.BeanModel
TYPE_ARRAY, TYPE_COLLECTION, TYPE_ENUMERATION, TYPE_ITERATOR, TYPE_MAP, TYPE_OBJECT, TYPE_OBJECT_BASE, TYPE_RESOURCE_BUNDLE
 
Constructor Summary
CollectionModel(java.util.Collection collection)
          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.
 TemplateModel get(java.lang.String key)
          Uses Beans introspection to locate a property or method with name matching the key name.
static CollectionModel getInstance(java.util.Collection object)
          Returns a model wrapping the specified collection object.
 int getType()
          Returns the type of this object (which is TYPE_COLLECTION)
 boolean isEmpty()
          Returns true if the underlying collection contains no elements.
 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.BeanModel
getAsString, getInstance, getInstance, getObject, invokeGenericGet
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionModel

public CollectionModel(java.util.Collection collection)
Creates a new model that wraps the specified collection object.
Parameters:
object - the collection object to wrap into a model.
Method Detail

getInstance

public static final CollectionModel getInstance(java.util.Collection object)
Returns a model wrapping the specified collection object. If there is already a cached model instance for this collection, 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 collection to wrap into a model.
Returns:
the model for the collection

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
Description copied from class: BeanModel
Uses Beans introspection to locate a property or method with name matching the key name. If a method or property is found, it is wrapped into SimpleMethodModel (for a method or indexed property), or evaluated on the fly and the return value wrapped into appropriate model (for a simple property) Models for various properties and methods are cached on a per-class basis, so the costly introspection is performed only once per property or method of a class. (Sidenote: this also implies that any class whose method has been called will be strongly referred to by the framework and will not become unloadable until this class has been unloaded first. Normally this is not an issue, but can be in a rare scenario where you create many classes on the fly. Also, as the cache grows with new classes and methods introduced to the framework, it may appear as if it were leaking memory. The framework does, however detect class reloads (if you happen to be in an environment that does this kind of things - servlet containers do it when they reload a web application) and flushes the cache. If no method or propery matching the key is found, the framework will try to invoke methods with signature get(java.lang.String), then get(java.lang.Object), or alternatively (if the wrapped object is a resource bundle) getObject(java.lang.String).
Overrides:
get in class BeanModel
Following copied from class: freemarker.ext.beans.BeanModel
Throws:
TemplateModelException - if there was no property nor method nor a generic get method to invoke.

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()
Description copied from interface: TemplateCollectionModel
Retrieves a template model iterator that is used to iterate over the elements in this collection.
Specified by:
iterator in interface TemplateCollectionModel

getType

public int getType()
Returns the type of this object (which is TYPE_COLLECTION)
Overrides:
getType in class BeanModel

isEmpty

public boolean isEmpty()
Returns true if the underlying collection contains no elements.
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.

size

public int size()
Specified by:
size in interface TemplateSequenceModel
Following copied from interface: freemarker.template.TemplateSequenceModel
Returns:
the number of items in the list.