org.apache.velocity.tools.generic
Class ArrayTool

java.lang.Object
  extended byorg.apache.velocity.tools.generic.ArrayTool

public class ArrayTool
extends java.lang.Object

Tool for working with arrays in Velocity templates. It provides a method to get and set specified elements, retrieve the length, and create clones of an array object. Also provides a method to convert arrays into java.util.List's.

 Example uses:
  $primes                   -> new int[] {2, 3, 5, 7}
  $array.length($primes)    -> 4
  $array.get($primes, 2)    -> 5
  $array.clone($primes)     -> int[] {2, 3, 5, 7}, != $primes
  $array.set($primes, 2, 1) -> (primes[2] becomes 1)
  $array.get($primes, 2)    -> 1
  $array.get($clone, 2)     -> 5
 

Example toolbox.xml config (if you want to use this with VelocityView): <tool> <key>array</key> <scope>application</scope> <class>org.apache.velocity.tools.generic.ArrayTool</class> </tool>

This tool is entirely threadsafe, and has no instance members. It may be used in any scope (request, session, or application).

Author:
Shinobu Kawai

Constructor Summary
ArrayTool()
          Default constructor.
 
Method Summary
 java.lang.Object clone(java.lang.Object array)
          Gets the clone of an array.
 java.lang.Object get(java.lang.Object array, int index)
          Gets the specified element of an array.
 boolean isArray(java.lang.Object object)
          Checks if an object is an array.
 java.lang.Integer length(java.lang.Object array)
          Gets the length of an array.
 java.util.List list(java.lang.Object array)
          Converts an array object into a java.util.List.
 java.lang.Object set(java.lang.Object array, int index, java.lang.Object value)
          Sets the specified element of an array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayTool

public ArrayTool()
Default constructor.

Method Detail

list

public java.util.List list(java.lang.Object array)
Converts an array object into a java.util.List.

Parameters:
array - an array object.
Returns:
the converted java.util.List.

get

public java.lang.Object get(java.lang.Object array,
                            int index)
Gets the specified element of an array. It will return null under the following conditions:

Parameters:
array - the array object.
index - the index of the array to get.
Returns:
the specified element of the array.

set

public java.lang.Object set(java.lang.Object array,
                            int index,
                            java.lang.Object value)
Sets the specified element of an array. It will return null under the following conditions:

Parameters:
array - the array object.
index - the index of the array to set.
value - the element to set.

length

public java.lang.Integer length(java.lang.Object array)
Gets the length of an array. It will return null under the following conditions:

Parameters:
array - the array object.
Returns:
the length of the array.

clone

public java.lang.Object clone(java.lang.Object array)
Gets the clone of an array. It will return null under the following conditions:

Parameters:
array - the array object.
Returns:
the clone of the array.

isArray

public boolean isArray(java.lang.Object object)
Checks if an object is an array.

Parameters:
object - the object to check.
Returns:
true if the object is an array.