freemarker.ext.beans
Class CollectionModel
java.lang.Object
|
+--freemarker.ext.beans.ObjectModelBase
|
+--freemarker.ext.beans.ObjectModel
|
+--freemarker.ext.beans.CollectionModel
- All Implemented Interfaces:
- TemplateHashModel, TemplateListModel2, TemplateModel, TemplateObjectModel, TemplateScalarModel
- Direct Known Subclasses:
- ListModel
- public class CollectionModel
- extends ObjectModel
- implements TemplateListModel2
A special case of ObjectModel
that supports the
collection.iterator
syntax to obtain a TemplateListModel2
instance backed by this collection.
The class itself also implements TemplateListModel2
directly, since this model preserves thread safety.
- Version:
- $Id: CollectionModel.java,v 1.4 2003/10/20 13:49:02 run2000 Exp $
- Author:
- Attila Szegedi, attila@szegedi.org
Constructor Summary |
CollectionModel(java.util.Collection collection)
Creates a new model that wraps the specified collection object. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
CollectionModel
public CollectionModel(java.util.Collection collection)
- Creates a new model that wraps the specified collection object.
- Parameters:
collection
- the collection object to wrap into a model.
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 expose.reflection.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:
ObjectModel
- 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
MethodModel
or ScalarModel
instance and
returned. 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.)
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)
.
- Overrides:
get
in class ObjectModel
- Following copied from class:
freemarker.ext.beans.ObjectModel
- Throws:
TemplateModelException
- if there was no property nor method nor
a generic get
method to invoke.
getType
public int getType()
- Returns the type of this object (which is TYPE_COLLECTION)
- Overrides:
getType
in class ObjectModel
isEmpty
public boolean isEmpty()
- Returns true if the underlying collection contains no elements.
- Specified by:
isEmpty
in interface TemplateModel
- Overrides:
isEmpty
in class ObjectModelBase
- Following copied from interface:
freemarker.template.TemplateModel
- Returns:
true
if this object is empty, otherwise false
templateIterator
public TemplateIteratorModel templateIterator()
throws TemplateModelException
- Retrive an iterator for this
Collection
.
- Specified by:
templateIterator
in interface TemplateListModel2
- Returns:
- a
TemplateIteratorModel
wrapping the underlying
java.util.Iterator
implementation
releaseIterator
public void releaseIterator(TemplateIteratorModel iterator)
- Release the iterator once we've finished with it
- Specified by:
releaseIterator
in interface TemplateListModel2
- Parameters:
iterator
- the iterator model to be released