org.apache.velocity.tools.generic
Class ListTool

java.lang.Object
  extended byorg.apache.velocity.tools.generic.ListTool
Direct Known Subclasses:
ExtendedListTool

public class ListTool
extends java.lang.Object

Tool for working with Lists and arrays in Velocity templates. It provides a method to get and set specified elements. Also provides methods to perform the following actions to Lists and arrays:

 Example uses:
  $primes                    -> new int[] {2, 3, 5, 7}
  $list.size($primes)        -> 4
  $list.get($primes, 2)      -> 5
  $list.set($primes, 2, 1)   -> (primes[2] becomes 1)
  $list.get($primes, 2)      -> 1
  $list.isEmpty($primes)     -> false
  $list.contains($primes, 7) -> true
 

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

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

Version:
$Id: ListTool.java,v 1.1 2005/08/31 14:39:34 czarneckid Exp $
Author:
Shinobu Kawai

Constructor Summary
ListTool()
          Default constructor.
 
Method Summary
 java.lang.Boolean contains(java.lang.Object list, java.lang.Object element)
          Checks if a List/array contains a certain element.
 java.lang.Object get(java.lang.Object list, int index)
          Gets the specified element of a List/array.
 boolean isArray(java.lang.Object object)
          Checks if an object is an array.
 java.lang.Boolean isEmpty(java.lang.Object list)
          Checks if a List/array is empty.
 boolean isList(java.lang.Object object)
          Checks if an object is a List.
 java.lang.Object set(java.lang.Object list, int index, java.lang.Object value)
          Sets the specified element of a List/array.
 java.lang.Integer size(java.lang.Object list)
          Gets the size of a List/array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ListTool

public ListTool()
Default constructor.

Method Detail

get

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

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

set

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

Parameters:
list - the List/array object.
index - the index of the List/array to set.
value - the element to set.
Returns:
blank if set, null if not set.

size

public java.lang.Integer size(java.lang.Object list)
Gets the size of a List/array. It will return null under the following conditions: The result will be same as the #length(Object) method.

Parameters:
list - the List object.
Returns:
the size of the List.
See Also:
#length(Object)

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.

isList

public boolean isList(java.lang.Object object)
Checks if an object is a List.

Parameters:
object - the object to check.
Returns:
true if the object is a List.

isEmpty

public java.lang.Boolean isEmpty(java.lang.Object list)
Checks if a List/array is empty.

Parameters:
list - the List/array to check.
Returns:
true if the List/array is empty.

contains

public java.lang.Boolean contains(java.lang.Object list,
                                  java.lang.Object element)
Checks if a List/array contains a certain element.

Parameters:
list - the List/array to check.
element - the element to check.
Returns:
true if the List/array contains the element.