freemarker.ext.beans2
Class ArrayWrapper

java.lang.Object
  extended by freemarker.ext.beans2.ArrayWrapper
All Implemented Interfaces:
ObjectWrapper, TemplateHashModel, TemplateIndexedModel, TemplateListModel2, TemplateModel, TemplateObjectModel, TemplateScalarModel, TemplateWriteableIndexedModel, java.io.Serializable, java.lang.Cloneable

public class ArrayWrapper
extends java.lang.Object
implements ObjectWrapper, TemplateListModel2, TemplateWriteableIndexedModel, TemplateHashModel, TemplateObjectModel, TemplateScalarModel, java.io.Serializable

Wrapper for an array object. Provides list and indexed access to elements of the wrapped array.

Since:
1.9
Version:
$Id: ArrayWrapper.java 1067 2011-06-06 10:55:28Z run2000 $
Author:
Nicholas Cull
See Also:
Serialized Form

Constructor Summary
ArrayWrapper()
          Default constructor where an array is not provided initially.
ArrayWrapper(java.lang.Object object)
          Constructor that takes an array object to be wrapped.
 
Method Summary
 java.lang.Object clone()
          Creates and returns a copy of this object.
 TemplateModel get(java.lang.String key)
          Gets a TemplateModel from the hash.
 java.lang.Object getAsObject()
          Return the underlying object to the reflection mechanism.
 java.lang.String getAsString()
          Returns the array's value as a String.
 TemplateModel getAtIndex(long index)
          Get the value corresponding to the given index.
 boolean isEmpty()
          Is the object empty?
 void putAtIndex(long index, TemplateModel model)
          Set the value corresponding to the given index.
 void releaseIterator(TemplateIteratorModel iterator)
          Returns the used iterator to the list model.
 void setObject(java.lang.Object object)
          Sets the object to be wrapped by this object wrapper.
 TemplateIteratorModel templateIterator()
          Retrieves an iterator to iterate over this list.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayWrapper

public ArrayWrapper()
Default constructor where an array is not provided initially. This allows the wrapper to be used as a prototype.


ArrayWrapper

public ArrayWrapper(java.lang.Object object)
Constructor that takes an array object to be wrapped.

Parameters:
object - the array object to be wrapped
Method Detail

setObject

public void setObject(java.lang.Object object)
Description copied from interface: ObjectWrapper
Sets the object to be wrapped by this object wrapper.

Specified by:
setObject in interface ObjectWrapper
Parameters:
object - the object to be wrapped

isEmpty

public boolean isEmpty()
                throws TemplateModelException
Description copied from interface: TemplateModel
Is the object empty?

Specified by:
isEmpty in interface TemplateModel
Returns:
true if this object is empty, otherwise false
Throws:
TemplateModelException

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
Description copied from interface: TemplateHashModel
Gets a TemplateModel from the hash.

Specified by:
get in interface 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.
Throws:
TemplateModelException - there was a problem getting the value for the given key

templateIterator

public TemplateIteratorModel templateIterator()
                                       throws TemplateModelException
Description copied from interface: TemplateListModel2
Retrieves an iterator to iterate over this list. Can choose to return null if there are no elements in the list.

Specified by:
templateIterator in interface TemplateListModel2
Returns:
an iterator to iterate over the current list.
Throws:
TemplateModelException - the next item in the list can't be retrieved, or no next item exists.

releaseIterator

public void releaseIterator(TemplateIteratorModel iterator)
Description copied from interface: TemplateListModel2

Returns the used iterator to the list model. Implement this method when you want to use an object pool of TemplateIterator objects. Otherwise, leave the implementation of this method blank.

Note that if the iterator returned in TemplateListModel2.templateIterator() is null, this method will not be called for the null iterator.

Specified by:
releaseIterator in interface TemplateListModel2
Parameters:
iterator - the iterator to be returned to the object pool, if any

getAtIndex

public TemplateModel getAtIndex(long index)
                         throws TemplateModelException
Description copied from interface: TemplateIndexedModel
Get the value corresponding to the given index. Traditionally this would correspond to an index into an array, or similar structure, such as a java.util.Vector.

Specified by:
getAtIndex in interface TemplateIndexedModel
Parameters:
index - the index of the underlying value we're interested in
Returns:
a TemplateModel representing the value for the given index
Throws:
TemplateModelException - the value could not be determined, possibly due to an index out-of-bounds, or an otherwise undefined value

putAtIndex

public void putAtIndex(long index,
                       TemplateModel model)
                throws TemplateModelException
Set the value corresponding to the given index.

Specified by:
putAtIndex in interface TemplateWriteableIndexedModel
Parameters:
index - the index of the underlying value we're interested in
model - the model to be added to the list
Throws:
TemplateModelException - the value could not be determined, possibly due to an index out-of-bounds, or an otherwise undefined value

getAsObject

public java.lang.Object getAsObject()
                             throws TemplateModelException
Return the underlying object to the reflection mechanism. Any variables, methods or properties can be called directly via reflection.

Specified by:
getAsObject in interface TemplateObjectModel
Returns:
the underlying object for this template model
Throws:
TemplateModelException - the object could not be returned

getAsString

public java.lang.String getAsString()
                             throws TemplateModelException
Returns the array's value as a String.

Specified by:
getAsString in interface TemplateScalarModel
Returns:
the String value of this scalar.
Throws:
TemplateModelException

clone

public java.lang.Object clone()
Creates and returns a copy of this object.

Specified by:
clone in interface ObjectWrapper
Overrides:
clone in class java.lang.Object
Returns:
a clone of this instance.
Throws:
java.lang.RuntimeException - the clone operation was not successful
java.lang.OutOfMemoryError - if there is not enough memory.
See Also:
Cloneable