freemarker.ext.beans
Class ArrayModel

java.lang.Object
  |
  +--freemarker.ext.beans.ObjectModelBase
        |
        +--freemarker.ext.beans.ArrayModel
All Implemented Interfaces:
TemplateHashModel, TemplateIndexedModel, TemplateListModel2, TemplateMethodModel2, TemplateModel, TemplateObjectModel, TemplateScalarModel

public final class ArrayModel
extends ObjectModelBase
implements TemplateHashModel, TemplateMethodModel2, TemplateIndexedModel, TemplateListModel2

A class that will wrap an arbitrary array into TemplateHashModel, TemplateMethodModel2, TemplateIndexedModel and TemplateListModel2 interfaces.

The models are cached (meaning requesting a model for same object twice will return the same model instance) unless the system property expose.reflection.nocache is set to true.

Apart from supporting retrieval through array[index] syntax and array("index") syntax, it also supports the array.length syntax for retrieving the length of the array. A template can iterate over the array directly using the <list> or <foreach> syntax.

The array.iterator syntax for retrieving a TemplateListModel2 interface backed by this array is supported for backward compatibility with earlier releases.

Using the model as a list model is thread-safe, as it maintains the list position on a per-iterator basis using the new TemplateListModel2 interface.

Version:
$Id: ArrayModel.java,v 1.4 2003/10/20 13:49:02 run2000 Exp $
Author:
Attila Szegedi, attila@szegedi.org

Fields inherited from class freemarker.ext.beans.ObjectModelBase
TYPE_ARRAY, TYPE_COLLECTION, TYPE_ENUMERATION, TYPE_ITERATOR, TYPE_LIST, TYPE_MAP, TYPE_OBJECT, TYPE_OBJECT_BASE, TYPE_RESOURCE_BUNDLE
 
Constructor Summary
ArrayModel(java.lang.Object array)
          Creates a new model that wraps the specified array object.
 
Method Summary
 TemplateModel exec(java.util.List arguments)
          The first argument of the list is interpreted as an array index (it can be either a Number, or a String containing parseable integer).
 TemplateModel get(java.lang.String key)
          If the key can be parsed by the java.lang.Integer#parseInt(java.lang.String) method into an array index, the array element at the parsed index is returned.
 TemplateModel getAtIndex(long index)
          Retrive the object at the specified index.
static ArrayModel getInstance(java.lang.Object object)
          Returns a model wrapping the specified array object.
 int getType()
          Returns the type of this object (which is TYPE_ARRAY)
 boolean isEmpty()
          Returns true if the wrapped array is null, or its length is 0.
 void releaseIterator(TemplateIteratorModel iterator)
          Return the iterator to the object pool, if any.
 TemplateIteratorModel templateIterator()
          Retrieve an iterator over this array.
 
Methods inherited from class freemarker.ext.beans.ObjectModelBase
getAsObject, getAsString, getInstance
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayModel

public ArrayModel(java.lang.Object array)
Creates a new model that wraps the specified array object.
Parameters:
array - the array object to wrap into a model.
Throws:
java.lang.IllegalArgumentException - if the passed object is not a Java array.
Method Detail

getInstance

public static final ArrayModel getInstance(java.lang.Object object)
Returns a model wrapping the specified array object. If there is already a cached model instance for this array, 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 array to wrap into a model.
Returns:
the model for the array
Throws:
java.lang.IllegalArgumentException - if the passed object is not a Java array.

getType

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

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
If the key can be parsed by the java.lang.Integer#parseInt(java.lang.String) method into an array index, the array element at the parsed index is returned. If the key is named "length", the length of the array is returned.
Specified by:
get in interface TemplateHashModel
Following copied from interface: freemarker.template.TemplateHashModel
Parameters:
key - the name by which the TemplateModel is identified in the template.
Returns:
the TemplateModel referred to by the key, or null if not found.

exec

public TemplateModel exec(java.util.List arguments)
                   throws TemplateModelException
The first argument of the list is interpreted as an array index (it can be either a Number, or a String containing parseable integer). The element at the specified index is returned.
Specified by:
exec in interface TemplateMethodModel2
Following copied from interface: freemarker.template.TemplateMethodModel2
Parameters:
arguments - a List of TemplateModel objects containing the values of the arguments passed to the method.
Returns:
the TemplateModel produced by the method, or null.

getAtIndex

public TemplateModel getAtIndex(long index)
                         throws TemplateModelException
Retrive the object at the specified index.
Specified by:
getAtIndex in interface TemplateIndexedModel
Parameters:
index - the index of the object to be retrieved
Returns:
a TemplateModel wrapping the underlying value of the object

templateIterator

public TemplateIteratorModel templateIterator()
                                       throws TemplateModelException
Retrieve an iterator over this array.
Specified by:
templateIterator in interface TemplateListModel2
Returns:
an iterator over the array, or null if the array is empty
Throws:
TemplateModelException -  

releaseIterator

public void releaseIterator(TemplateIteratorModel iterator)
Return the iterator to the object pool, if any.
Specified by:
releaseIterator in interface TemplateListModel2
Parameters:
iterator - the iterator to be returned

isEmpty

public boolean isEmpty()
Returns true if the wrapped array is null, or its length is 0.
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