commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang ArrayUtils.java
Date Fri, 30 Jan 2004 01:39:58 GMT
ggregory    2004/01/29 17:39:58

  Modified:    lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
               lang/src/java/org/apache/commons/lang ArrayUtils.java
  Log:
  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
  
  Revision  Changes    Path
  1.22      +98 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java
  
  Index: ArrayUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- ArrayUtilsTest.java	19 Jan 2004 22:55:05 -0000	1.21
  +++ ArrayUtilsTest.java	30 Jan 2004 01:39:57 -0000	1.22
  @@ -111,6 +111,103 @@
           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));
  
  
  
  1.35      +124 -1    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.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- ArrayUtils.java	25 Jan 2004 00:09:10 -0000	1.34
  +++ ArrayUtils.java	30 Jan 2004 01:39:58 -0000	1.35
  @@ -2752,4 +2752,127 @@
           }
           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;
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message