diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 024d766a5..205c78839 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -66,6 +66,7 @@ The type attribute can be add,update,fix,remove. Use Set instead of List for checking the contains() method #734. Javadoc for StringUtils.substringBefore(String str, int separator) doesn't mention that the separator is an int. Fix NullPointerException in ThreadUtils.getSystemThreadGroup() when the current thread is stopped. + ArrayUtils.toPrimitive(Boolean...) null array elements map to false, like Boolean.parseBoolean(null) and its callers return false. Add EnumUtils.getEnumSystemProperty(...). Add TriConsumer. diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 501220566..c83a2ea0c 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -9250,23 +9250,15 @@ public class ArrayUtils { *

* This method returns {@code null} for a {@code null} input array. *

+ *

+ * Null array elements map to false, like {@code Boolean.parseBoolean(null)} and its callers return false. + *

* - * @param array a {@code Boolean} array, may be {@code null} + * @param array a {@code Boolean} array, may be {@code null} * @return a {@code boolean} array, {@code null} if null array input - * @throws NullPointerException if an array element is {@code null} */ public static boolean[] toPrimitive(final Boolean[] array) { - if (array == null) { - return null; - } - 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; + return toPrimitive(array, false); } /** diff --git a/src/main/java/org/apache/commons/lang3/BooleanUtils.java b/src/main/java/org/apache/commons/lang3/BooleanUtils.java index 920a5988c..34e699df1 100644 --- a/src/main/java/org/apache/commons/lang3/BooleanUtils.java +++ b/src/main/java/org/apache/commons/lang3/BooleanUtils.java @@ -101,7 +101,6 @@ public class BooleanUtils { /** *

Performs an 'and' operation on an array of Booleans.

- * *
      *   BooleanUtils.and(Boolean.TRUE, Boolean.TRUE)                 = Boolean.TRUE
      *   BooleanUtils.and(Boolean.FALSE, Boolean.FALSE)               = Boolean.FALSE
@@ -109,23 +108,22 @@ public class BooleanUtils {
      *   BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE)   = Boolean.TRUE
      *   BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE
      *   BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE)  = Boolean.FALSE
+     *   BooleanUtils.and(null, null)                                 = Boolean.FALSE
      * 
+ *

+ * Null array elements map to false, like {@code Boolean.parseBoolean(null)} and its callers return false. + *

* * @param array an array of {@code Boolean}s * @return the result of the logical 'and' operation. That is {@code false} * if any of the parameters is {@code false} and {@code true} otherwise. * @throws NullPointerException if {@code array} is {@code null} * @throws IllegalArgumentException if {@code array} is empty. - * @throws IllegalArgumentException if {@code array} contains a {@code null} * @since 3.0.1 */ public static Boolean and(final Boolean... array) { ObjectUtils.requireNonEmpty(array, "array"); - try { - return and(ArrayUtils.toPrimitive(array)) ? Boolean.TRUE : Boolean.FALSE; - } catch (final NullPointerException ex) { - throw new IllegalArgumentException("The array must not contain any null elements"); - } + return and(ArrayUtils.toPrimitive(array)) ? Boolean.TRUE : Boolean.FALSE; } /** @@ -279,7 +277,6 @@ public class BooleanUtils { /** *

Performs an 'or' operation on an array of Booleans.

- * *
      *   BooleanUtils.or(Boolean.TRUE, Boolean.TRUE)                  = Boolean.TRUE
      *   BooleanUtils.or(Boolean.FALSE, Boolean.FALSE)                = Boolean.FALSE
@@ -288,22 +285,22 @@ public class BooleanUtils {
      *   BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE)  = Boolean.TRUE
      *   BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE)   = Boolean.TRUE
      *   BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE
+     *   BooleanUtils.or(Boolean.TRUE, null)                          = Boolean.TRUE
+     *   BooleanUtils.or(Boolean.FALSE, null)                         = Boolean.FALSE
      * 
+ *

+ * Null array elements map to false, like {@code Boolean.parseBoolean(null)} and its callers return false. + *

* * @param array an array of {@code Boolean}s * @return {@code true} if any of the arguments is {@code true}, and it returns {@code false} otherwise. * @throws NullPointerException if {@code array} is {@code null} * @throws IllegalArgumentException if {@code array} is empty. - * @throws IllegalArgumentException if {@code array} contains a {@code null} * @since 3.0.1 */ public static Boolean or(final Boolean... array) { ObjectUtils.requireNonEmpty(array, "array"); - try { - return or(ArrayUtils.toPrimitive(array)) ? Boolean.TRUE : Boolean.FALSE; - } catch (final NullPointerException ex) { - throw new IllegalArgumentException("The array must not contain any null elements"); - } + return or(ArrayUtils.toPrimitive(array)) ? Boolean.TRUE : Boolean.FALSE; } /** @@ -1103,27 +1100,26 @@ public class BooleanUtils { /** *

Performs an xor on an array of Booleans.

- * *
-     *   BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE })   = Boolean.FALSE
-     *   BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE
-     *   BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE })  = Boolean.TRUE
-     *   BooleanUtils.xor(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE)     = Boolean.TRUE
+     *   BooleanUtils.xor(Boolean.TRUE, Boolean.TRUE)                 = Boolean.FALSE
+     *   BooleanUtils.xor(Boolean.FALSE, Boolean.FALSE)               = Boolean.FALSE
+     *   BooleanUtils.xor(Boolean.TRUE, Boolean.FALSE)                = Boolean.TRUE
+     *   BooleanUtils.xor(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE) = Boolean.TRUE
+     *   BooleanUtils.xor(Boolean.FALSE, null)                        = Boolean.FALSE
+     *   BooleanUtils.xor(Boolean.TRUE, null)                         = Boolean.TRUE
      * 
+ *

+ * Null array elements map to false, like {@code Boolean.parseBoolean(null)} and its callers return false. + *

* * @param array an array of {@code Boolean}s * @return the result of the xor operations * @throws NullPointerException if {@code array} is {@code null} * @throws IllegalArgumentException if {@code array} is empty. - * @throws IllegalArgumentException if {@code array} contains a {@code null} */ public static Boolean xor(final Boolean... array) { ObjectUtils.requireNonEmpty(array, "array"); - try { - return xor(ArrayUtils.toPrimitive(array)) ? Boolean.TRUE : Boolean.FALSE; - } catch (final NullPointerException ex) { - throw new IllegalArgumentException("The array must not contain any null elements"); - } + return xor(ArrayUtils.toPrimitive(array)) ? Boolean.TRUE : Boolean.FALSE; } /** diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 292a4a5fd..3e76bf273 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -6282,8 +6282,7 @@ public class ArrayUtilsTest { assertNull(ArrayUtils.toPrimitive(b)); assertSame(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.toPrimitive(new Boolean[0])); assertArrayEquals(new boolean[]{true, false, true}, ArrayUtils.toPrimitive(new Boolean[]{Boolean.TRUE, Boolean.FALSE, Boolean.TRUE})); - - assertThrows(NullPointerException.class, () -> ArrayUtils.toPrimitive(new Boolean[]{Boolean.TRUE, null})); + assertArrayEquals(new boolean[]{true, false}, ArrayUtils.toPrimitive(new Boolean[]{Boolean.TRUE, null})); } @Test diff --git a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java index 98147d7e8..e4b29c82e 100644 --- a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java @@ -436,7 +436,7 @@ public class BooleanUtilsTest { @Test public void testAnd_object_nullElementInput() { - assertThrows(IllegalArgumentException.class, () -> BooleanUtils.and(new Boolean[] {null})); + assertEquals(Boolean.FALSE, BooleanUtils.and(new Boolean[] {null})); } @Test @@ -635,7 +635,7 @@ public class BooleanUtilsTest { @Test public void testOr_object_nullElementInput() { - assertThrows(IllegalArgumentException.class, () -> BooleanUtils.or(new Boolean[] {null})); + assertEquals(Boolean.FALSE, BooleanUtils.or(new Boolean[] {null})); } @Test @@ -817,7 +817,7 @@ public class BooleanUtilsTest { @Test public void testXor_object_nullElementInput() { - assertThrows(IllegalArgumentException.class, () -> BooleanUtils.xor(new Boolean[] {null})); + assertEquals(Boolean.FALSE, BooleanUtils.xor(new Boolean[] {null})); } @Test