Date: 2004-11-07T03:53:17
Editor: ShinobuKawai <[EMAIL PROTECTED]>
Wiki: Jakarta-Velocity Wiki
Page: ArrayTool
URL: http://wiki.apache.org/jakarta-velocity/ArrayTool
Some refactoring.
Change Log:
------------------------------------------------------------------------------
@@ -19,7 +19,6 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
/**
@@ -83,19 +82,10 @@
*/
public List list(Object array)
{
- if (array == null)
+ if (!this.isArray(array))
{
return null;
}
- if (!array.getClass().isArray())
- {
- return null;
- }
- if (array instanceof Object[])
- {
- Object[] objects = (Object[]) array;
- return Arrays.asList(objects);
- }
// Thanks to Eric Fixler for this refactor.
int length = Array.getLength(array);
@@ -117,15 +107,11 @@
* </ul>
* @param array the array object.
* @param index the index of the element to get.
- * @return the specified element of an array.
+ * @return the specified element of the array.
*/
public Object get(Object array, int index)
{
- if (array == null)
- {
- return null;
- }
- if (!array.getClass().isArray())
+ if (!this.isArray(array))
{
return null;
}
@@ -143,11 +129,7 @@
public Object set(Object array, int index, Object value)
{
- if (array == null)
- {
- return null;
- }
- if (!array.getClass().isArray())
+ if (!this.isArray(array))
{
return null;
}
@@ -172,15 +154,11 @@
* <li><code>array</code> is not an array.</li>
* </ul>
* @param array the array object.
- * @return the length of an array.
+ * @return the length of the array.
*/
public Integer length(Object array)
{
- if (array == null)
- {
- return null;
- }
- if (!array.getClass().isArray())
+ if (!this.isArray(array))
{
return null;
}
@@ -189,23 +167,47 @@
return new Integer(Array.getLength(array));
}
+ /**
+ * Gets the clone of an array.
+ * It will return null under the following conditions:
+ * <ul>
+ * <li><code>array</code> is null.</li>
+ * <li><code>array</code> is not an array.</li>
+ * </ul>
+ * @param array the array object.
+ * @return the clone of the array.
+ */
public Object clone(Object array)
{
- if (array == null)
- {
- return null;
- }
- Class clazz = array.getClass();
- if (!clazz.isArray())
+ if (!this.isArray(array))
{
return null;
}
- Class type = clazz.getComponentType();
+ Class type = array.getClass().getComponentType();
int length = Array.getLength(array);
Object clone = Array.newInstance(type, length);
System.arraycopy(array, 0, clone, 0, length);
return clone;
+ }
+
+ /**
+ * Checks if an object is an array.
+ * @param object the object to check.
+ * @return <code>true</code> if the object is an array.
+ */
+ private boolean isArray(Object object)
+ {
+ if (object == null)
+ {
+ return false;
+ }
+ if (!object.getClass().isArray())
+ {
+ return false;
+ }
+
+ return true;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]