org.szegedi.expose.model
Class ReflectionArrayModel

java.lang.Object
  |
  +--org.szegedi.expose.model.ReflectionObjectModelBase
        |
        +--org.szegedi.expose.model.ReflectionArrayModel
All Implemented Interfaces:
freemarker.template.TemplateHashModel, freemarker.template.TemplateListModel, freemarker.template.TemplateMethodModel, freemarker.template.TemplateModel, freemarker.template.TemplateScalarModel

public final class ReflectionArrayModel
extends ReflectionObjectModelBase
implements freemarker.template.TemplateHashModel, freemarker.template.TemplateMethodModel, freemarker.template.TemplateListModel

A class that will wrap an arbitrary array into TemplateHashModel, TemplateMethodModel, and TemplateListModel 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. Except supporting retrieval through array.index syntax and array("index") syntax, it also supports the array.length syntax for retrieving the length of the array. Using the model as a list model is thread-safe, as it maintains the list position on a per-thread basis.

Version:
1.0
Author:
Attila Szegedi, attila@szegedi.org

Fields inherited from class org.szegedi.expose.model.ReflectionObjectModelBase
TYPE_ARRAY, TYPE_COLLECTION, TYPE_ENUMERATION, TYPE_ITERATOR, TYPE_MAP, TYPE_OBJECT, TYPE_OBJECT_BASE, TYPE_RESOURCE_BUNDLE
 
Constructor Summary
ReflectionArrayModel(java.lang.Object array)
          Creates a new model that wraps the specified array object.
 
Method Summary
 freemarker.template.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).
 freemarker.template.TemplateModel get(int index)
          Retrieves an array element by its index, wrapped into an appropriate template model.
 freemarker.template.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.
static ReflectionArrayModel 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 hasNext()
          True if the list pointer is not past the last element of the array
 boolean isEmpty()
          Returns true if the wrapped array is null, or its length is 0.
 boolean isRewound()
          True if the list pointer points to the first element of the array
 freemarker.template.TemplateModel listSize()
          Returns a Simple scalar with a string containing the decimal representation of the array size.
 freemarker.template.TemplateModel next()
          Returns the array element at the list pointer, advances list pointer by one
 void rewind()
          Rewinds the list pointer to the first element of the array
 
Methods inherited from class org.szegedi.expose.model.ReflectionObjectModelBase
getAsString, getInstance, getObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionArrayModel

public ReflectionArrayModel(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 ReflectionArrayModel 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 ReflectionObjectModelBase

get

public freemarker.template.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. If the key is named "length", the length of the array is returned.
Specified by:
get in interface freemarker.template.TemplateHashModel

exec

public freemarker.template.TemplateModel exec(java.util.List arguments)
                                       throws freemarker.template.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 freemarker.template.TemplateMethodModel

get

public freemarker.template.TemplateModel get(int index)
Retrieves an array element by its index, wrapped into an appropriate template model.

isEmpty

public boolean isEmpty()
Returns true if the wrapped array is null, or its length is 0.
Specified by:
isEmpty in interface freemarker.template.TemplateModel
Overrides:
isEmpty in class ReflectionObjectModelBase

hasNext

public boolean hasNext()
True if the list pointer is not past the last element of the array
Specified by:
hasNext in interface freemarker.template.TemplateListModel

isRewound

public boolean isRewound()
True if the list pointer points to the first element of the array
Specified by:
isRewound in interface freemarker.template.TemplateListModel

next

public freemarker.template.TemplateModel next()
Returns the array element at the list pointer, advances list pointer by one
Specified by:
next in interface freemarker.template.TemplateListModel

rewind

public void rewind()
Rewinds the list pointer to the first element of the array
Specified by:
rewind in interface freemarker.template.TemplateListModel

listSize

public freemarker.template.TemplateModel listSize()
Returns a Simple scalar with a string containing the decimal representation of the array size.