Package freemarker.ext.beans
Class IteratorModel
java.lang.Object
freemarker.ext.beans.BeanModel
freemarker.ext.beans.IteratorModel
- All Implemented Interfaces:
WrapperTemplateModel,AdapterTemplateModel,TemplateCollectionModel,TemplateHashModel,TemplateHashModelEx,TemplateModel,TemplateModelIterator,TemplateModelWithAPISupport
public class IteratorModel
extends BeanModel
implements TemplateModelIterator, TemplateCollectionModel
A class that adds TemplateModelIterator functionality to the
Iterator interface implementers.
It differs from the 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.
-
Field Summary
Fields inherited from interface freemarker.template.TemplateModel
NOTHING -
Constructor Summary
ConstructorsConstructorDescriptionIteratorModel(Iterator iterator, BeansWrapper wrapper)Creates a new model that wraps the specified iterator object. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnsIterator.hasNext().booleanhasNext()Calls underlyingIterator.hasNext().iterator()This allows the iterator to be used in a<#list>block.next()Calls underlyingIterator.next()and wraps the result.Methods inherited from class freemarker.ext.beans.BeanModel
get, get, getAdaptedObject, getAPI, getBeforeMethodCall, getWrappedObject, hasPlainGetMethod, invokeGenericGet, isEmpty, keys, keySet, size, toString, unwrap, values, wrap
-
Constructor Details
-
IteratorModel
Creates a new model that wraps the specified iterator object.- Parameters:
iterator- the iterator object to wrap into a model.wrapper- theBeansWrapperassociated with this model. Every model has to have an associatedBeansWrapperinstance. The model gains many attributes from its wrapper, including the caching behavior, method exposure level, method-over-item shadowing policy etc.
-
-
Method Details
-
iterator
This allows the iterator to be used in a<#list>block.- Specified by:
iteratorin interfaceTemplateCollectionModel- Returns:
- "this"
- Throws:
TemplateModelException
-
hasNext
public boolean hasNext()Calls underlyingIterator.hasNext().- Specified by:
hasNextin interfaceTemplateModelIterator- Returns:
- whether there are any more items to iterate over.
-
next
Calls underlyingIterator.next()and wraps the result.- Specified by:
nextin interfaceTemplateModelIterator- Throws:
TemplateModelException- if the next model can not be retrieved (i.e. because the iterator is exhausted).
-
getAsBoolean
public boolean getAsBoolean()ReturnsIterator.hasNext(). Therefore, an iterator that has no more element evaluates to false, and an iterator that has further elements evaluates to true.
-