diff --git a/src/java/org/apache/commons/lang/ArrayUtils.java b/src/java/org/apache/commons/lang/ArrayUtils.java index 7f8177a47..c5ebeea2b 100644 --- a/src/java/org/apache/commons/lang/ArrayUtils.java +++ b/src/java/org/apache/commons/lang/ArrayUtils.java @@ -68,8 +68,9 @@ import org.apache.commons.lang.builder.ToStringStyle; * @author Moritz Petersen * @author Fredrik Westermarck * @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 + // ---------------------------------------------------------------------- + /** + *
Converts an array of object Booleans to primitives.
+ * + *This method returns null
if null
input.
Boolean
array, may be null
+ * @return a boolean
array
+ * @throws NullPointerException if array content is null
+ */
+ 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;
+ }
+
+ /**
+ * Converts an array of object Booleans to primitives handling null.
+ * + *This method returns null
if null
input.
Boolean
array, may be null
+ * @param valueForNull the value to insert if null
found
+ * @return a boolean
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;
+ }
+
+ /**
+ * Converts an array of primitive booleans to objects.
+ * + *This method returns null
if null
input.
boolean
array
+ * @return a Boolean
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;
+ }
+
}
diff --git a/src/test/org/apache/commons/lang/ArrayUtilsTest.java b/src/test/org/apache/commons/lang/ArrayUtilsTest.java
index c531e51e0..a01b2576c 100644
--- a/src/test/org/apache/commons/lang/ArrayUtilsTest.java
+++ b/src/test/org/apache/commons/lang/ArrayUtilsTest.java
@@ -64,10 +64,11 @@ import junit.textui.TestRunner;
/**
* Unit tests {@link org.apache.commons.lang.ArrayUtils}.
*
- * @author Stephen Colebourne
+ * @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}))
+ );
+ }
+
}