Add primitive boolean/object conversions
Bug 21068, from Matthew Hawthorne git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137381 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2878a430c1
commit
a5c0361ee5
|
@ -68,8 +68,9 @@ import org.apache.commons.lang.builder.ToStringStyle;
|
|||
* @author Moritz Petersen
|
||||
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
|
||||
* @author Nikolay Metchev
|
||||
* @author Matthew Hawthorne
|
||||
* @since 2.0
|
||||
* @version $Id: ArrayUtils.java,v 1.14 2003/06/25 23:32:08 scolebourne Exp $
|
||||
* @version $Id: ArrayUtils.java,v 1.15 2003/06/25 23:33:47 scolebourne Exp $
|
||||
*/
|
||||
public class ArrayUtils {
|
||||
|
||||
|
@ -905,4 +906,72 @@ public class ArrayUtils {
|
|||
return (indexOf(array, objectToFind) != -1);
|
||||
}
|
||||
|
||||
// Primitive/Object converters
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Converts an array of object Booleans to primitives.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Boolean</code> array, may be <code>null</code>
|
||||
* @return a <code>boolean</code> array
|
||||
* @throws NullPointerException if array content is <code>null</code>
|
||||
*/
|
||||
public static boolean[] toPrimitive(final Boolean[] array) {
|
||||
if (array == null) {
|
||||
return null;
|
||||
} else if (array.length == 0) {
|
||||
return EMPTY_BOOLEAN_ARRAY;
|
||||
}
|
||||
final boolean[] result = new boolean[array.length];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
result[i] = array[i].booleanValue();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of object Booleans to primitives handling null.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>Boolean</code> array, may be <code>null</code>
|
||||
* @param valueForNull the value to insert if <code>null</code> found
|
||||
* @return a <code>boolean</code> array
|
||||
*/
|
||||
public static boolean[] toPrimitive(final Boolean[] array, final boolean valueForNull) {
|
||||
if (array == null) {
|
||||
return null;
|
||||
} else if (array.length == 0) {
|
||||
return EMPTY_BOOLEAN_ARRAY;
|
||||
}
|
||||
final boolean[] result = new boolean[array.length];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
Boolean b = array[i];
|
||||
result[i] = (b == null ? valueForNull : b.booleanValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts an array of primitive booleans to objects.</p>
|
||||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> input.</p>
|
||||
*
|
||||
* @param array a <code>boolean</code> array
|
||||
* @return a <code>Boolean</code> array
|
||||
*/
|
||||
public static Boolean[] toObject(final boolean[] array) {
|
||||
if (array == null) {
|
||||
return null;
|
||||
} else if (array.length == 0) {
|
||||
return EMPTY_BOOLEAN_OBJECT_ARRAY;
|
||||
}
|
||||
final Boolean[] result = new Boolean[array.length];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -64,10 +64,11 @@ import junit.textui.TestRunner;
|
|||
/**
|
||||
* Unit tests {@link org.apache.commons.lang.ArrayUtils}.
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @author Stephen Colebourne
|
||||
* @author Moritz Petersen
|
||||
* @author Nikolay Metchev
|
||||
* @version $Id: ArrayUtilsTest.java,v 1.6 2003/03/23 21:47:30 scolebourne Exp $
|
||||
* @author Matthew Hawthorne
|
||||
* @version $Id: ArrayUtilsTest.java,v 1.7 2003/06/25 23:33:47 scolebourne Exp $
|
||||
*/
|
||||
public class ArrayUtilsTest extends TestCase {
|
||||
|
||||
|
@ -80,8 +81,8 @@ public class ArrayUtilsTest extends TestCase {
|
|||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(ArrayUtilsTest.class);
|
||||
suite.setName("ArrayUtils Tests");
|
||||
TestSuite suite = new TestSuite(ArrayUtilsTest.class);
|
||||
suite.setName("ArrayUtils Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
@ -706,4 +707,47 @@ public class ArrayUtilsTest extends TestCase {
|
|||
assertEquals(true, ArrayUtils.contains(array, null));
|
||||
assertEquals(false, ArrayUtils.contains(array, "notInArray"));
|
||||
}
|
||||
|
||||
// testToPrimitive/Object for boolean
|
||||
// -----------------------------------------------------------------------
|
||||
public void testToPrimitive_boolean() {
|
||||
assertEquals(null, ArrayUtils.toPrimitive(null));
|
||||
assertSame(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.toPrimitive(new Boolean[0]));
|
||||
assertTrue(Arrays.equals(
|
||||
new boolean[] {true, false, true},
|
||||
ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, Boolean.FALSE, Boolean.TRUE}))
|
||||
);
|
||||
|
||||
try {
|
||||
ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, null});
|
||||
fail();
|
||||
} catch (NullPointerException ex) {}
|
||||
}
|
||||
|
||||
public void testToPrimitive_boolean_boolean() {
|
||||
assertEquals(null, ArrayUtils.toPrimitive(null, false));
|
||||
assertSame(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.toPrimitive(new Boolean[0], false));
|
||||
assertTrue(Arrays.equals(
|
||||
new boolean[] {true, false, true},
|
||||
ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, Boolean.FALSE, Boolean.TRUE}, false))
|
||||
);
|
||||
assertTrue(Arrays.equals(
|
||||
new boolean[] {true, false, false},
|
||||
ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, null, Boolean.FALSE}, false))
|
||||
);
|
||||
assertTrue(Arrays.equals(
|
||||
new boolean[] {true, true, false},
|
||||
ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, null, Boolean.FALSE}, true))
|
||||
);
|
||||
}
|
||||
|
||||
public void testToObject_boolean() {
|
||||
assertEquals(null, ArrayUtils.toObject(null));
|
||||
assertSame(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, ArrayUtils.toObject(new boolean[0]));
|
||||
assertTrue(Arrays.equals(
|
||||
new Boolean[] {Boolean.TRUE, Boolean.FALSE, Boolean.TRUE},
|
||||
ArrayUtils.toObject(new boolean[] {true, false, true}))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue