diff --git a/src/java/org/apache/commons/lang/ArrayUtils.java b/src/java/org/apache/commons/lang/ArrayUtils.java index c5ebeea2b..30f7f18a1 100644 --- a/src/java/org/apache/commons/lang/ArrayUtils.java +++ b/src/java/org/apache/commons/lang/ArrayUtils.java @@ -70,7 +70,7 @@ import org.apache.commons.lang.builder.ToStringStyle; * @author Nikolay Metchev * @author Matthew Hawthorne * @since 2.0 - * @version $Id: ArrayUtils.java,v 1.15 2003/06/25 23:33:47 scolebourne Exp $ + * @version $Id: ArrayUtils.java,v 1.16 2003/06/28 18:01:18 scolebourne Exp $ */ public class ArrayUtils { @@ -906,7 +906,10 @@ public class ArrayUtils { return (indexOf(array, objectToFind) != -1); } - // Primitive/Object converters + // Primitive/Object array converters + // ---------------------------------------------------------------------- + + // Boolean array converters // ---------------------------------------------------------------------- /** *
Converts an array of object Booleans to primitives.
@@ -974,4 +977,412 @@ public class ArrayUtils { return result; } + // Byte array converters + // ---------------------------------------------------------------------- + /** + *Converts an array of object Bytes to primitives.
+ * + *This method returns null
if null
input.
Byte
array, may be null
+ * @return a byte
array
+ * @throws NullPointerException if array content is null
+ */
+ public static byte[] toPrimitive(final Byte[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_BYTE_ARRAY;
+ }
+ final byte[] result = new byte[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = array[i].byteValue();
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of object Bytes to primitives handling null.
+ * + *This method returns null
if null
input.
Byte
array, may be null
+ * @param valueForNull the value to insert if null
found
+ * @return a byte
array
+ */
+ public static byte[] toPrimitive(final Byte[] array, final byte valueForNull) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_BYTE_ARRAY;
+ }
+ final byte[] result = new byte[array.length];
+ for (int i = 0; i < array.length; i++) {
+ Byte b = array[i];
+ result[i] = (b == null ? valueForNull : b.byteValue());
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of primitive bytes to objects.
+ * + *This method returns null
if null
input.
byte
array
+ * @return a Byte
array
+ */
+ public static Byte[] toObject(final byte[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_BYTE_OBJECT_ARRAY;
+ }
+ final Byte[] result = new Byte[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = new Byte(array[i]);
+ }
+ return result;
+ }
+
+ // Short array converters
+ // ----------------------------------------------------------------------
+ /**
+ * Converts an array of object Shorts to primitives.
+ * + *This method returns null
if null
input.
Short
array, may be null
+ * @return a byte
array
+ * @throws NullPointerException if array content is null
+ */
+ public static short[] toPrimitive(final Short[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_SHORT_ARRAY;
+ }
+ final short[] result = new short[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = array[i].shortValue();
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of object Short to primitives handling null.
+ * + *This method returns null
if null
input.
Short
array, may be null
+ * @param valueForNull the value to insert if null
found
+ * @return a byte
array
+ */
+ public static short[] toPrimitive(final Short[] array, final short valueForNull) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_SHORT_ARRAY;
+ }
+ final short[] result = new short[array.length];
+ for (int i = 0; i < array.length; i++) {
+ Short b = array[i];
+ result[i] = (b == null ? valueForNull : b.shortValue());
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of primitive shorts to objects.
+ * + *This method returns null
if null
input.
short
array
+ * @return a Short
array
+ */
+ public static Short[] toObject(final short[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_SHORT_OBJECT_ARRAY;
+ }
+ final Short[] result = new Short[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = new Short(array[i]);
+ }
+ return result;
+ }
+
+ // Int array converters
+ // ----------------------------------------------------------------------
+ /**
+ * Converts an array of object Integers to primitives.
+ * + *This method returns null
if null
input.
Integer
array, may be null
+ * @return an int
array
+ * @throws NullPointerException if array content is null
+ */
+ public static int[] toPrimitive(final Integer[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_INT_ARRAY;
+ }
+ final int[] result = new int[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = array[i].intValue();
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of object Integer to primitives handling null.
+ * + *This method returns null
if null
input.
Integer
array, may be null
+ * @param valueForNull the value to insert if null
found
+ * @return an int
array
+ */
+ public static int[] toPrimitive(final Integer[] array, final int valueForNull) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_INT_ARRAY;
+ }
+ final int[] result = new int[array.length];
+ for (int i = 0; i < array.length; i++) {
+ Integer b = array[i];
+ result[i] = (b == null ? valueForNull : b.intValue());
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of primitive ints to objects.
+ * + *This method returns null
if null
input.
int
array
+ * @return an Integer
array
+ */
+ public static Integer[] toObject(final int[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_INTEGER_OBJECT_ARRAY;
+ }
+ final Integer[] result = new Integer[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = new Integer(array[i]);
+ }
+ return result;
+ }
+
+ // Long array converters
+ // ----------------------------------------------------------------------
+ /**
+ * Converts an array of object Longs to primitives.
+ * + *This method returns null
if null
input.
Long
array, may be null
+ * @return a long
array
+ * @throws NullPointerException if array content is null
+ */
+ public static long[] toPrimitive(final Long[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_LONG_ARRAY;
+ }
+ final long[] result = new long[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = array[i].longValue();
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of object Long to primitives handling null.
+ * + *This method returns null
if null
input.
Long
array, may be null
+ * @param valueForNull the value to insert if null
found
+ * @return a long
array
+ */
+ public static long[] toPrimitive(final Long[] array, final long valueForNull) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_LONG_ARRAY;
+ }
+ final long[] result = new long[array.length];
+ for (int i = 0; i < array.length; i++) {
+ Long b = array[i];
+ result[i] = (b == null ? valueForNull : b.longValue());
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of primitive longs to objects.
+ * + *This method returns null
if null
input.
long
array
+ * @return a Long
array
+ */
+ public static Long[] toObject(final long[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_LONG_OBJECT_ARRAY;
+ }
+ final Long[] result = new Long[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = new Long(array[i]);
+ }
+ return result;
+ }
+
+ // Float array converters
+ // ----------------------------------------------------------------------
+ /**
+ * Converts an array of object Floats to primitives.
+ * + *This method returns null
if null
input.
Float
array, may be null
+ * @return a float
array
+ * @throws NullPointerException if array content is null
+ */
+ public static float[] toPrimitive(final Float[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_FLOAT_ARRAY;
+ }
+ final float[] result = new float[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = array[i].floatValue();
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of object Floats to primitives handling null.
+ * + *This method returns null
if null
input.
Float
array, may be null
+ * @param valueForNull the value to insert if null
found
+ * @return a float
array
+ */
+ public static float[] toPrimitive(final Float[] array, final float valueForNull) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_FLOAT_ARRAY;
+ }
+ final float[] result = new float[array.length];
+ for (int i = 0; i < array.length; i++) {
+ Float b = array[i];
+ result[i] = (b == null ? valueForNull : b.floatValue());
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of primitive floats to objects.
+ * + *This method returns null
if null
input.
float
array
+ * @return a Float
array
+ */
+ public static Float[] toObject(final float[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_FLOAT_OBJECT_ARRAY;
+ }
+ final Float[] result = new Float[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = new Float(array[i]);
+ }
+ return result;
+ }
+
+ // Double array converters
+ // ----------------------------------------------------------------------
+ /**
+ * Converts an array of object Doubles to primitives.
+ * + *This method returns null
if null
input.
Double
array, may be null
+ * @return a double
array
+ * @throws NullPointerException if array content is null
+ */
+ public static double[] toPrimitive(final Double[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_DOUBLE_ARRAY;
+ }
+ final double[] result = new double[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = array[i].doubleValue();
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of object Doubles to primitives handling null.
+ * + *This method returns null
if null
input.
Double
array, may be null
+ * @param valueForNull the value to insert if null
found
+ * @return a double
array
+ */
+ public static double[] toPrimitive(final Double[] array, final double valueForNull) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_DOUBLE_ARRAY;
+ }
+ final double[] result = new double[array.length];
+ for (int i = 0; i < array.length; i++) {
+ Double b = array[i];
+ result[i] = (b == null ? valueForNull : b.doubleValue());
+ }
+ return result;
+ }
+
+ /**
+ * Converts an array of primitive doubles to objects.
+ * + *This method returns null
if null
input.
double
array
+ * @return a Double
array
+ */
+ public static Double[] toObject(final double[] array) {
+ if (array == null) {
+ return null;
+ } else if (array.length == 0) {
+ return EMPTY_DOUBLE_OBJECT_ARRAY;
+ }
+ final Double[] result = new Double[array.length];
+ for (int i = 0; i < array.length; i++) {
+ result[i] = new Double(array[i]);
+ }
+ return result;
+ }
+
}
diff --git a/src/test/org/apache/commons/lang/ArrayUtilsTest.java b/src/test/org/apache/commons/lang/ArrayUtilsTest.java
index a01b2576c..4a044f8d5 100644
--- a/src/test/org/apache/commons/lang/ArrayUtilsTest.java
+++ b/src/test/org/apache/commons/lang/ArrayUtilsTest.java
@@ -68,7 +68,7 @@ import junit.textui.TestRunner;
* @author Moritz Petersen
* @author Nikolay Metchev
* @author Matthew Hawthorne
- * @version $Id: ArrayUtilsTest.java,v 1.7 2003/06/25 23:33:47 scolebourne Exp $
+ * @version $Id: ArrayUtilsTest.java,v 1.8 2003/06/28 18:01:19 scolebourne Exp $
*/
public class ArrayUtilsTest extends TestCase {
@@ -142,10 +142,7 @@ public class ArrayUtilsTest extends TestCase {
assertEquals("bar", map.get("foo"));
assertEquals("world", map.get("hello"));
- try {
- ArrayUtils.toMap(null);
- fail("exception expected");
- } catch (IllegalArgumentException ex) {}
+ assertEquals(null, ArrayUtils.toMap(null));
try {
ArrayUtils.toMap(new String[][] {{"foo", "bar"}, {"short"}});
fail("exception expected");
@@ -711,7 +708,8 @@ public class ArrayUtilsTest extends TestCase {
// testToPrimitive/Object for boolean
// -----------------------------------------------------------------------
public void testToPrimitive_boolean() {
- assertEquals(null, ArrayUtils.toPrimitive(null));
+ final Boolean[] b = null;
+ assertEquals(null, ArrayUtils.toPrimitive(b));
assertSame(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.toPrimitive(new Boolean[0]));
assertTrue(Arrays.equals(
new boolean[] {true, false, true},
@@ -742,7 +740,8 @@ public class ArrayUtilsTest extends TestCase {
}
public void testToObject_boolean() {
- assertEquals(null, ArrayUtils.toObject(null));
+ final boolean[] b = null;
+ assertEquals(null, ArrayUtils.toObject(b));
assertSame(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, ArrayUtils.toObject(new boolean[0]));
assertTrue(Arrays.equals(
new Boolean[] {Boolean.TRUE, Boolean.FALSE, Boolean.TRUE},
@@ -750,4 +749,341 @@ public class ArrayUtilsTest extends TestCase {
);
}
+ // testToPrimitive/Object for byte
+ // -----------------------------------------------------------------------
+ public void testToPrimitive_byte() {
+ final Byte[] b = null;
+ assertEquals(null, ArrayUtils.toPrimitive(b));
+
+ assertSame(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.toPrimitive(new Byte[0]));
+
+ assertTrue(Arrays.equals(
+ new byte[] {Byte.MIN_VALUE, Byte.MAX_VALUE, (byte)9999999},
+ ArrayUtils.toPrimitive(new Byte[] {new Byte(Byte.MIN_VALUE),
+ new Byte(Byte.MAX_VALUE), new Byte((byte)9999999)}))
+ );
+
+ try {
+ ArrayUtils.toPrimitive(new Byte[] {new Byte(Byte.MIN_VALUE), null});
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ public void testToPrimitive_byte_byte() {
+ final Byte[] b = null;
+ assertEquals(null, ArrayUtils.toPrimitive(b, Byte.MIN_VALUE));
+
+ assertSame(ArrayUtils.EMPTY_BYTE_ARRAY,
+ ArrayUtils.toPrimitive(new Byte[0], (byte)1));
+
+ assertTrue(Arrays.equals(
+ new byte[] {Byte.MIN_VALUE, Byte.MAX_VALUE, (byte)9999999},
+ ArrayUtils.toPrimitive(new Byte[] {new Byte(Byte.MIN_VALUE),
+ new Byte(Byte.MAX_VALUE), new Byte((byte)9999999)},
+ Byte.MIN_VALUE))
+ );
+
+ assertTrue(Arrays.equals(
+ new byte[] {Byte.MIN_VALUE, Byte.MAX_VALUE, (byte)9999999},
+ ArrayUtils.toPrimitive(new Byte[] {new Byte(Byte.MIN_VALUE), null,
+ new Byte((byte)9999999)}, Byte.MAX_VALUE))
+ );
+ }
+
+ public void testToObject_byte() {
+ final byte[] b = null;
+ assertEquals(null, ArrayUtils.toObject(b));
+
+ assertSame(ArrayUtils.EMPTY_BYTE_OBJECT_ARRAY,
+ ArrayUtils.toObject(new byte[0]));
+
+ assertTrue(Arrays.equals(
+ new Byte[] {new Byte(Byte.MIN_VALUE),
+ new Byte(Byte.MAX_VALUE), new Byte((byte)9999999)},
+ ArrayUtils.toObject(new byte[] {Byte.MIN_VALUE, Byte.MAX_VALUE,
+ (byte)9999999}))
+ );
+ }
+
+ // testToPrimitive/Object for short
+ // -----------------------------------------------------------------------
+ public void testToPrimitive_short() {
+ final Short[] b = null;
+ assertEquals(null, ArrayUtils.toPrimitive(b));
+
+ assertSame(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.toPrimitive(new Short[0]));
+
+ assertTrue(Arrays.equals(
+ new short[] {Short.MIN_VALUE, Short.MAX_VALUE, (short)9999999},
+ ArrayUtils.toPrimitive(new Short[] {new Short(Short.MIN_VALUE),
+ new Short(Short.MAX_VALUE), new Short((short)9999999)}))
+ );
+
+ try {
+ ArrayUtils.toPrimitive(new Short[] {new Short(Short.MIN_VALUE), null});
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ public void testToPrimitive_short_short() {
+ final Short[] s = null;
+ assertEquals(null, ArrayUtils.toPrimitive(s, Short.MIN_VALUE));
+
+ assertSame(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.toPrimitive(new Short[0],
+ Short.MIN_VALUE));
+
+ assertTrue(Arrays.equals(
+ new short[] {Short.MIN_VALUE, Short.MAX_VALUE, (short)9999999},
+ ArrayUtils.toPrimitive(new Short[] {new Short(Short.MIN_VALUE),
+ new Short(Short.MAX_VALUE), new Short((short)9999999)}, Short.MIN_VALUE))
+ );
+
+ assertTrue(Arrays.equals(
+ new short[] {Short.MIN_VALUE, Short.MAX_VALUE, (short)9999999},
+ ArrayUtils.toPrimitive(new Short[] {new Short(Short.MIN_VALUE), null,
+ new Short((short)9999999)}, Short.MAX_VALUE))
+ );
+ }
+
+ public void testToObject_short() {
+ final short[] b = null;
+ assertEquals(null, ArrayUtils.toObject(b));
+
+ assertSame(ArrayUtils.EMPTY_SHORT_OBJECT_ARRAY,
+ ArrayUtils.toObject(new short[0]));
+
+ assertTrue(Arrays.equals(
+ new Short[] {new Short(Short.MIN_VALUE), new Short(Short.MAX_VALUE),
+ new Short((short)9999999)},
+ ArrayUtils.toObject(new short[] {Short.MIN_VALUE, Short.MAX_VALUE,
+ (short)9999999}))
+ );
+ }
+
+ // testToPrimitive/Object for int
+ // -----------------------------------------------------------------------
+ public void testToPrimitive_int() {
+ final Integer[] b = null;
+ assertEquals(null, ArrayUtils.toPrimitive(b));
+ assertSame(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.toPrimitive(new Integer[0]));
+ assertTrue(Arrays.equals(
+ new int[] {Integer.MIN_VALUE, Integer.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Integer[] {new Integer(Integer.MIN_VALUE),
+ new Integer(Integer.MAX_VALUE), new Integer(9999999)}))
+ );
+
+ try {
+ ArrayUtils.toPrimitive(new Integer[] {new Integer(Integer.MIN_VALUE), null});
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ public void testToPrimitive_int_int() {
+ final Long[] l = null;
+ assertEquals(null, ArrayUtils.toPrimitive(l, Integer.MIN_VALUE));
+ assertSame(ArrayUtils.EMPTY_INT_ARRAY,
+ ArrayUtils.toPrimitive(new Integer[0], 1));
+ assertTrue(Arrays.equals(
+ new int[] {Integer.MIN_VALUE, Integer.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Integer[] {new Integer(Integer.MIN_VALUE),
+ new Integer(Integer.MAX_VALUE), new Integer(9999999)},1)));
+ assertTrue(Arrays.equals(
+ new int[] {Integer.MIN_VALUE, Integer.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Integer[] {new Integer(Integer.MIN_VALUE),
+ null, new Integer(9999999)}, Integer.MAX_VALUE))
+ );
+ }
+
+ public void testToObject_int() {
+ final int[] b = null;
+ assertEquals(null, ArrayUtils.toObject(b));
+
+ assertSame(
+ ArrayUtils.EMPTY_INTEGER_OBJECT_ARRAY,
+ ArrayUtils.toObject(new int[0]));
+
+ assertTrue(
+ Arrays.equals(
+ new Integer[] {
+ new Integer(Integer.MIN_VALUE),
+ new Integer(Integer.MAX_VALUE),
+ new Integer(9999999)},
+ ArrayUtils.toObject(
+ new int[] { Integer.MIN_VALUE, Integer.MAX_VALUE, 9999999 })));
+ }
+
+ // testToPrimitive/Object for long
+ // -----------------------------------------------------------------------
+ public void testToPrimitive_long() {
+ final Long[] b = null;
+ assertEquals(null, ArrayUtils.toPrimitive(b));
+
+ assertSame(ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.toPrimitive(new Long[0]));
+
+ assertTrue(Arrays.equals(
+ new long[] {Long.MIN_VALUE, Long.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Long[] {new Long(Long.MIN_VALUE),
+ new Long(Long.MAX_VALUE), new Long(9999999)}))
+ );
+
+ try {
+ ArrayUtils.toPrimitive(new Long[] {new Long(Long.MIN_VALUE), null});
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ public void testToPrimitive_long_long() {
+ final Long[] l = null;
+ assertEquals(null, ArrayUtils.toPrimitive(l, Long.MIN_VALUE));
+
+ assertSame(ArrayUtils.EMPTY_LONG_ARRAY,
+ ArrayUtils.toPrimitive(new Long[0], 1));
+
+ assertTrue(Arrays.equals(
+ new long[] {Long.MIN_VALUE, Long.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Long[] {new Long(Long.MIN_VALUE),
+ new Long(Long.MAX_VALUE), new Long(9999999)},1)));
+
+ assertTrue(Arrays.equals(
+ new long[] {Long.MIN_VALUE, Long.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Long[] {new Long(Long.MIN_VALUE),
+ null, new Long(9999999)}, Long.MAX_VALUE))
+ );
+ }
+
+ public void testToObject_long() {
+ final long[] b = null;
+ assertEquals(null, ArrayUtils.toObject(b));
+
+ assertSame(
+ ArrayUtils.EMPTY_LONG_OBJECT_ARRAY,
+ ArrayUtils.toObject(new long[0]));
+
+ assertTrue(
+ Arrays.equals(
+ new Long[] {
+ new Long(Long.MIN_VALUE),
+ new Long(Long.MAX_VALUE),
+ new Long(9999999)},
+ ArrayUtils.toObject(
+ new long[] { Long.MIN_VALUE, Long.MAX_VALUE, 9999999 })));
+ }
+
+ // testToPrimitive/Object for float
+ // -----------------------------------------------------------------------
+ public void testToPrimitive_float() {
+ final Float[] b = null;
+ assertEquals(null, ArrayUtils.toPrimitive(b));
+
+ assertSame(ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.toPrimitive(new Float[0]));
+
+ assertTrue(Arrays.equals(
+ new float[] {Float.MIN_VALUE, Float.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Float[] {new Float(Float.MIN_VALUE),
+ new Float(Float.MAX_VALUE), new Float(9999999)}))
+ );
+
+ try {
+ ArrayUtils.toPrimitive(new Float[] {new Float(Float.MIN_VALUE), null});
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ public void testToPrimitive_float_float() {
+ final Float[] l = null;
+ assertEquals(null, ArrayUtils.toPrimitive(l, Float.MIN_VALUE));
+
+ assertSame(ArrayUtils.EMPTY_FLOAT_ARRAY,
+ ArrayUtils.toPrimitive(new Float[0], 1));
+
+ assertTrue(Arrays.equals(
+ new float[] {Float.MIN_VALUE, Float.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Float[] {new Float(Float.MIN_VALUE),
+ new Float(Float.MAX_VALUE), new Float(9999999)},1)));
+
+ assertTrue(Arrays.equals(
+ new float[] {Float.MIN_VALUE, Float.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Float[] {new Float(Float.MIN_VALUE),
+ null, new Float(9999999)}, Float.MAX_VALUE))
+ );
+ }
+
+ public void testToObject_float() {
+ final float[] b = null;
+ assertEquals(null, ArrayUtils.toObject(b));
+
+ assertSame(
+ ArrayUtils.EMPTY_FLOAT_OBJECT_ARRAY,
+ ArrayUtils.toObject(new float[0]));
+
+ assertTrue(
+ Arrays.equals(
+ new Float[] {
+ new Float(Float.MIN_VALUE),
+ new Float(Float.MAX_VALUE),
+ new Float(9999999)},
+ ArrayUtils.toObject(
+ new float[] { Float.MIN_VALUE, Float.MAX_VALUE, 9999999 })));
+ }
+
+ // testToPrimitive/Object for double
+ // -----------------------------------------------------------------------
+ public void testToPrimitive_double() {
+ final Double[] b = null;
+ assertEquals(null, ArrayUtils.toPrimitive(b));
+
+ assertSame(ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.toPrimitive(new Double[0]));
+
+ assertTrue(Arrays.equals(
+ new double[] {Double.MIN_VALUE, Double.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Double[] {new Double(Double.MIN_VALUE),
+ new Double(Double.MAX_VALUE), new Double(9999999)}))
+ );
+
+ try {
+ ArrayUtils.toPrimitive(new Float[] {new Float(Float.MIN_VALUE), null});
+ fail();
+ } catch (NullPointerException ex) {}
+ }
+
+ public void testToPrimitive_double_double() {
+ final Double[] l = null;
+ assertEquals(null, ArrayUtils.toPrimitive(l, Double.MIN_VALUE));
+
+ assertSame(ArrayUtils.EMPTY_DOUBLE_ARRAY,
+ ArrayUtils.toPrimitive(new Double[0], 1));
+
+ assertTrue(Arrays.equals(
+ new double[] {Double.MIN_VALUE, Double.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Double[] {new Double(Double.MIN_VALUE),
+ new Double(Double.MAX_VALUE), new Double(9999999)},1)));
+
+ assertTrue(Arrays.equals(
+ new double[] {Double.MIN_VALUE, Double.MAX_VALUE, 9999999},
+ ArrayUtils.toPrimitive(new Double[] {new Double(Double.MIN_VALUE),
+ null, new Double(9999999)}, Double.MAX_VALUE))
+ );
+ }
+
+ public void testToObject_double() {
+ final double[] b = null;
+ assertEquals(null, ArrayUtils.toObject(b));
+
+ assertSame(
+ ArrayUtils.EMPTY_DOUBLE_OBJECT_ARRAY,
+ ArrayUtils.toObject(new double[0]));
+
+ assertTrue(
+ Arrays.equals(
+ new Double[] {
+ new Double(Double.MIN_VALUE),
+ new Double(Double.MAX_VALUE),
+ new Double(9999999)},
+ ArrayUtils.toObject(
+ new double[] { Double.MIN_VALUE, Double.MAX_VALUE, 9999999 })));
+ }
+
}