View Javadoc

1   /*
2    * Copyright 2003-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.velocity.tools.generic;
18  
19  import java.lang.reflect.Array;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  /***
24   * Tool for working with arrays in Velocity templates.
25   * It provides a method to get and set specified elements,
26   * retrieve the length, and create clones of an array object.
27   * Also provides a method to convert arrays into java.util.List's.
28   *
29   * <p><pre>
30   * Example uses:
31   *  $primes                   -> new int[] {2, 3, 5, 7}
32   *  $array.length($primes)    -> 4
33   *  $array.get($primes, 2)    -> 5
34   *  $array.clone($primes)     -> int[] {2, 3, 5, 7}, != $primes
35   *  $array.set($primes, 2, 1) -> (primes[2] becomes 1)
36   *  $array.get($primes, 2)    -> 1
37   *  $array.get($clone, 2)     -> 5
38   *
39   * Example toolbox.xml config (if you want to use this with VelocityView):
40   * &lt;tool&gt;
41   *   &lt;key&gt;array&lt;/key&gt;
42   *   &lt;scope&gt;application&lt;/scope&gt;
43   *   &lt;class&gt;org.apache.velocity.tools.generic.ArrayTool&lt;/class&gt;
44   * &lt;/tool&gt;
45   * </pre></p>
46   *
47   * <p>This tool is entirely threadsafe, and has no instance members.
48   * It may be used in any scope (request, session, or application).
49   * </p>
50   *
51   * @author <a href="mailto:shinobu@ieee.org">Shinobu Kawai</a>
52   * @version $Id: $
53   */
54  public class ArrayTool
55  {
56  
57      /***
58       * Default constructor.
59       */
60      public ArrayTool()
61      {
62      }
63  
64      /***
65       * Converts an array object into a java.util.List.
66       * <ul>
67       *   <li>
68       *     If the object is an array of an Object,
69       *     it will return a java.util.List of the elements.
70       *   </li>
71       *   <li>
72       *     If the object is an array of a primitive type,
73       *     it will return a java.util.List of the elements wrapped in their wrapper class.
74       *   </li>
75       *   <li>
76       *     If the object is none of the above, it will return null.
77       *   </li>
78       * </ul>
79       * @param array an array object.
80       * @return the converted java.util.List.
81       */
82      public List list(Object array)
83      {
84          if (!this.isArray(array))
85          {
86              return null;
87          }
88  
89          // Thanks to Eric Fixler for this refactor.
90          int length = Array.getLength(array);
91          List asList = new ArrayList(length);
92          for (int index = 0; index < length; ++index)
93          {
94              asList.add(Array.get(array, index));
95          }
96          return asList;
97      }
98  
99      /***
100      * Gets the specified element of an array.
101      * It will return null under the following conditions:
102      * <ul>
103      *   <li><code>array</code> is null.</li>
104      *   <li><code>array</code> is not an array.</li>
105      *   <li><code>array</code> doesn't have an <code>index</code>th value.</li>
106      * </ul>
107      * @param array the array object.
108      * @param index the index of the array to get.
109      * @return the specified element of the array.
110      */
111     public Object get(Object array, int index)
112     {
113         if (!this.isArray(array))
114         {
115             return null;
116         }
117 
118         try
119         {
120             return Array.get(array, index);
121         }
122         catch (IndexOutOfBoundsException e)
123         {
124             // The index was wrong.
125             return null;
126         }
127     }
128 
129     /***
130      * Sets the specified element of an array.
131      * It will return null under the following conditions:
132      * <ul>
133      *   <li><code>array</code> is null.</li>
134      *   <li><code>array</code> is not an array.</li>
135      *   <li><code>array</code> doesn't have an <code>index</code>th value.</li>
136      * </ul>
137      * @param array the array object.
138      * @param index the index of the array to set.
139      * @param value the element to set.
140      */
141     public Object set(Object array, int index, Object value)
142     {
143         if (!this.isArray(array))
144         {
145             return null;
146         }
147 
148         try
149         {
150             Array.set(array, index, value);
151             return "";
152         }
153         catch (IndexOutOfBoundsException e)
154         {
155             // The index was wrong.
156             return null;
157         }
158     }
159 
160     /***
161      * Gets the length of an array.
162      * It will return null under the following conditions:
163      * <ul>
164      *   <li><code>array</code> is null.</li>
165      *   <li><code>array</code> is not an array.</li>
166      * </ul>
167      * @param array the array object.
168      * @return the length of the array.
169      */
170     public Integer length(Object array)
171     {
172         if (!this.isArray(array))
173         {
174             return null;
175         }
176 
177         // Thanks to Eric Fixler for this refactor.
178         return new Integer(Array.getLength(array));
179     }
180 
181     /***
182      * Gets the clone of an array.
183      * It will return null under the following conditions:
184      * <ul>
185      *   <li><code>array</code> is null.</li>
186      *   <li><code>array</code> is not an array.</li>
187      * </ul>
188      * @param array the array object.
189      * @return the clone of the array.
190      */
191     public Object clone(Object array)
192     {
193         if (!this.isArray(array))
194         {
195             return null;
196         }
197 
198         Class type = array.getClass().getComponentType();
199         int length = Array.getLength(array);
200         Object clone = Array.newInstance(type, length);
201         System.arraycopy(array, 0, clone, 0, length);
202         return clone;
203     }
204 
205     /***
206      * Checks if an object is an array.
207      * @param object the object to check.
208      * @return <code>true</code> if the object is an array.
209      */
210     public boolean isArray(Object object)
211     {
212         if (object == null)
213         {
214             return false;
215         }
216         return object.getClass().isArray();
217     }
218 
219 }