Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.velocity.tools.generic
Class ArrayTool

    java.lang.Object
      org.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.
  - If the object is an array of an Object, it will return a java.util.List of the elements.
  - If the object is an array of a primitive type, it will return a java.util.List of the elements wrapped in their wrapper class.
  - If the object is none of the above, it will return null.

  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:
  - `array` is null.
  - `array` is not an array.
  - `array` doesn't have an `index`th value.

  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:
  - `array` is null.
  - `array` is not an array.
  - `array` doesn't have an `index`th value.

  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:
  - `array` is null.
  - `array` is not an array.

  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:
  - `array` is null.
  - `array` is not an array.

  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.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD