Date: 2004-11-07T02:22:42
Editor: ShinobuKawai <[EMAIL PROTECTED]>
Wiki: Jakarta-Velocity Wiki
Page: ArrayTool
URL: http://wiki.apache.org/jakarta-velocity/ArrayTool
Some refactoring.
Change Log:
------------------------------------------------------------------------------
@@ -17,6 +17,7 @@
package org.apache.velocity.tools.generic;
+import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -81,96 +82,32 @@
*/
public List list(Object array)
{
+ if (array == null)
+ {
+ return null;
+ }
if (array instanceof List)
{
return (List) array;
}
+ if (!array.getClass().isArray())
+ {
+ return null;
+ }
if (array instanceof Object[])
{
Object[] objects = (Object[]) array;
return Arrays.asList(objects);
}
- if (array instanceof long[])
+
+ // Thanks to Eric Fixler for this refactor.
+ int length = Array.getLength(array);
+ List asList = new ArrayList(length);
+ for (int index = 0; index < length; ++index)
{
- long[] longs = (long[]) array;
- List asList = new ArrayList(longs.length);
- for (int index = 0; index < longs.length; ++index)
- {
- asList.add(new Long(longs[index]));
- }
- return asList;
- }
- if (array instanceof int[])
- {
- int[] ints = (int[]) array;
- List asList = new ArrayList(ints.length);
- for (int index = 0; index < ints.length; ++index)
- {
- asList.add(new Integer(ints[index]));
- }
- return asList;
- }
- if (array instanceof short[])
- {
- short[] shorts = (short[]) array;
- List asList = new ArrayList(shorts.length);
- for (int index = 0; index < shorts.length; ++index)
- {
- asList.add(new Short(shorts[index]));
- }
- return asList;
- }
- if (array instanceof byte[])
- {
- byte[] bytes = (byte[]) array;
- List asList = new ArrayList(bytes.length);
- for (int index = 0; index < bytes.length; ++index)
- {
- asList.add(new Byte(bytes[index]));
- }
- return asList;
- }
- if (array instanceof char[])
- {
- char[] chars = (char[]) array;
- List asList = new ArrayList(chars.length);
- for (int index = 0; index < chars.length; ++index)
- {
- asList.add(new Character(chars[index]));
- }
- return asList;
- }
- if (array instanceof double[])
- {
- double[] doubles = (double[]) array;
- List asList = new ArrayList(doubles.length);
- for (int index = 0; index < doubles.length; ++index)
- {
- asList.add(new Double(doubles[index]));
- }
- return asList;
- }
- if (array instanceof float[])
- {
- float[] floats = (float[]) array;
- List asList = new ArrayList(floats.length);
- for (int index = 0; index < floats.length; ++index)
- {
- asList.add(new Float(floats[index]));
- }
- return asList;
- }
- if (array instanceof boolean[])
- {
- boolean[] booleans = (boolean[]) array;
- List asList = new ArrayList(booleans.length);
- for (int index = 0; index < booleans.length; ++index)
- {
- asList.add(new Boolean(booleans[index]));
- }
- return asList;
+ asList.add(Array.get(array, index));
}
- return null;
+ return asList;
}
/**
@@ -193,6 +130,7 @@
}
catch (RuntimeException e)
{
+ // It wasn't an array, or the index was wrong.
}
return null;
}
@@ -209,14 +147,21 @@
*/
public Integer length(Object array)
{
- try
+ if (array == null)
{
- return new Integer(this.list(array).size());
+ return null;
}
- catch (RuntimeException e)
+ if (array instanceof List)
{
+ return new Integer(((List) array).size());
}
- return null;
+ if (!array.getClass().isArray())
+ {
+ return null;
+ }
+
+ // Thanks to Eric Fixler for this refactor.
+ return new Integer(Array.getLength(array));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]