Date: 2004-11-07T03:23:26
   Editor: ShinobuKawai <[EMAIL PROTECTED]>
   Wiki: Jakarta-Velocity Wiki
   Page: ArrayTool
   URL: http://wiki.apache.org/jakarta-velocity/ArrayTool

   Add #clone() support.

Change Log:

------------------------------------------------------------------------------
@@ -24,8 +24,8 @@
 
 /**
  * Tool for working with arrays in Velocity templates.
- * It provides a method to get the length and
- * get and set specified elements of an array object.
+ * 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.
  *
  * <p><pre>
@@ -33,8 +33,10 @@
  *  $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):
  * &lt;tool&gt;
@@ -185,6 +187,44 @@
 
         // Thanks to Eric Fixler for this refactor.
         return new Integer(Array.getLength(array));
+    }
+
+    public Object clone(Object array)
+    {
+        // There must be a better way of doing this...
+        if (array instanceof Object[])
+        {
+            return ((Object[]) array).clone();
+        }
+        if (array instanceof long[])
+        {
+            return ((long[]) array).clone();
+        }
+        if (array instanceof int[])
+        {
+            return ((int[]) array).clone();
+        }
+        if (array instanceof short[])
+        {
+            return ((short[]) array).clone();
+        }
+        if (array instanceof byte[])
+        {
+            return ((byte[]) array).clone();
+        }
+        if (array instanceof char[])
+        {
+            return ((char[]) array).clone();
+        }
+        if (array instanceof double[])
+        {
+            return ((double[]) array).clone();
+        }
+        if (array instanceof float[])
+        {
+            return ((float[]) array).clone();
+        }
+        return null;
     }
 
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to