PR: Bugzilla Bug 27661 [lang] [patch] ArrayUtils.addAll doesn't always return new array

Submitted by:	Maarten Coene
Reviewed by:	
Gary Gregory


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137827 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2004-03-16 01:40:57 +00:00
parent 694d1de4a9
commit d58c692b20
2 changed files with 10 additions and 7 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.42 2004/02/18 22:59:50 ggregory Exp $
* @version $Id: ArrayUtils.java,v 1.43 2004/03/16 01:40:57 ggregory Exp $
*/
public class ArrayUtils {
@ -2768,12 +2768,13 @@ public static boolean isEmpty(final boolean[] array) {
/**
* <p>Adds all the elements of the provided 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>.</p>
* by all of the elements <code>array2</code>. If an array is returned, it is always
* a new array.</p>
*
* <pre>
* ArrayUtils.addAll(null, null) = null
* ArrayUtils.addAll(array1, null) = array1
* ArrayUtils.addAll(null, array2) = array2
* ArrayUtils.addAll(array1, null) = cloned copy of array1
* ArrayUtils.addAll(null, array2) = cloned copy of array2
* ArrayUtils.addAll([], []) = []
* ArrayUtils.addAll([null], [null]) = [null, null]
* ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
@ -2787,9 +2788,9 @@ public static boolean isEmpty(final boolean[] array) {
*/
public static Object[] addAll(Object[] array1, Object[] array2) {
if (array1 == null) {
return array2;
return clone(array2);
} else if (array2 == null) {
return array1;
return clone(array1);
} else {
Object[] joinedArray = (Object[]) Array.newInstance(array1.getClass().getComponentType(), array1.length
+ array2.length);

View File

@ -27,7 +27,7 @@
* Tests ArrayUtils add methods.
*
* @author Gary D. Gregory
* @version $Id: ArrayUtilsAddTest.java,v 1.2 2004/02/18 23:06:19 ggregory Exp $
* @version $Id: ArrayUtilsAddTest.java,v 1.3 2004/03/16 01:40:57 ggregory Exp $
*/
public class ArrayUtilsAddTest extends TestCase {
public static void main(String[] args) {
@ -197,10 +197,12 @@ public void testAddObjectArrayToObjectArray() {
String[] stringArray1 = new String[]{"a", "b", "c"};
String[] stringArray2 = new String[]{"1", "2", "3"};
newArray = ArrayUtils.addAll(stringArray1, null);
assertNotSame(stringArray1, newArray);
assertTrue(Arrays.equals(stringArray1, newArray));
assertTrue(Arrays.equals((new String[]{"a", "b", "c"}), newArray));
assertEquals(String.class, newArray.getClass().getComponentType());
newArray = ArrayUtils.addAll(null, stringArray2);
assertNotSame(stringArray2, newArray);
assertTrue(Arrays.equals(stringArray2, newArray));
assertTrue(Arrays.equals((new String[]{"1", "2", "3"}), newArray));
assertEquals(String.class, newArray.getClass().getComponentType());