freemarker.ext.beans
public class: IteratorModel [javadoc |
source]
java.lang.Object
freemarker.ext.beans.BeanModel
freemarker.ext.beans.IteratorModel
All Implemented Interfaces:
TemplateModelIterator, TemplateCollectionModel, AdapterTemplateModel, WrapperTemplateModel, TemplateHashModelEx
A class that adds TemplateModelIterator functionality to the
Iterator interface implementers.
It differs from the freemarker.template.SimpleCollection in that
it inherits from BeanModel , and therefore you can call methods on
it directly, even to the effect of calling iterator.remove() in
the template.
Using the model as a collection model is NOT
thread-safe, as iterators are inherently not thread-safe.
Further, you can iterate over it only once. Attempts to call the
#iterator() method after it was already driven to the end once will
throw an exception.
- author:
Attila - Szegedi
- version:
$ - Id: IteratorModel.java,v 1.26 2003/06/03 13:21:32 szegedia Exp $
| Constructor: |
public IteratorModel(Iterator iterator,
BeansWrapper wrapper) {
super(iterator, wrapper);
}
Creates a new model that wraps the specified iterator object. Parameters:
iterator - the iterator 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.
|
| Methods from freemarker.ext.beans.BeanModel: |
|---|
|
get, getAdaptedObject, getWrappedObject, hasPlainGetMethod, invokeGenericGet, isEmpty, keySet, keys, size, toString, unwrap, values, wrap |
| Method from freemarker.ext.beans.IteratorModel Detail: |
public boolean getAsBoolean() {
return hasNext();
}
Returns Iterator#hasNext() . Therefore, an
iterator that has no more element evaluates to false, and an
iterator that has further elements evaluates to true. |
public boolean hasNext() {
return ((Iterator)object).hasNext();
}
|
public TemplateModelIterator iterator() throws TemplateModelException {
synchronized(this) {
if(accessed) {
throw new TemplateModelException(
"This collection is stateful and can not be iterated over the" +
" second time.");
}
accessed = true;
}
return this;
}
This allows the iterator to be used in a <foreach> block. |
public TemplateModel next() throws TemplateModelException {
try {
return wrap(((Iterator)object).next());
}
catch(NoSuchElementException e) {
throw new TemplateModelException(
"No more elements in the iterator.", e);
}
}
|