bayard 2005/01/21 20:22:13
Modified: lang/src/java/org/apache/commons/lang ArrayUtils.java
lang/src/test/org/apache/commons/lang ArrayUtilsAddTest.java
Log:
primitive overloads added for add(array, int index, element)
Revision Changes Path
1.50 +268 -6
jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java
Index: ArrayUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -r1.49 -r1.50
--- ArrayUtils.java 10 Oct 2004 18:56:16 -0000 1.49
+++ ArrayUtils.java 22 Jan 2005 04:22:12 -0000 1.50
@@ -3191,25 +3191,287 @@
* (index < 0 || index > array.length).
*/
public static Object[] add(Object[] array, int index, Object element) {
+ Class clss = null;
+ if(array != null) {
+ clss = array.getClass().getComponentType();
+ } else
+ if(element != null) {
+ clss = element.getClass();
+ } else {
+ return new Object[] { null };
+ }
+ return (Object[]) add( (Object) array, index, element, clss );
+ }
+
+ /**
+ * <p>Inserts the specified element at the specified position in the
array.
+ * Shifts the element currently at that position (if any) and any
subsequent
+ * elements to the right (adds one to their indices).</p>
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.</p>
+ *
+ * <p>If the input array is <code>null</code>, a new one element array
is returned
+ * whose component type is the same as the element.</p>
+ *
+ * <pre>
+ * ArrayUtils.add(null, 0, true) = [true]
+ * ArrayUtils.add([true], 0, false) = [false, true]
+ * ArrayUtils.add([false], 1, true) = [false, true]
+ * ArrayUtils.add([true, false], 1, true) = [true, true, false]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be
<code>null</code>
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new
element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ */
+ public static boolean[] add(boolean[] array, int index, boolean element)
{
+ return (boolean[]) add( (Object) array, index, new Boolean(element),
Boolean.TYPE );
+ }
+
+ /**
+ * <p>Inserts the specified element at the specified position in the
array.
+ * Shifts the element currently at that position (if any) and any
subsequent
+ * elements to the right (adds one to their indices).</p>
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.</p>
+ *
+ * <p>If the input array is <code>null</code>, a new one element array
is returned
+ * whose component type is the same as the element.</p>
+ *
+ * <pre>
+ * ArrayUtils.add(null, 0, 'a') = ['a']
+ * ArrayUtils.add(['a'], 0, 'b') = ['b', 'a']
+ * ArrayUtils.add(['a', 'b'], 0, 'c') = ['c', 'a', 'b']
+ * ArrayUtils.add(['a', 'b'], 1, 'k') = ['a', 'k', 'b']
+ * ArrayUtils.add(['a', 'b', 'c'], 1, 't') = ['a', 't', 'b', 'c']
+ * </pre>
+ *
+ * @param array the array to add the element to, may be
<code>null</code>
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new
element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ */
+ public static char[] add(char[] array, int index, char element) {
+ return (char[]) add( (Object) array, index, new Character(element),
Character.TYPE );
+ }
+
+ /**
+ * <p>Inserts the specified element at the specified position in the
array.
+ * Shifts the element currently at that position (if any) and any
subsequent
+ * elements to the right (adds one to their indices).</p>
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.</p>
+ *
+ * <p>If the input array is <code>null</code>, a new one element array
is returned
+ * whose component type is the same as the element.</p>
+ *
+ * <pre>
+ * ArrayUtils.add([1], 0, 2) = [2, 1]
+ * ArrayUtils.add([2, 6], 2, 3) = [2, 6, 3]
+ * ArrayUtils.add([2, 6], 0, 1) = [1, 2, 6]
+ * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be
<code>null</code>
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new
element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ */
+ public static byte[] add(byte[] array, int index, byte element) {
+ return (byte[]) add( (Object) array, index, new Byte(element),
Byte.TYPE );
+ }
+
+ /**
+ * <p>Inserts the specified element at the specified position in the
array.
+ * Shifts the element currently at that position (if any) and any
subsequent
+ * elements to the right (adds one to their indices).</p>
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.</p>
+ *
+ * <p>If the input array is <code>null</code>, a new one element array
is returned
+ * whose component type is the same as the element.</p>
+ *
+ * <pre>
+ * ArrayUtils.add([1], 0, 2) = [2, 1]
+ * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
+ * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
+ * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be
<code>null</code>
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new
element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ */
+ public static short[] add(short[] array, int index, short element) {
+ return (short[]) add( (Object) array, index, new Short(element),
Short.TYPE );
+ }
+
+ /**
+ * <p>Inserts the specified element at the specified position in the
array.
+ * Shifts the element currently at that position (if any) and any
subsequent
+ * elements to the right (adds one to their indices).</p>
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.</p>
+ *
+ * <p>If the input array is <code>null</code>, a new one element array
is returned
+ * whose component type is the same as the element.</p>
+ *
+ * <pre>
+ * ArrayUtils.add([1], 0, 2) = [2, 1]
+ * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
+ * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
+ * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be
<code>null</code>
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new
element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ */
+ public static int[] add(int[] array, int index, int element) {
+ return (int[]) add( (Object) array, index, new Integer(element),
Integer.TYPE );
+ }
+
+ /**
+ * <p>Inserts the specified element at the specified position in the
array.
+ * Shifts the element currently at that position (if any) and any
subsequent
+ * elements to the right (adds one to their indices).</p>
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.</p>
+ *
+ * <p>If the input array is <code>null</code>, a new one element array
is returned
+ * whose component type is the same as the element.</p>
+ *
+ * <pre>
+ * ArrayUtils.add([1L], 0, 2L) = [2L, 1L]
+ * ArrayUtils.add([2L, 6L], 2, 10L) = [2L, 6L, 10L]
+ * ArrayUtils.add([2L, 6L], 0, -4L) = [-4L, 2L, 6L]
+ * ArrayUtils.add([2L, 6L, 3L], 2, 1L) = [2L, 6L, 1L, 3L]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be
<code>null</code>
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new
element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ */
+ public static long[] add(long[] array, int index, long element) {
+ return (long[]) add( (Object) array, index, new Long(element),
Long.TYPE );
+ }
+
+ /**
+ * <p>Inserts the specified element at the specified position in the
array.
+ * Shifts the element currently at that position (if any) and any
subsequent
+ * elements to the right (adds one to their indices).</p>
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.</p>
+ *
+ * <p>If the input array is <code>null</code>, a new one element array
is returned
+ * whose component type is the same as the element.</p>
+ *
+ * <pre>
+ * ArrayUtils.add([1.1f], 0, 2.2f) = [2.2f, 1.1f]
+ * ArrayUtils.add([2.3f, 6.4f], 2, 10.5f) = [2.3f, 6.4f, 10.5f]
+ * ArrayUtils.add([2.6f, 6.7f], 0, -4.8f) = [-4.8f, 2.6f, 6.7f]
+ * ArrayUtils.add([2.9f, 6.0f, 0.3f], 2, 1.0f) = [2.9f, 6.0f, 1.0f,
0.3f]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be
<code>null</code>
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new
element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ */
+ public static float[] add(float[] array, int index, float element) {
+ return (float[]) add( (Object) array, index, new Float(element),
Float.TYPE );
+ }
+
+ /**
+ * <p>Inserts the specified element at the specified position in the
array.
+ * Shifts the element currently at that position (if any) and any
subsequent
+ * elements to the right (adds one to their indices).</p>
+ *
+ * <p>This method returns a new array with the same elements of the input
+ * array plus the given element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.</p>
+ *
+ * <p>If the input array is <code>null</code>, a new one element array
is returned
+ * whose component type is the same as the element.</p>
+ *
+ * <pre>
+ * ArrayUtils.add([1.1], 0, 2.2) = [2.2, 1.1]
+ * ArrayUtils.add([2.3, 6.4], 2, 10.5) = [2.3, 6.4, 10.5]
+ * ArrayUtils.add([2.6, 6.7], 0, -4.8) = [-4.8, 2.6, 6.7]
+ * ArrayUtils.add([2.9, 6.0, 0.3], 2, 1.0) = [2.9, 6.0, 1.0, 0.3]
+ * </pre>
+ *
+ * @param array the array to add the element to, may be
<code>null</code>
+ * @param index the position of the new object
+ * @param element the object to add
+ * @return A new array containing the existing elements and the new
element
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index > array.length).
+ */
+ public static double[] add(double[] array, int index, double element) {
+ return (double[]) add( (Object) array, index, new Double(element),
Double.TYPE );
+ }
+
+ private static Object add(Object array, int index, Object element, Class
clss) {
if (array == null) {
if (index != 0) {
throw new IndexOutOfBoundsException("Index: " + index + ",
Length: 0");
}
- Object joinedArray = Array.newInstance(element != null ?
element.getClass() : Object.class, 1);
+ Object joinedArray = Array.newInstance(clss, 1);
Array.set(joinedArray, 0, element);
- return (Object[]) joinedArray;
+ return joinedArray;
}
- int length = array.length;
+ int length = Array.getLength(array);
if (index > length || index < 0) {
throw new IndexOutOfBoundsException("Index: " + index + ",
Length: " + length);
}
- Object result =
Array.newInstance(array.getClass().getComponentType(), length + 1);
+ Object result = Array.newInstance(clss, length + 1);
System.arraycopy(array, 0, result, 0, index);
Array.set(result, index, element);
if (index < length) {
System.arraycopy(array, index, result, index + 1, length -
index);
}
- return (Object[]) result;
+ return result;
}
/**
1.5 +83 -1
jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java
Index: ArrayUtilsAddTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ArrayUtilsAddTest.java 9 Oct 2004 11:55:51 -0000 1.4
+++ ArrayUtilsAddTest.java 22 Jan 2005 04:22:12 -0000 1.5
@@ -285,6 +285,88 @@
assertEquals("2", result2[1]);
assertEquals("4", result2[2]);
assertEquals("5", result2[3]);
+
+ // boolean tests
+ boolean[] booleanArray = ArrayUtils.add( null, 0, true );
+ assertTrue( Arrays.equals( new boolean[] { true }, booleanArray ) );
+ booleanArray = ArrayUtils.add( new boolean[] { true }, 0, false);
+ assertTrue( Arrays.equals( new boolean[] { false, true },
booleanArray ) );
+ booleanArray = ArrayUtils.add( new boolean[] { false }, 1, true);
+ assertTrue( Arrays.equals( new boolean[] { false, true },
booleanArray ) );
+ booleanArray = ArrayUtils.add( new boolean[] { true, false }, 1,
true);
+ assertTrue( Arrays.equals( new boolean[] { true, true, false },
booleanArray ) );
+
+ // char tests
+ char[] charArray = ArrayUtils.add( (char[]) null, 0, 'a' );
+ assertTrue( Arrays.equals( new char[] { 'a' }, charArray ) );
+ charArray = ArrayUtils.add( new char[] { 'a' }, 0, 'b');
+ assertTrue( Arrays.equals( new char[] { 'b', 'a' }, charArray ) );
+ charArray = ArrayUtils.add( new char[] { 'a', 'b' }, 0, 'c');
+ assertTrue( Arrays.equals( new char[] { 'c', 'a', 'b' }, charArray )
);
+ charArray = ArrayUtils.add( new char[] { 'a', 'b' }, 1, 'k');
+ assertTrue( Arrays.equals( new char[] { 'a', 'k', 'b' }, charArray )
);
+ charArray = ArrayUtils.add( new char[] { 'a', 'b', 'c' }, 1, 't');
+ assertTrue( Arrays.equals( new char[] { 'a', 't', 'b', 'c' },
charArray ) );
+
+ // short tests
+ short[] shortArray = ArrayUtils.add( new short[] { 1 }, 0, (short)
2);
+ assertTrue( Arrays.equals( new short[] { 2, 1 }, shortArray ) );
+ shortArray = ArrayUtils.add( new short[] { 2, 6 }, 2, (short) 10);
+ assertTrue( Arrays.equals( new short[] { 2, 6, 10 }, shortArray ) );
+ shortArray = ArrayUtils.add( new short[] { 2, 6 }, 0, (short) -4);
+ assertTrue( Arrays.equals( new short[] { -4, 2, 6 }, shortArray ) );
+ shortArray = ArrayUtils.add( new short[] { 2, 6, 3 }, 2, (short) 1);
+ assertTrue( Arrays.equals( new short[] { 2, 6, 1, 3 }, shortArray )
);
+
+ // byte tests
+ byte[] byteArray = ArrayUtils.add( new byte[] { 1 }, 0, (byte) 2);
+ assertTrue( Arrays.equals( new byte[] { 2, 1 }, byteArray ) );
+ byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 2, (byte) 3);
+ assertTrue( Arrays.equals( new byte[] { 2, 6, 3 }, byteArray ) );
+ byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 0, (byte) 1);
+ assertTrue( Arrays.equals( new byte[] { 1, 2, 6 }, byteArray ) );
+ byteArray = ArrayUtils.add( new byte[] { 2, 6, 3 }, 2, (byte) 1);
+ assertTrue( Arrays.equals( new byte[] { 2, 6, 1, 3 }, byteArray ) );
+
+ // int tests
+ int[] intArray = ArrayUtils.add( new int[] { 1 }, 0, 2);
+ assertTrue( Arrays.equals( new int[] { 2, 1 }, intArray ) );
+ intArray = ArrayUtils.add( new int[] { 2, 6 }, 2, 10);
+ assertTrue( Arrays.equals( new int[] { 2, 6, 10 }, intArray ) );
+ intArray = ArrayUtils.add( new int[] { 2, 6 }, 0, -4);
+ assertTrue( Arrays.equals( new int[] { -4, 2, 6 }, intArray ) );
+ intArray = ArrayUtils.add( new int[] { 2, 6, 3 }, 2, 1);
+ assertTrue( Arrays.equals( new int[] { 2, 6, 1, 3 }, intArray ) );
+
+ // long tests
+ long[] longArray = ArrayUtils.add( new long[] { 1L }, 0, 2L);
+ assertTrue( Arrays.equals( new long[] { 2L, 1L }, longArray ) );
+ longArray = ArrayUtils.add( new long[] { 2L, 6L }, 2, 10L);
+ assertTrue( Arrays.equals( new long[] { 2L, 6L, 10L }, longArray ) );
+ longArray = ArrayUtils.add( new long[] { 2L, 6L }, 0, -4L);
+ assertTrue( Arrays.equals( new long[] { -4L, 2L, 6L }, longArray ) );
+ longArray = ArrayUtils.add( new long[] { 2L, 6L, 3L }, 2, 1L);
+ assertTrue( Arrays.equals( new long[] { 2L, 6L, 1L, 3L }, longArray
) );
+
+ // float tests
+ float[] floatArray = ArrayUtils.add( new float[] { 1.1f }, 0, 2.2f);
+ assertTrue( Arrays.equals( new float[] { 2.2f, 1.1f }, floatArray )
);
+ floatArray = ArrayUtils.add( new float[] { 2.3f, 6.4f }, 2, 10.5f);
+ assertTrue( Arrays.equals( new float[] { 2.3f, 6.4f, 10.5f },
floatArray ) );
+ floatArray = ArrayUtils.add( new float[] { 2.6f, 6.7f }, 0, -4.8f);
+ assertTrue( Arrays.equals( new float[] { -4.8f, 2.6f, 6.7f },
floatArray ) );
+ floatArray = ArrayUtils.add( new float[] { 2.9f, 6.0f, 0.3f }, 2,
1.0f);
+ assertTrue( Arrays.equals( new float[] { 2.9f, 6.0f, 1.0f, 0.3f },
floatArray ) );
+
+ // double tests
+ double[] doubleArray = ArrayUtils.add( new double[] { 1.1 }, 0, 2.2);
+ assertTrue( Arrays.equals( new double[] { 2.2, 1.1 }, doubleArray )
);
+ doubleArray = ArrayUtils.add( new double[] { 2.3, 6.4 }, 2, 10.5);
+ assertTrue( Arrays.equals( new double[] { 2.3, 6.4, 10.5 },
doubleArray ) );
+ doubleArray = ArrayUtils.add( new double[] { 2.6, 6.7 }, 0, -4.8);
+ assertTrue( Arrays.equals( new double[] { -4.8, 2.6, 6.7 },
doubleArray ) );
+ doubleArray = ArrayUtils.add( new double[] { 2.9, 6.0, 0.3 }, 2,
1.0);
+ assertTrue( Arrays.equals( new double[] { 2.9, 6.0, 1.0, 0.3 },
doubleArray ) );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]