commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ArrayUtilsAddTest.java
Date Sat, 09 Oct 2004 11:55:51 GMT
scolebourne    2004/10/09 04:55:51

  Modified:    lang/src/java/org/apache/commons/lang ArrayUtils.java
               lang/src/test/org/apache/commons/lang ArrayUtilsAddTest.java
  Log:
  Fix bug where add() didn't use array type
  Remove unecessary calls to lastIndex() for performance
  
  Revision  Changes    Path
  1.48      +13 -12    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.47
  retrieving revision 1.48
  diff -u -r1.47 -r1.48
  --- ArrayUtils.java	8 Oct 2004 00:11:00 -0000	1.47
  +++ ArrayUtils.java	9 Oct 2004 11:55:51 -0000	1.48
  @@ -2943,9 +2943,10 @@
        * @since 2.1
        */
       public static Object[] add(Object[] array, Object element) {
  -        Object newArray = copyArrayGrow1(array, element != null ? element.getClass() :
Object.class);
  -        Array.set(newArray, lastIndex(newArray), element);
  -        return (Object[]) newArray;
  +        Class type = (array != null ? array.getClass() : (element != null ? element.getClass()
: Object.class));
  +        Object[] newArray = (Object[]) copyArrayGrow1(array, type);
  +        newArray[newArray.length - 1] = element;
  +        return newArray;
       }
       
       /**
  @@ -2971,7 +2972,7 @@
        */
       public static boolean[] add(boolean[] array, boolean element) {
           boolean[] newArray = (boolean[])copyArrayGrow1(array, Boolean.TYPE);
  -        newArray[lastIndex(newArray)] = element;
  +        newArray[newArray.length - 1] = element;
           return newArray;
       }
       
  @@ -2998,7 +2999,7 @@
        */
       public static byte[] add(byte[] array, byte element) {
           byte[] newArray = (byte[])copyArrayGrow1(array, Byte.TYPE);
  -        newArray[lastIndex(newArray)] = element;
  +        newArray[newArray.length - 1] = element;
           return newArray;
       }
       
  @@ -3025,7 +3026,7 @@
        */
       public static char[] add(char[] array, char element) {
           char[] newArray = (char[])copyArrayGrow1(array, Character.TYPE);
  -        newArray[lastIndex(newArray)] = element;
  +        newArray[newArray.length - 1] = element;
           return newArray;
       }
       
  @@ -3052,7 +3053,7 @@
        */
       public static double[] add(double[] array, double element) {
           double[] newArray = (double[])copyArrayGrow1(array, Double.TYPE);
  -        newArray[lastIndex(newArray)] = element;
  +        newArray[newArray.length - 1] = element;
           return newArray;
       }
       
  @@ -3079,7 +3080,7 @@
        */
       public static float[] add(float[] array, float element) {
           float[] newArray = (float[])copyArrayGrow1(array, Float.TYPE);
  -        newArray[lastIndex(newArray)] = element;
  +        newArray[newArray.length - 1] = element;
           return newArray;
       }
       
  @@ -3106,7 +3107,7 @@
        */
       public static int[] add(int[] array, int element) {
           int[] newArray = (int[])copyArrayGrow1(array, Integer.TYPE);
  -        newArray[lastIndex(newArray)] = element;
  +        newArray[newArray.length - 1] = element;
           return newArray;
       }
       
  @@ -3133,7 +3134,7 @@
        */
       public static long[] add(long[] array, long element) {
           long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE);
  -        newArray[lastIndex(newArray)] = element;
  +        newArray[newArray.length - 1] = element;
           return newArray;
       }
       
  @@ -3160,7 +3161,7 @@
        */
       public static short[] add(short[] array, short element) {
           short[] newArray = (short[])copyArrayGrow1(array, Short.TYPE);
  -        newArray[lastIndex(newArray)] = element;
  +        newArray[newArray.length - 1] = element;
           return newArray;
       }
       
  
  
  
  1.4       +19 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java
  
  Index: ArrayUtilsAddTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ArrayUtilsAddTest.java	16 Mar 2004 01:40:57 -0000	1.3
  +++ ArrayUtilsAddTest.java	9 Oct 2004 11:55:51 -0000	1.4
  @@ -178,17 +178,35 @@
           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());
  +        
  +        Number[] numberArray1 = new Number[]{new Integer(1), new Double(2)};
  +        newArray = ArrayUtils.add(numberArray1, new Float(3));
  +        assertTrue(Arrays.equals((new Number[]{new Integer(1), new Double(2), new Float(3)}),
newArray));
  +        assertEquals(Number.class, newArray.getClass().getComponentType());
  +        
  +        numberArray1 = null;
  +        newArray = ArrayUtils.add(numberArray1, new Float(3));
  +        assertTrue(Arrays.equals((new Float[]{new Float(3)}), newArray));
  +        assertEquals(Float.class, newArray.getClass().getComponentType());
  +        
  +        numberArray1 = null;
  +        newArray = ArrayUtils.add(numberArray1, null);
  +        assertTrue(Arrays.equals((new Object[]{null}), newArray));
  +        assertEquals(Object.class, newArray.getClass().getComponentType());
       }
       
       public void testAddObjectArrayToObjectArray() {
  
  
  

---------------------------------------------------------------------
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