You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by gg...@apache.org on 2004/02/03 23:14:24 UTC

cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang ArrayUtils.java

ggregory    2004/02/03 14:14:24

  Modified:    lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
                        LangTestSuite.java
               lang/src/java/org/apache/commons/lang ArrayUtils.java
  Added:       lang/src/test/org/apache/commons/lang ArrayUtilsAddTest.java
  Log:
  Bugzilla Bug 26056 [lang] Add methods to ArrayUtils: add at end and insert-like ops
  Added methods: primitiveType[] add(primitiveType[], primitiveType)
  
  Revision  Changes    Path
  1.24      +1 -98     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.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- ArrayUtilsTest.java	31 Jan 2004 20:12:15 -0000	1.23
  +++ ArrayUtilsTest.java	3 Feb 2004 22:14:24 -0000	1.24
  @@ -111,103 +111,6 @@
           assertEquals(false, Modifier.isFinal(ArrayUtils.class.getModifiers()));
       }
       
  -    void assertArraysEquals(Object[] array1, Object[] array2) {
  -        assertTrue(Arrays.equals(array1, array2));
  -    }
  -    
  -    public void testJoin() {
  -        assertNull(ArrayUtils.addAll(null, null));
  -        Object[] joinedArray;
  -        String[] stringArray1 = new String[]{"a", "b", "c"};
  -        String[] stringArray2 = new String[]{"1", "2", "3"};
  -        joinedArray = ArrayUtils.addAll(stringArray1, null);
  -        assertArraysEquals(stringArray1, joinedArray);
  -        assertArraysEquals(new String[]{"a", "b", "c"}, joinedArray);
  -        assertEquals(String.class, joinedArray.getClass().getComponentType());
  -        joinedArray = ArrayUtils.addAll(null, stringArray2);
  -        assertArraysEquals(stringArray2, joinedArray);
  -        assertArraysEquals(new String[]{"1", "2", "3"}, joinedArray);
  -        assertEquals(String.class, joinedArray.getClass().getComponentType());
  -        joinedArray = ArrayUtils.addAll(stringArray1, stringArray2);
  -        assertArraysEquals(new String[]{"a", "b", "c", "1", "2", "3"}, joinedArray);
  -        assertEquals(String.class, joinedArray.getClass().getComponentType());
  -        joinedArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, null);
  -        assertArraysEquals(ArrayUtils.EMPTY_STRING_ARRAY, joinedArray);
  -        assertArraysEquals(new String[]{}, joinedArray);
  -        assertEquals(String.class, joinedArray.getClass().getComponentType());
  -        joinedArray = ArrayUtils.addAll(null, ArrayUtils.EMPTY_STRING_ARRAY);
  -        assertArraysEquals(ArrayUtils.EMPTY_STRING_ARRAY, joinedArray);
  -        assertArraysEquals(new String[]{}, joinedArray);
  -        assertEquals(String.class, joinedArray.getClass().getComponentType());
  -        joinedArray = ArrayUtils.addAll(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.addAll(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.25      +2 -1      jakarta-commons/lang/src/test/org/apache/commons/lang/LangTestSuite.java
  
  Index: LangTestSuite.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/LangTestSuite.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- LangTestSuite.java	31 Jan 2004 13:00:07 -0000	1.24
  +++ LangTestSuite.java	3 Feb 2004 22:14:24 -0000	1.25
  @@ -89,6 +89,7 @@
           TestSuite suite = new TestSuite();
           suite.setName("Commons-Lang Tests");
           suite.addTest(ArrayUtilsTest.suite());
  +        suite.addTest(ArrayUtilsAddTest.suite());
           suite.addTest(BitFieldTest.suite());
           suite.addTest(BooleanUtilsTest.suite());
           suite.addTest(CharRangeTest.suite());
  
  
  
  1.1                  jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java
  
  Index: ArrayUtilsAddTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.lang;
  
  import java.util.Arrays;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  /**
   * Tests ArrayUtils add methods.
   * 
   * @author Gary D. Gregory
   * @version $Id: ArrayUtilsAddTest.java,v 1.1 2004/02/03 22:14:24 ggregory Exp $
   */
  public class ArrayUtilsAddTest extends TestCase {
      public static void main(String[] args) {
          TestRunner.run(suite());
      }
  
      public static Test suite() {
          TestSuite suite = new TestSuite(ArrayUtilsAddTest.class);
          suite.setName("ArrayUtils add Tests");
          return suite;
      }
  
      public void testAddObjectArrayBoolean() {
          boolean[] newArray;
          newArray = ArrayUtils.add((boolean[])null, false);
          assertTrue(Arrays.equals(new boolean[]{false}, newArray));
          assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((boolean[])null, true);
          assertTrue(Arrays.equals(new boolean[]{true}, newArray));
          assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
          boolean[] array1 = new boolean[]{true, false, true};
          newArray = ArrayUtils.add(array1, false);
          assertTrue(Arrays.equals(new boolean[]{true, false, true, false}, newArray));
          assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
      }
      
      public void testAddObjectArrayByte() {
          byte[] newArray;
          newArray = ArrayUtils.add((byte[])null, (byte)0);
          assertTrue(Arrays.equals(new byte[]{0}, newArray));
          assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((byte[])null, (byte)1);
          assertTrue(Arrays.equals(new byte[]{1}, newArray));
          assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
          byte[] array1 = new byte[]{1, 2, 3};
          newArray = ArrayUtils.add(array1, (byte)0);
          assertTrue(Arrays.equals(new byte[]{1, 2, 3, 0}, newArray));
          assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(array1, (byte)4);
          assertTrue(Arrays.equals(new byte[]{1, 2, 3, 4}, newArray));
          assertEquals(Byte.TYPE, newArray.getClass().getComponentType());        
      }
      
      public void testAddObjectArrayChar() {
          char[] newArray;
          newArray = ArrayUtils.add((char[])null, (char)0);
          assertTrue(Arrays.equals(new char[]{0}, newArray));
          assertEquals(Character.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((char[])null, (char)1);
          assertTrue(Arrays.equals(new char[]{1}, newArray));
          assertEquals(Character.TYPE, newArray.getClass().getComponentType());
          char[] array1 = new char[]{1, 2, 3};
          newArray = ArrayUtils.add(array1, (char)0);
          assertTrue(Arrays.equals(new char[]{1, 2, 3, 0}, newArray));
          assertEquals(Character.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(array1, (char)4);
          assertTrue(Arrays.equals(new char[]{1, 2, 3, 4}, newArray));
          assertEquals(Character.TYPE, newArray.getClass().getComponentType());        
      }
      
      public void testAddObjectArrayDouble() {
          double[] newArray;
          newArray = ArrayUtils.add((double[])null, 0);
          assertTrue(Arrays.equals(new double[]{0}, newArray));
          assertEquals(Double.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((double[])null, 1);
          assertTrue(Arrays.equals(new double[]{1}, newArray));
          assertEquals(Double.TYPE, newArray.getClass().getComponentType());
          double[] array1 = new double[]{1, 2, 3};
          newArray = ArrayUtils.add(array1, 0);
          assertTrue(Arrays.equals(new double[]{1, 2, 3, 0}, newArray));
          assertEquals(Double.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(array1, 4);
          assertTrue(Arrays.equals(new double[]{1, 2, 3, 4}, newArray));
          assertEquals(Double.TYPE, newArray.getClass().getComponentType());        
      }
      
      public void testAddObjectArrayFloat() {
          float[] newArray;
          newArray = ArrayUtils.add((float[])null, 0);
          assertTrue(Arrays.equals(new float[]{0}, newArray));
          assertEquals(Float.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((float[])null, 1);
          assertTrue(Arrays.equals(new float[]{1}, newArray));
          assertEquals(Float.TYPE, newArray.getClass().getComponentType());
          float[] array1 = new float[]{1, 2, 3};
          newArray = ArrayUtils.add(array1, 0);
          assertTrue(Arrays.equals(new float[]{1, 2, 3, 0}, newArray));
          assertEquals(Float.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(array1, 4);
          assertTrue(Arrays.equals(new float[]{1, 2, 3, 4}, newArray));
          assertEquals(Float.TYPE, newArray.getClass().getComponentType());        
      }
      
      public void testAddObjectArrayInt() {
          int[] newArray;
          newArray = ArrayUtils.add((int[])null, 0);
          assertTrue(Arrays.equals(new int[]{0}, newArray));
          assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((int[])null, 1);
          assertTrue(Arrays.equals(new int[]{1}, newArray));
          assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
          int[] array1 = new int[]{1, 2, 3};
          newArray = ArrayUtils.add(array1, 0);
          assertTrue(Arrays.equals(new int[]{1, 2, 3, 0}, newArray));
          assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(array1, 4);
          assertTrue(Arrays.equals(new int[]{1, 2, 3, 4}, newArray));
          assertEquals(Integer.TYPE, newArray.getClass().getComponentType());        
      }
      
      public void testAddObjectArrayLong() {
          long[] newArray;
          newArray = ArrayUtils.add((long[])null, 0);
          assertTrue(Arrays.equals(new long[]{0}, newArray));
          assertEquals(Long.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((long[])null, 1);
          assertTrue(Arrays.equals(new long[]{1}, newArray));
          assertEquals(Long.TYPE, newArray.getClass().getComponentType());
          long[] array1 = new long[]{1, 2, 3};
          newArray = ArrayUtils.add(array1, 0);
          assertTrue(Arrays.equals(new long[]{1, 2, 3, 0}, newArray));
          assertEquals(Long.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(array1, 4);
          assertTrue(Arrays.equals(new long[]{1, 2, 3, 4}, newArray));
          assertEquals(Long.TYPE, newArray.getClass().getComponentType());        
      }
      
      public void testAddObjectArrayShort() {
          short[] newArray;
          newArray = ArrayUtils.add((short[])null, (short)0);
          assertTrue(Arrays.equals(new short[]{0}, newArray));
          assertEquals(Short.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((short[])null, (short)1);
          assertTrue(Arrays.equals(new short[]{1}, newArray));
          assertEquals(Short.TYPE, newArray.getClass().getComponentType());
          short[] array1 = new short[]{1, 2, 3};
          newArray = ArrayUtils.add(array1, (short)0);
          assertTrue(Arrays.equals(new short[]{1, 2, 3, 0}, newArray));
          assertEquals(Short.TYPE, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(array1, (short)4);
          assertTrue(Arrays.equals(new short[]{1, 2, 3, 4}, newArray));
          assertEquals(Short.TYPE, newArray.getClass().getComponentType());        
      }
      
      public void testAddObjectArrayObject() {
          Object[] newArray;
          newArray = ArrayUtils.add((Object[])null, null);
          assertTrue(Arrays.equals((new Object[]{null}), newArray));
          assertEquals(Object.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((Object[])null, "a");
          assertTrue(Arrays.equals((new String[]{"a"}), newArray));
          assertTrue(Arrays.equals((new Object[]{"a"}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          String[] stringArray1 = new String[]{"a", "b", "c"};
          newArray = ArrayUtils.add(stringArray1, null);
          assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(stringArray1, "d");
          assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
      }
      
      public void testAddObjectArrayToObjectArray() {
          assertNull(ArrayUtils.addAll(null, null));
          Object[] newArray;
          String[] stringArray1 = new String[]{"a", "b", "c"};
          String[] stringArray2 = new String[]{"1", "2", "3"};
          newArray = ArrayUtils.addAll(stringArray1, null);
          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);
          assertTrue(Arrays.equals(stringArray2, newArray));
          assertTrue(Arrays.equals((new String[]{"1", "2", "3"}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.addAll(stringArray1, stringArray2);
          assertTrue(Arrays.equals((new String[]{"a", "b", "c", "1", "2", "3"}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, null);
          assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray));
          assertTrue(Arrays.equals((new String[]{}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.addAll(null, ArrayUtils.EMPTY_STRING_ARRAY);
          assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray));
          assertTrue(Arrays.equals((new String[]{}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.EMPTY_STRING_ARRAY);
          assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray));
          assertTrue(Arrays.equals((new String[]{}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          String[] stringArrayNull = new String []{null};
          newArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull);        
          assertTrue(Arrays.equals((new String[]{null, null}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
      }    
      
      public void testAddObjectAtIndex() {
          Object[] newArray;
          newArray = ArrayUtils.add((Object[])null, 0, null);
          assertTrue(Arrays.equals((new Object[]{null}), newArray));
          assertEquals(Object.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add((Object[])null, 0, "a");
          assertTrue(Arrays.equals((new String[]{"a"}), newArray));
          assertTrue(Arrays.equals((new Object[]{"a"}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          String[] stringArray1 = new String[]{"a", "b", "c"};
          newArray = ArrayUtils.add(stringArray1, 0, null);
          assertTrue(Arrays.equals((new String[]{null, "a", "b", "c"}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(stringArray1, 1, null);
          assertTrue(Arrays.equals((new String[]{"a", null, "b", "c"}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(stringArray1, 3, null);
          assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());
          newArray = ArrayUtils.add(stringArray1, 3, "d");
          assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray));
          assertEquals(String.class, newArray.getClass().getComponentType());        
          assertEquals(String.class, newArray.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]);
      }
      
  }
  
  
  
  1.40      +253 -16   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.39
  retrieving revision 1.40
  diff -u -r1.39 -r1.40
  --- ArrayUtils.java	31 Jan 2004 20:12:16 -0000	1.39
  +++ ArrayUtils.java	3 Feb 2004 22:14:24 -0000	1.40
  @@ -2787,11 +2787,11 @@
       }
   
       /**
  -     * <p>Adds the element to the end of the array.</p>
  +     * <p>Copies the given array and adds the given element at the end of the new 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>
  +     * 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>
  @@ -2806,25 +2806,262 @@
        * 
        * @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
  +     * @return A new array containing the existing elements plus the new element
        * @since 2.1
        */
  -    public static Object[] add(Object[] array, Object element) {
  -        Object joinedArray;
  -        int elementPos;
  +    public static Object[] add(final Object[] array, final Object element) {
  +        Object newArray = copyArrayGrow1(array, element != null ? element.getClass() : Object.class);
  +        Array.set(newArray, lastIndex(newArray), element);
  +        return (Object[]) newArray;
  +    }
  +    
  +    /**
  +     * <p>Copies the given array and adds the given element at the end of the new 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, true)          = [true]
  +     * ArrayUtils.add([true], false)       = [true, false]
  +     * ArrayUtils.add([true, false], true) = [true, false, true]
  +     * </pre>
  +     * 
  +     * @param array  the array to copy and add the element to, may be <code>null</code>
  +     * @param element  the object to add at the last index of the new array
  +     * @return A new array containing the existing elements plus the new element
  +     * @since 2.1
  +     */
  +    public static boolean[] add(final boolean[] array, final boolean element) {
  +        boolean[] newArray = (boolean[])copyArrayGrow1(array, Boolean.TYPE);
  +        newArray[lastIndex(newArray)] = element;
  +        return newArray;
  +    }
  +    
  +    /**
  +     * <p>Copies the given array and adds the given element at the end of the new 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, 0)   = [0]
  +     * ArrayUtils.add([1], 0)    = [1, 0]
  +     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  +     * </pre>
  +     * 
  +     * @param array  the array to copy and add the element to, may be <code>null</code>
  +     * @param element  the object to add at the last index of the new array
  +     * @return A new array containing the existing elements plus the new element
  +     * @since 2.1
  +     */
  +    public static byte[] add(final byte[] array, final byte element) {
  +        byte[] newArray = (byte[])copyArrayGrow1(array, Byte.TYPE);
  +        newArray[lastIndex(newArray)] = element;
  +        return newArray;
  +    }
  +    
  +    /**
  +     * <p>Copies the given array and adds the given element at the end of the new 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, '0')       = ['0']
  +     * ArrayUtils.add(['1'], '0')      = ['1', '0']
  +     * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
  +     * </pre>
  +     * 
  +     * @param array  the array to copy and add the element to, may be <code>null</code>
  +     * @param element  the object to add at the last index of the new array
  +     * @return A new array containing the existing elements plus the new element
  +     * @since 2.1
  +     */
  +    public static char[] add(final char[] array, final char element) {
  +        char[] newArray = (char[])copyArrayGrow1(array, Character.TYPE);
  +        newArray[lastIndex(newArray)] = element;
  +        return newArray;
  +    }
  +    
  +    /**
  +     * <p>Copies the given array and adds the given element at the end of the new 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, 0)   = [0]
  +     * ArrayUtils.add([1], 0)    = [1, 0]
  +     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  +     * </pre>
  +     * 
  +     * @param array  the array to copy and add the element to, may be <code>null</code>
  +     * @param element  the object to add at the last index of the new array
  +     * @return A new array containing the existing elements plus the new element
  +     * @since 2.1
  +     */
  +    public static double[] add(final double[] array, final double element) {
  +        double[] newArray = (double[])copyArrayGrow1(array, Double.TYPE);
  +        newArray[lastIndex(newArray)] = element;
  +        return newArray;
  +    }
  +    
  +    /**
  +     * <p>Copies the given array and adds the given element at the end of the new 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, 0)   = [0]
  +     * ArrayUtils.add([1], 0)    = [1, 0]
  +     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  +     * </pre>
  +     * 
  +     * @param array  the array to copy and add the element to, may be <code>null</code>
  +     * @param element  the object to add at the last index of the new array
  +     * @return A new array containing the existing elements plus the new element
  +     * @since 2.1
  +     */
  +    public static float[] add(final float[] array, final float element) {
  +        float[] newArray = (float[])copyArrayGrow1(array, Float.TYPE);
  +        newArray[lastIndex(newArray)] = element;
  +        return newArray;
  +    }
  +    
  +    /**
  +     * <p>Copies the given array and adds the given element at the end of the new 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, 0)   = [0]
  +     * ArrayUtils.add([1], 0)    = [1, 0]
  +     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  +     * </pre>
  +     * 
  +     * @param array  the array to copy and add the element to, may be <code>null</code>
  +     * @param element  the object to add at the last index of the new array
  +     * @return A new array containing the existing elements plus the new element
  +     * @since 2.1
  +     */
  +    public static int[] add(final int[] array, final int element) {
  +        int[] newArray = (int[])copyArrayGrow1(array, Integer.TYPE);
  +        newArray[lastIndex(newArray)] = element;
  +        return newArray;
  +    }
  +    
  +    /**
  +     * <p>Copies the given array and adds the given element at the end of the new 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, 0)   = [0]
  +     * ArrayUtils.add([1], 0)    = [1, 0]
  +     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  +     * </pre>
  +     * 
  +     * @param array  the array to copy and add the element to, may be <code>null</code>
  +     * @param element  the object to add at the last index of the new array
  +     * @return A new array containing the existing elements plus the new element
  +     * @since 2.1
  +     */
  +    public static long[] add(final long[] array, final long element) {
  +        long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE);
  +        newArray[lastIndex(newArray)] = element;
  +        return newArray;
  +    }
  +    
  +    /**
  +     * <p>Copies the given array and adds the given element at the end of the new 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, 0)   = [0]
  +     * ArrayUtils.add([1], 0)    = [1, 0]
  +     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  +     * </pre>
  +     * 
  +     * @param array  the array to copy and add the element to, may be <code>null</code>
  +     * @param element  the object to add at the last index of the new array
  +     * @return A new array containing the existing elements plus the new element
  +     * @since 2.1
  +     */
  +    public static short[] add(final short[] array, final short element) {
  +        short[] newArray = (short[])copyArrayGrow1(array, Short.TYPE);
  +        newArray[lastIndex(newArray)] = element;
  +        return newArray;
  +    }
  +    
  +    /**
  +     * Returns a copy of the given array of size 1 greater than the argument. 
  +     * The last value of the array is left to the default value.
  +     * 
  +     * @param array The array to copy, must not be <code>null</code>.
  +     * @param newArrayComponentType If <code>array</code> is <code>null</code>, create a 
  +     * size 1 array of this type.
  +     * @return A new copy of the array of size 1 greater than the input.
  +     */    
  +    private static Object copyArrayGrow1(final Object array, Class newArrayComponentType) {
           if (array != null) {
  -            joinedArray = Array.newInstance(array.getClass().getComponentType(), array.length + 1);
  -            System.arraycopy(array, 0, joinedArray, 0, array.length);
  -            elementPos = array.length;
  +            int arrayLength = Array.getLength(array);
  +            Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);
  +            System.arraycopy(array, 0, newArray, 0, arrayLength);
  +            return newArray;
           } else {
  -            // null input array, use the element type
  -            joinedArray = Array.newInstance(element != null ? element.getClass() : Object.class, 1);
  -            elementPos = 0;
  +            return Array.newInstance(newArrayComponentType, 1);
           }
  -        Array.set(joinedArray, elementPos, element);
  -        return (Object[]) joinedArray;
       }
  -
  +    
  +    /**
  +     * Returns the last index of the given array. This value is one less than the size since
  +     * arrays indices are 0-based.
  +     *  
  +     * @param array The array to return the last index for, must not be <code>null</code>.
  +     * @return The last index
  +     */
  +    public static int lastIndex(final Object array) {
  +        return Array.getLength(array) - 1;
  +    }
  +    
       /**
        * <p>Inserts the specified element at the specified position in the array. 
        * Shifts the element currently at that position (if any) and any subsequent
  
  
  

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