freemarker.ext.beans
Class ArrayModel

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

public final class ArrayModel
extends BeanModel
implements TemplateCollectionModel, TemplateSequenceModel

A class that will wrap an arbitrary array into TemplateCollectionModel, TemplateSequenceModel, and TemplateHashModel interfaces. The models are cached (meaning requesting a model for same object twice will return the same model instance) unless the system property freemarker.beans.nocache is set to true. It supports element retrieval through the array[index] syntax. It also supports the array.length syntax for retrieving the length of the array, and can be iterated as a list.

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
ArrayModel(java.lang.Object array)
          Creates a new model that wraps the specified array object.
 
Method Summary
 TemplateModel get(int index)
          Retrieves the i-th template model in this sequence.
 TemplateModel get(java.lang.String key)
          If the key is named "length", the length of the array is returned.
static BeanModel 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.
 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, getObject, invokeGenericGet
 
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:
object - 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 BeanModel 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 freemarker.beans.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 BeanModel

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
If the key is named "length", the length of the array is returned. Otherwise acts as BeanModel.get(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.

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

get

public TemplateModel get(int index)
                  throws TemplateModelException
Description copied from interface: TemplateSequenceModel
Retrieves the i-th template model in this sequence.
Specified by:
get in interface TemplateSequenceModel

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 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.