Add primitive/object conversions for all types
bug 21068, from Matthew Hawthorne git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137383 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
008d16ed5b
commit
48cea59f38
|
@ -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
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Converts an array of object Booleans to primitives.</p>
|
||||
|
@ -974,4 +977,412 @@ public class ArrayUtils {
|
|||
return result;
|
||||
}
|
||||
|
||||
// Byte array converters
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Converts an array of object Bytes to primitives.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Byte</code> array, may be <code>null</code>
|
||||
* @return a <code>byte</code> array
|
||||
* @throws NullPointerException if array content is <code>null</code>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of object Bytes to primitives handling null.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Byte</code> array, may be <code>null</code>
|
||||
* @param valueForNull the value to insert if <code>null</code> found
|
||||
* @return a <code>byte</code> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of primitive bytes to objects.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>byte</code> array
|
||||
* @return a <code>Byte</code> 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
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Converts an array of object Shorts to primitives.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Short</code> array, may be <code>null</code>
|
||||
* @return a <code>byte</code> array
|
||||
* @throws NullPointerException if array content is <code>null</code>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of object Short to primitives handling null.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Short</code> array, may be <code>null</code>
|
||||
* @param valueForNull the value to insert if <code>null</code> found
|
||||
* @return a <code>byte</code> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of primitive shorts to objects.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>short</code> array
|
||||
* @return a <code>Short</code> 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
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Converts an array of object Integers to primitives.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Integer</code> array, may be <code>null</code>
|
||||
* @return an <code>int</code> array
|
||||
* @throws NullPointerException if array content is <code>null</code>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of object Integer to primitives handling null.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Integer</code> array, may be <code>null</code>
|
||||
* @param valueForNull the value to insert if <code>null</code> found
|
||||
* @return an <code>int</code> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of primitive ints to objects.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array an <code>int</code> array
|
||||
* @return an <code>Integer</code> 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
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Converts an array of object Longs to primitives.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Long</code> array, may be <code>null</code>
|
||||
* @return a <code>long</code> array
|
||||
* @throws NullPointerException if array content is <code>null</code>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of object Long to primitives handling null.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Long</code> array, may be <code>null</code>
|
||||
* @param valueForNull the value to insert if <code>null</code> found
|
||||
* @return a <code>long</code> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of primitive longs to objects.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>long</code> array
|
||||
* @return a <code>Long</code> 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
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Converts an array of object Floats to primitives.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Float</code> array, may be <code>null</code>
|
||||
* @return a <code>float</code> array
|
||||
* @throws NullPointerException if array content is <code>null</code>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of object Floats to primitives handling null.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Float</code> array, may be <code>null</code>
|
||||
* @param valueForNull the value to insert if <code>null</code> found
|
||||
* @return a <code>float</code> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of primitive floats to objects.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>float</code> array
|
||||
* @return a <code>Float</code> 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
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Converts an array of object Doubles to primitives.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Double</code> array, may be <code>null</code>
|
||||
* @return a <code>double</code> array
|
||||
* @throws NullPointerException if array content is <code>null</code>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of object Doubles to primitives handling null.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Double</code> array, may be <code>null</code>
|
||||
* @param valueForNull the value to insert if <code>null</code> found
|
||||
* @return a <code>double</code> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of primitive doubles to objects.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>double</code> array
|
||||
* @return a <code>Double</code> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 })));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue