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

   Some refactoring.

Change Log:

------------------------------------------------------------------------------
@@ -191,40 +191,21 @@
 
     public Object clone(Object array)
     {
-        // There must be a better way of doing this...
-        if (array instanceof Object[])
+        if (array == null)
         {
-            return ((Object[]) array).clone();
+            return null;
         }
-        if (array instanceof long[])
+        Class clazz = array.getClass();
+        if (!clazz.isArray())
         {
-            return ((long[]) array).clone();
+            return null;
         }
-        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;
+
+        Class type = clazz.getComponentType();
+        int length = Array.getLength(array);
+        Object clone = Array.newInstance(type, length);
+        System.arraycopy(array, 0, clone, 0, length);
+        return clone;
     }
 
 }

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

Reply via email to