Fix bug where add() didn't use array type
Remove unecessary calls to lastIndex() for performance git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137970 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
657eac9347
commit
f4f34dc60a
|
@ -44,7 +44,7 @@ import org.apache.commons.lang.builder.ToStringStyle;
|
||||||
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
|
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
|
||||||
* @author Maarten Coene
|
* @author Maarten Coene
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
* @version $Id: ArrayUtils.java,v 1.47 2004/10/08 00:11:00 scolebourne Exp $
|
* @version $Id: ArrayUtils.java,v 1.48 2004/10/09 11:55:51 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class ArrayUtils {
|
public class ArrayUtils {
|
||||||
|
|
||||||
|
@ -2943,9 +2943,10 @@ public class ArrayUtils {
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static Object[] add(Object[] array, Object element) {
|
public static Object[] add(Object[] array, Object element) {
|
||||||
Object newArray = copyArrayGrow1(array, element != null ? element.getClass() : Object.class);
|
Class type = (array != null ? array.getClass() : (element != null ? element.getClass() : Object.class));
|
||||||
Array.set(newArray, lastIndex(newArray), element);
|
Object[] newArray = (Object[]) copyArrayGrow1(array, type);
|
||||||
return (Object[]) newArray;
|
newArray[newArray.length - 1] = element;
|
||||||
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2971,7 +2972,7 @@ public class ArrayUtils {
|
||||||
*/
|
*/
|
||||||
public static boolean[] add(boolean[] array, boolean element) {
|
public static boolean[] add(boolean[] array, boolean element) {
|
||||||
boolean[] newArray = (boolean[])copyArrayGrow1(array, Boolean.TYPE);
|
boolean[] newArray = (boolean[])copyArrayGrow1(array, Boolean.TYPE);
|
||||||
newArray[lastIndex(newArray)] = element;
|
newArray[newArray.length - 1] = element;
|
||||||
return newArray;
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2998,7 +2999,7 @@ public class ArrayUtils {
|
||||||
*/
|
*/
|
||||||
public static byte[] add(byte[] array, byte element) {
|
public static byte[] add(byte[] array, byte element) {
|
||||||
byte[] newArray = (byte[])copyArrayGrow1(array, Byte.TYPE);
|
byte[] newArray = (byte[])copyArrayGrow1(array, Byte.TYPE);
|
||||||
newArray[lastIndex(newArray)] = element;
|
newArray[newArray.length - 1] = element;
|
||||||
return newArray;
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3025,7 +3026,7 @@ public class ArrayUtils {
|
||||||
*/
|
*/
|
||||||
public static char[] add(char[] array, char element) {
|
public static char[] add(char[] array, char element) {
|
||||||
char[] newArray = (char[])copyArrayGrow1(array, Character.TYPE);
|
char[] newArray = (char[])copyArrayGrow1(array, Character.TYPE);
|
||||||
newArray[lastIndex(newArray)] = element;
|
newArray[newArray.length - 1] = element;
|
||||||
return newArray;
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3052,7 +3053,7 @@ public class ArrayUtils {
|
||||||
*/
|
*/
|
||||||
public static double[] add(double[] array, double element) {
|
public static double[] add(double[] array, double element) {
|
||||||
double[] newArray = (double[])copyArrayGrow1(array, Double.TYPE);
|
double[] newArray = (double[])copyArrayGrow1(array, Double.TYPE);
|
||||||
newArray[lastIndex(newArray)] = element;
|
newArray[newArray.length - 1] = element;
|
||||||
return newArray;
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3079,7 +3080,7 @@ public class ArrayUtils {
|
||||||
*/
|
*/
|
||||||
public static float[] add(float[] array, float element) {
|
public static float[] add(float[] array, float element) {
|
||||||
float[] newArray = (float[])copyArrayGrow1(array, Float.TYPE);
|
float[] newArray = (float[])copyArrayGrow1(array, Float.TYPE);
|
||||||
newArray[lastIndex(newArray)] = element;
|
newArray[newArray.length - 1] = element;
|
||||||
return newArray;
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3106,7 +3107,7 @@ public class ArrayUtils {
|
||||||
*/
|
*/
|
||||||
public static int[] add(int[] array, int element) {
|
public static int[] add(int[] array, int element) {
|
||||||
int[] newArray = (int[])copyArrayGrow1(array, Integer.TYPE);
|
int[] newArray = (int[])copyArrayGrow1(array, Integer.TYPE);
|
||||||
newArray[lastIndex(newArray)] = element;
|
newArray[newArray.length - 1] = element;
|
||||||
return newArray;
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3133,7 +3134,7 @@ public class ArrayUtils {
|
||||||
*/
|
*/
|
||||||
public static long[] add(long[] array, long element) {
|
public static long[] add(long[] array, long element) {
|
||||||
long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE);
|
long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE);
|
||||||
newArray[lastIndex(newArray)] = element;
|
newArray[newArray.length - 1] = element;
|
||||||
return newArray;
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3160,7 +3161,7 @@ public class ArrayUtils {
|
||||||
*/
|
*/
|
||||||
public static short[] add(short[] array, short element) {
|
public static short[] add(short[] array, short element) {
|
||||||
short[] newArray = (short[])copyArrayGrow1(array, Short.TYPE);
|
short[] newArray = (short[])copyArrayGrow1(array, Short.TYPE);
|
||||||
newArray[lastIndex(newArray)] = element;
|
newArray[newArray.length - 1] = element;
|
||||||
return newArray;
|
return newArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ import junit.textui.TestRunner;
|
||||||
* Tests ArrayUtils add methods.
|
* Tests ArrayUtils add methods.
|
||||||
*
|
*
|
||||||
* @author Gary D. Gregory
|
* @author Gary D. Gregory
|
||||||
* @version $Id: ArrayUtilsAddTest.java,v 1.3 2004/03/16 01:40:57 ggregory Exp $
|
* @version $Id: ArrayUtilsAddTest.java,v 1.4 2004/10/09 11:55:51 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class ArrayUtilsAddTest extends TestCase {
|
public class ArrayUtilsAddTest extends TestCase {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -178,17 +178,35 @@ public class ArrayUtilsAddTest extends TestCase {
|
||||||
newArray = ArrayUtils.add((Object[])null, null);
|
newArray = ArrayUtils.add((Object[])null, null);
|
||||||
assertTrue(Arrays.equals((new Object[]{null}), newArray));
|
assertTrue(Arrays.equals((new Object[]{null}), newArray));
|
||||||
assertEquals(Object.class, newArray.getClass().getComponentType());
|
assertEquals(Object.class, newArray.getClass().getComponentType());
|
||||||
|
|
||||||
newArray = ArrayUtils.add((Object[])null, "a");
|
newArray = ArrayUtils.add((Object[])null, "a");
|
||||||
assertTrue(Arrays.equals((new String[]{"a"}), newArray));
|
assertTrue(Arrays.equals((new String[]{"a"}), newArray));
|
||||||
assertTrue(Arrays.equals((new Object[]{"a"}), newArray));
|
assertTrue(Arrays.equals((new Object[]{"a"}), newArray));
|
||||||
assertEquals(String.class, newArray.getClass().getComponentType());
|
assertEquals(String.class, newArray.getClass().getComponentType());
|
||||||
|
|
||||||
String[] stringArray1 = new String[]{"a", "b", "c"};
|
String[] stringArray1 = new String[]{"a", "b", "c"};
|
||||||
newArray = ArrayUtils.add(stringArray1, null);
|
newArray = ArrayUtils.add(stringArray1, null);
|
||||||
assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray));
|
assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray));
|
||||||
assertEquals(String.class, newArray.getClass().getComponentType());
|
assertEquals(String.class, newArray.getClass().getComponentType());
|
||||||
|
|
||||||
newArray = ArrayUtils.add(stringArray1, "d");
|
newArray = ArrayUtils.add(stringArray1, "d");
|
||||||
assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray));
|
assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray));
|
||||||
assertEquals(String.class, newArray.getClass().getComponentType());
|
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() {
|
public void testAddObjectArrayToObjectArray() {
|
||||||
|
|
Loading…
Reference in New Issue