bayard 2005/01/26 22:43:20
Modified: lang/src/java/org/apache/commons/lang ArrayUtils.java
lang/src/test/org/apache/commons/lang ArrayUtilsAddTest.java
Log:
overloaded addAll for all primitives
Revision Changes Path
1.51 +238 -7
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.50
retrieving revision 1.51
diff -u -r1.50 -r1.51
--- ArrayUtils.java 22 Jan 2005 04:22:12 -0000 1.50
+++ ArrayUtils.java 27 Jan 2005 06:43:20 -0000 1.51
@@ -2887,13 +2887,244 @@
return clone(array2);
} else if (array2 == null) {
return clone(array1);
- } else {
- Object[] joinedArray = (Object[])
Array.newInstance(array1.getClass().getComponentType(), array1.length
- + array2.length);
- System.arraycopy(array1, 0, joinedArray, 0, array1.length);
- System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
- return joinedArray;
}
+ Object[] joinedArray = (Object[])
Array.newInstance(array1.getClass().getComponentType(),
+ array1.length +
array2.length);
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
+ return joinedArray;
+ }
+
+ /**
+ * <p>Adds all the elements of the given arrays into a new array.</p>
+ * <p>The new array contains all of the element of <code>array1</code>
followed
+ * by all of the elements <code>array2</code>. When an array is
returned, it is always
+ * a new array.</p>
+ *
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
+ *
+ * @param array1 the first array whose elements are added to the new
array.
+ * @param array2 the second array whose elements are added to the new
array.
+ * @return The new boolean[] array.
+ * @since 2.1
+ */
+ public static boolean[] addAll(boolean[] array1, boolean[] array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
+ }
+ boolean[] joinedArray = new boolean[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
+ return joinedArray;
+ }
+
+ /**
+ * <p>Adds all the elements of the given arrays into a new array.</p>
+ * <p>The new array contains all of the element of <code>array1</code>
followed
+ * by all of the elements <code>array2</code>. When an array is
returned, it is always
+ * a new array.</p>
+ *
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
+ *
+ * @param array1 the first array whose elements are added to the new
array.
+ * @param array2 the second array whose elements are added to the new
array.
+ * @return The new char[] array.
+ * @since 2.1
+ */
+ public static char[] addAll(char[] array1, char[] array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
+ }
+ char[] joinedArray = new char[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
+ return joinedArray;
+ }
+
+ /**
+ * <p>Adds all the elements of the given arrays into a new array.</p>
+ * <p>The new array contains all of the element of <code>array1</code>
followed
+ * by all of the elements <code>array2</code>. When an array is
returned, it is always
+ * a new array.</p>
+ *
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
+ *
+ * @param array1 the first array whose elements are added to the new
array.
+ * @param array2 the second array whose elements are added to the new
array.
+ * @return The new byte[] array.
+ * @since 2.1
+ */
+ public static byte[] addAll(byte[] array1, byte[] array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
+ }
+ byte[] joinedArray = new byte[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
+ return joinedArray;
+ }
+
+ /**
+ * <p>Adds all the elements of the given arrays into a new array.</p>
+ * <p>The new array contains all of the element of <code>array1</code>
followed
+ * by all of the elements <code>array2</code>. When an array is
returned, it is always
+ * a new array.</p>
+ *
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
+ *
+ * @param array1 the first array whose elements are added to the new
array.
+ * @param array2 the second array whose elements are added to the new
array.
+ * @return The new short[] array.
+ * @since 2.1
+ */
+ public static short[] addAll(short[] array1, short[] array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
+ }
+ short[] joinedArray = new short[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
+ return joinedArray;
+ }
+
+ /**
+ * <p>Adds all the elements of the given arrays into a new array.</p>
+ * <p>The new array contains all of the element of <code>array1</code>
followed
+ * by all of the elements <code>array2</code>. When an array is
returned, it is always
+ * a new array.</p>
+ *
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
+ *
+ * @param array1 the first array whose elements are added to the new
array.
+ * @param array2 the second array whose elements are added to the new
array.
+ * @return The new int[] array.
+ * @since 2.1
+ */
+ public static int[] addAll(int[] array1, int[] array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
+ }
+ int[] joinedArray = new int[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
+ return joinedArray;
+ }
+
+ /**
+ * <p>Adds all the elements of the given arrays into a new array.</p>
+ * <p>The new array contains all of the element of <code>array1</code>
followed
+ * by all of the elements <code>array2</code>. When an array is
returned, it is always
+ * a new array.</p>
+ *
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
+ *
+ * @param array1 the first array whose elements are added to the new
array.
+ * @param array2 the second array whose elements are added to the new
array.
+ * @return The new long[] array.
+ * @since 2.1
+ */
+ public static long[] addAll(long[] array1, long[] array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
+ }
+ long[] joinedArray = new long[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
+ return joinedArray;
+ }
+
+ /**
+ * <p>Adds all the elements of the given arrays into a new array.</p>
+ * <p>The new array contains all of the element of <code>array1</code>
followed
+ * by all of the elements <code>array2</code>. When an array is
returned, it is always
+ * a new array.</p>
+ *
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
+ *
+ * @param array1 the first array whose elements are added to the new
array.
+ * @param array2 the second array whose elements are added to the new
array.
+ * @return The new float[] array.
+ * @since 2.1
+ */
+ public static float[] addAll(float[] array1, float[] array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
+ }
+ float[] joinedArray = new float[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
+ return joinedArray;
+ }
+
+ /**
+ * <p>Adds all the elements of the given arrays into a new array.</p>
+ * <p>The new array contains all of the element of <code>array1</code>
followed
+ * by all of the elements <code>array2</code>. When an array is
returned, it is always
+ * a new array.</p>
+ *
+ * <pre>
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1
+ * ArrayUtils.addAll(null, array2) = cloned copy of array2
+ * ArrayUtils.addAll([], []) = []
+ * </pre>
+ *
+ * @param array1 the first array whose elements are added to the new
array.
+ * @param array2 the second array whose elements are added to the new
array.
+ * @return The new double[] array.
+ * @since 2.1
+ */
+ public static double[] addAll(double[] array1, double[] array2) {
+ if (array1 == null) {
+ return clone(array2);
+ } else if (array2 == null) {
+ return clone(array1);
+ }
+ double[] joinedArray = new double[array1.length + array2.length];
+ System.arraycopy(array1, 0, joinedArray, 0, array1.length);
+ System.arraycopy(array2, 0, joinedArray, array1.length,
array2.length);
+ return joinedArray;
}
/**
1.6 +35 -2
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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ArrayUtilsAddTest.java 22 Jan 2005 04:22:12 -0000 1.5
+++ ArrayUtilsAddTest.java 27 Jan 2005 06:43:20 -0000 1.6
@@ -210,7 +210,7 @@
}
public void testAddObjectArrayToObjectArray() {
- assertNull(ArrayUtils.addAll(null, null));
+ assertNull(ArrayUtils.addAll((Object[]) null, (Object[]) null));
Object[] newArray;
String[] stringArray1 = new String[]{"a", "b", "c"};
String[] stringArray2 = new String[]{"1", "2", "3"};
@@ -243,6 +243,39 @@
newArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull);
assertTrue(Arrays.equals((new String[]{null, null}), newArray));
assertEquals(String.class, newArray.getClass().getComponentType());
+
+ // boolean
+ assertTrue( Arrays.equals( new boolean[] { true, false, false, true
},
+ ArrayUtils.addAll( new boolean[] { true, false }, new boolean[]
{ false, true } ) ) );
+
+ // char
+ assertTrue( Arrays.equals( new char[] { 'a', 'b', 'c', 'd' },
+ ArrayUtils.addAll( new char[] { 'a', 'b' }, new char[] { 'c',
'd' } ) ) );
+
+ // byte
+ assertTrue( Arrays.equals( new byte[] { (byte) 0, (byte) 1, (byte)
2, (byte) 3 },
+ ArrayUtils.addAll( new byte[] { (byte) 0, (byte) 1 }, new byte[]
{ (byte) 2, (byte) 3 } ) ) );
+
+ // short
+ assertTrue( Arrays.equals( new short[] { (short) 10, (short) 20,
(short) 30, (short) 40 },
+ ArrayUtils.addAll( new short[] { (short) 10, (short) 20 }, new
short[] { (short) 30, (short) 40 } ) ) );
+
+ // int
+ assertTrue( Arrays.equals( new int[] { 1, 1000, -1000, -1 },
+ ArrayUtils.addAll( new int[] { 1, 1000 }, new int[] { -1000, -1
} ) ) );
+
+ // long
+ assertTrue( Arrays.equals( new long[] { 1L, -1L, 1000L, -1000L },
+ ArrayUtils.addAll( new long[] { 1L, -1L }, new long[] { 1000L,
-1000L } ) ) );
+
+ // float
+ assertTrue( Arrays.equals( new float[] { 10.5f, 10.1f, 1.6f, 0.01f
},
+ ArrayUtils.addAll( new float[] { 10.5f, 10.1f }, new float[] {
1.6f, 0.01f } ) ) );
+
+ // double
+ assertTrue( Arrays.equals( new double[] { Math.PI, -Math.PI, 0, 9.99
},
+ ArrayUtils.addAll( new double[] { Math.PI, -Math.PI }, new
double[] { 0, 9.99 } ) ) );
+
}
public void testAddObjectAtIndex() {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]