overloaded addAll for all primitives

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@138015 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2005-01-27 06:43:20 +00:00
parent 838c1e7d0e
commit 4af43102aa
2 changed files with 273 additions and 9 deletions

View File

@ -44,7 +44,7 @@
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
* @author Maarten Coene
* @since 2.0
* @version $Id: ArrayUtils.java,v 1.50 2005/01/22 04:22:12 bayard Exp $
* @version $Id: ArrayUtils.java,v 1.51 2005/01/27 06:43:20 bayard Exp $
*/
public class ArrayUtils {
@ -2887,13 +2887,244 @@ public static Object[] addAll(Object[] array1, Object[] array2) {
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;
}
/**

View File

@ -27,7 +27,7 @@
* Tests ArrayUtils add methods.
*
* @author Gary D. Gregory
* @version $Id: ArrayUtilsAddTest.java,v 1.5 2005/01/22 04:22:12 bayard Exp $
* @version $Id: ArrayUtilsAddTest.java,v 1.6 2005/01/27 06:43:20 bayard Exp $
*/
public class ArrayUtilsAddTest extends TestCase {
public static void main(String[] args) {
@ -210,7 +210,7 @@ public void testAddObjectArrayObject() {
}
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 @@ public void testAddObjectArrayToObjectArray() {
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() {