Three new methods and unit tests: add(Object[],Object), add(Object[],int,Object), and join(Object[],Object[]).

PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=26056
Submitted by:	Gary Gregory and Maarten Coene
Reviewed by:	Gary Gregory


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137762 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2004-01-30 01:39:58 +00:00
parent 93199975fa
commit b4ec10a316
2 changed files with 222 additions and 2 deletions

View File

@ -82,7 +82,7 @@
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
* @author Fredrik Westermarck
* @since 2.0
* @version $Id: ArrayUtils.java,v 1.34 2004/01/25 00:09:10 tobrien Exp $
* @version $Id: ArrayUtils.java,v 1.35 2004/01/30 01:39:58 ggregory Exp $
*/
public class ArrayUtils {
@ -2752,4 +2752,127 @@ public static boolean isEmpty(final boolean[] array) {
}
return false;
}
/**
* <p>Joins the elements of the provided arrays into a single new array.</p>
* <p>The new array contains all of the element of the first array followed
* by all of the elements from the second array.</p>
*
* <pre>
* ArrayUtils.join(null, null) = null
* ArrayUtils.join(array1, null) = array1
* ArrayUtils.join(null, array2) = array2
* ArrayUtils.join([], []) = []
* ArrayUtils.join([null], [null]) = [null, null]
* ArrayUtils.join(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
* </pre>
*
* @param array1 the first array of values to join together, may be null
* @param array2 the second array of values to join together, may be null
* @return The new joined array, <code>null</code> if null array inputs.
* The type of the joined array is the type of the first array.
* @since 2.1
*/
public static Object[] join(Object[] array1, Object[] array2) {
if (array1 == null) {
return array2;
} else if (array2 == null) {
return 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;
}
}
/**
* <p>Adds the element to the end of the array.</p>
*
* <p>The new array contains the same elements of the input
* array plus the given element in the last position. The component type of
* the new array is 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, null) = [null]
* ArrayUtils.add(null, "a") = ["a"]
* ArrayUtils.add(["a"], null) = ["a", null]
* ArrayUtils.add(["a"], "b") = ["a", "b"]
* ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
* </pre>
*
* @param array the array to "add" the element to, may be <code>null</code>
* @param element the object to add
* @return A new array containing the existing elements and the new element
* @since 2.1
*/
public static Object[] add(Object[] array, Object element) {
Object joinedArray;
int elementPos;
if (array != null) {
joinedArray = Array.newInstance(array.getClass().getComponentType(), array.length + 1);
System.arraycopy(array, 0, joinedArray, 0, array.length);
elementPos = array.length;
} else {
// null input array, use the element type
joinedArray = Array.newInstance(element != null ? element.getClass() : Object.class, 1);
elementPos = 0;
}
Array.set(joinedArray, elementPos, element);
return (Object[]) joinedArray;
}
/**
* <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, null) = [null]
* ArrayUtils.add(null, 0, "a") = ["a"]
* ArrayUtils.add(["a"], 1, null) = ["a", null]
* ArrayUtils.add(["a"], 1, "b") = ["a", "b"]
* ArrayUtils.add(["a", "b"], 3, "c") = ["a", "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 Object[] add(final Object[] array, final int index, final Object element) {
if (array == null) {
if (index != 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0");
}
Object joinedArray = Array.newInstance(element != null ? element.getClass() : Object.class, 1);
Array.set(joinedArray, 0, element);
return (Object[]) joinedArray;
}
int length = array.length;
if (index > length || index < 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
}
Object result = Array.newInstance(array.getClass().getComponentType(), 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;
}
}

View File

@ -75,7 +75,7 @@
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
* @author Fredrik Westermarck
* @author Gary Gregory
* @version $Id: ArrayUtilsTest.java,v 1.21 2004/01/19 22:55:05 ggregory Exp $
* @version $Id: ArrayUtilsTest.java,v 1.22 2004/01/30 01:39:57 ggregory Exp $
*/
public class ArrayUtilsTest extends TestCase {
@ -111,6 +111,103 @@ public void testConstructor() {
assertEquals(false, Modifier.isFinal(ArrayUtils.class.getModifiers()));
}
void assertArraysEquals(Object[] array1, Object[] array2) {
assertTrue(Arrays.equals(array1, array2));
}
public void testJoin() {
assertNull(ArrayUtils.join(null, null));
Object[] joinedArray;
String[] stringArray1 = new String[]{"a", "b", "c"};
String[] stringArray2 = new String[]{"1", "2", "3"};
joinedArray = ArrayUtils.join(stringArray1, null);
assertArraysEquals(stringArray1, joinedArray);
assertArraysEquals(new String[]{"a", "b", "c"}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.join(null, stringArray2);
assertArraysEquals(stringArray2, joinedArray);
assertArraysEquals(new String[]{"1", "2", "3"}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.join(stringArray1, stringArray2);
assertArraysEquals(new String[]{"a", "b", "c", "1", "2", "3"}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.join(ArrayUtils.EMPTY_STRING_ARRAY, null);
assertArraysEquals(ArrayUtils.EMPTY_STRING_ARRAY, joinedArray);
assertArraysEquals(new String[]{}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.join(null, ArrayUtils.EMPTY_STRING_ARRAY);
assertArraysEquals(ArrayUtils.EMPTY_STRING_ARRAY, joinedArray);
assertArraysEquals(new String[]{}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.join(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.EMPTY_STRING_ARRAY);
assertArraysEquals(ArrayUtils.EMPTY_STRING_ARRAY, joinedArray);
assertArraysEquals(new String[]{}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
String[] stringArrayNull = new String []{null};
joinedArray = ArrayUtils.join(stringArrayNull, stringArrayNull);
assertArraysEquals(new String[]{null, null}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
}
public void testAddObjectArrayObject() {
Object[] joinedArray;
joinedArray = ArrayUtils.add((Object[])null, null);
assertArraysEquals(new Object[]{null}, joinedArray);
assertEquals(Object.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.add((Object[])null, "a");
assertArraysEquals(new String[]{"a"}, joinedArray);
assertArraysEquals(new Object[]{"a"}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
String[] stringArray1 = new String[]{"a", "b", "c"};
joinedArray = ArrayUtils.add(stringArray1, null);
assertArraysEquals(new String[]{"a", "b", "c", null}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.add(stringArray1, "d");
assertArraysEquals(new String[]{"a", "b", "c", "d"}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
}
public void testAddAtIndex() {
Object[] joinedArray;
joinedArray = ArrayUtils.add((Object[])null, 0, null);
assertArraysEquals(new Object[]{null}, joinedArray);
assertEquals(Object.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.add((Object[])null, 0, "a");
assertArraysEquals(new String[]{"a"}, joinedArray);
assertArraysEquals(new Object[]{"a"}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
String[] stringArray1 = new String[]{"a", "b", "c"};
joinedArray = ArrayUtils.add(stringArray1, 0, null);
assertArraysEquals(new String[]{null, "a", "b", "c"}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.add(stringArray1, 1, null);
assertArraysEquals(new String[]{"a", null, "b", "c"}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.add(stringArray1, 3, null);
assertArraysEquals(new String[]{"a", "b", "c", null}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
joinedArray = ArrayUtils.add(stringArray1, 3, "d");
assertArraysEquals(new String[]{"a", "b", "c", "d"}, joinedArray);
assertEquals(String.class, joinedArray.getClass().getComponentType());
Object[] o = new Object[] {"1", "2", "4"};
Object[] result = ArrayUtils.add(o, 2, "3");
Object[] result2 = ArrayUtils.add(o, 3, "5");
assertNotNull(result);
assertEquals(4, result.length);
assertEquals("1", result[0]);
assertEquals("2", result[1]);
assertEquals("3", result[2]);
assertEquals("4", result[3]);
assertNotNull(result2);
assertEquals(4, result2.length);
assertEquals("1", result2[0]);
assertEquals("2", result2[1]);
assertEquals("4", result2[2]);
assertEquals("5", result2[3]);
}
//-----------------------------------------------------------------------
public void testToString() {
assertEquals("{}", ArrayUtils.toString(null));