From c808034989f77791c709bc644046be27f908c08e Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Sun, 17 Jul 2011 01:39:14 +0000 Subject: [PATCH] [LANG-722] Add BooleanUtils.and/or varargs methods to complement xor git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1147511 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang3/BooleanUtils.java | 156 +++++++- .../commons/lang3/BooleanUtilsTest.java | 366 ++++++++++++++++++ 2 files changed, 511 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/BooleanUtils.java b/src/main/java/org/apache/commons/lang3/BooleanUtils.java index ec5c98293..c5a09f35a 100644 --- a/src/main/java/org/apache/commons/lang3/BooleanUtils.java +++ b/src/main/java/org/apache/commons/lang3/BooleanUtils.java @@ -867,15 +867,150 @@ public static String toString(boolean bool, String trueString, String falseStrin return bool ? trueString : falseString; } - // xor methods + // logical operations // ---------------------------------------------------------------------- + /** + *

Performs an and on a set of booleans.

+ * + *
+     *   BooleanUtils.and(true, true)         = true
+     *   BooleanUtils.and(false, false)       = false
+     *   BooleanUtils.and(true, false)        = false
+     *   BooleanUtils.and(true, true, false)  = false
+     *   BooleanUtils.and(true, true, true)   = true
+     * 
+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean and(boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *

Performs an and on an array of Booleans.

+ * + *
+     *   BooleanUtils.and(Boolean.TRUE, Boolean.TRUE)                 = Boolean.TRUE
+     *   BooleanUtils.and(Boolean.FALSE, Boolean.FALSE)               = Boolean.FALSE
+     *   BooleanUtils.and(Boolean.TRUE, Boolean.FALSE)                = Boolean.FALSE
+     *   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
+     * 
+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException 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 and(Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *

Performs an or on a set of booleans.

+ * + *
+     *   BooleanUtils.or(true, true)          = true
+     *   BooleanUtils.or(false, false)        = false
+     *   BooleanUtils.or(true, false)         = true
+     *   BooleanUtils.or(true, true, false)   = true
+     *   BooleanUtils.or(true, true, true)    = true
+     *   BooleanUtils.or(false, false, false) = false
+     * 
+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean or(boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *

Performs an or on an array of Booleans.

+ * + *
+     *   BooleanUtils.or(Boolean.TRUE, Boolean.TRUE)                  = Boolean.TRUE
+     *   BooleanUtils.or(Boolean.FALSE, Boolean.FALSE)                = Boolean.FALSE
+     *   BooleanUtils.or(Boolean.TRUE, Boolean.FALSE)                 = Boolean.TRUE
+     *   BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE)    = Boolean.TRUE
+     *   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
+     * 
+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException 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 or(Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + /** *

Performs an xor on a set of booleans.

* *
-     *   BooleanUtils.xor(new boolean[] { true, true })   = false
-     *   BooleanUtils.xor(new boolean[] { false, false }) = false
-     *   BooleanUtils.xor(new boolean[] { true, false })  = true
+     *   BooleanUtils.xor(true, true)   = false
+     *   BooleanUtils.xor(false, false) = false
+     *   BooleanUtils.xor(true, false)  = true
+     *   BooleanUtils.xor(true, true)   = false
+     *   BooleanUtils.xor(false, false) = false
+     *   BooleanUtils.xor(true, false)  = true
      * 
* * @param array an array of {@code boolean}s @@ -891,7 +1026,7 @@ public static boolean xor(boolean... array) { if (array.length == 0) { throw new IllegalArgumentException("Array is empty"); } - + // Loops through array, comparing each item int trueCount = 0; for (boolean element : array) { @@ -905,11 +1040,11 @@ public static boolean xor(boolean... array) { } } } - + // Returns true if there was exactly 1 true item return trueCount == 1; } - + /** *

Performs an xor on an array of Booleans.

* @@ -932,13 +1067,12 @@ public static Boolean xor(Boolean... array) { if (array.length == 0) { throw new IllegalArgumentException("Array is empty"); } - boolean[] primitive = null; try { - primitive = ArrayUtils.toPrimitive(array); + boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; } catch (NullPointerException ex) { throw new IllegalArgumentException("The array must not contain any null elements"); } - return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; } - + } diff --git a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java index 0cb6f806f..b1069f60e 100644 --- a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java @@ -611,4 +611,370 @@ public void testXor_object_validInput_3items() { .booleanValue()); } + // testAnd + // ----------------------------------------------------------------------- + @Test(expected = IllegalArgumentException.class) + public void testAnd_primitive_nullInput() { + BooleanUtils.and((boolean[]) null); + } + + @Test(expected = IllegalArgumentException.class) + public void testAnd_primitive_emptyInput() { + BooleanUtils.and(new boolean[] {}); + } + + @Test + public void testAnd_primitive_validInput_2items() { + assertTrue( + "False result for (true, true)", + BooleanUtils.and(new boolean[] { true, true })); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils.and(new boolean[] { false, false })); + + assertTrue( + "True result for (true, false)", + ! BooleanUtils.and(new boolean[] { true, false })); + + assertTrue( + "True result for (false, true)", + ! BooleanUtils.and(new boolean[] { false, true })); + } + + @Test + public void testAnd_primitive_validInput_3items() { + assertTrue( + "True result for (false, false, true)", + ! BooleanUtils.and(new boolean[] { false, false, true })); + + assertTrue( + "True result for (false, true, false)", + ! BooleanUtils.and(new boolean[] { false, true, false })); + + assertTrue( + "True result for (true, false, false)", + ! BooleanUtils.and(new boolean[] { true, false, false })); + + assertTrue( + "False result for (true, true, true)", + BooleanUtils.and(new boolean[] { true, true, true })); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils.and(new boolean[] { false, false, false })); + + assertTrue( + "True result for (true, true, false)", + ! BooleanUtils.and(new boolean[] { true, true, false })); + + assertTrue( + "True result for (true, false, true)", + ! BooleanUtils.and(new boolean[] { true, false, true })); + + assertTrue( + "True result for (false, true, true)", + ! BooleanUtils.and(new boolean[] { false, true, true })); + } + + @Test(expected = IllegalArgumentException.class) + public void testAnd_object_nullInput() { + BooleanUtils.and((Boolean[]) null); + } + + @Test(expected = IllegalArgumentException.class) + public void testAnd_object_emptyInput() { + BooleanUtils.and(new Boolean[] {}); + } + + @Test(expected = IllegalArgumentException.class) + public void testAnd_object_nullElementInput() { + BooleanUtils.and(new Boolean[] {null}); + } + + @Test + public void testAnd_object_validInput_2items() { + assertTrue( + "False result for (true, true)", + BooleanUtils + .and(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils + .and(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "True result for (true, false)", + ! BooleanUtils + .and(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "True result for (false, true)", + ! BooleanUtils + .and(new Boolean[] { Boolean.FALSE, Boolean.TRUE }) + .booleanValue()); + } + + @Test + public void testAnd_object_validInput_3items() { + assertTrue( + "True result for (false, false, true)", + ! BooleanUtils + .and( + new Boolean[] { + Boolean.FALSE, + Boolean.FALSE, + Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "True result for (false, true, false)", + ! BooleanUtils + .and( + new Boolean[] { + Boolean.FALSE, + Boolean.TRUE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "True result for (true, false, false)", + ! BooleanUtils + .and( + new Boolean[] { + Boolean.TRUE, + Boolean.FALSE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (true, true, true)", + BooleanUtils + .and(new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils.and( + new Boolean[] { + Boolean.FALSE, + Boolean.FALSE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "True result for (true, true, false)", + ! BooleanUtils.and( + new Boolean[] { + Boolean.TRUE, + Boolean.TRUE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "True result for (true, false, true)", + ! BooleanUtils.and( + new Boolean[] { + Boolean.TRUE, + Boolean.FALSE, + Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "True result for (false, true, true)", + ! BooleanUtils.and( + new Boolean[] { + Boolean.FALSE, + Boolean.TRUE, + Boolean.TRUE }) + .booleanValue()); + } + + // testOr + // ----------------------------------------------------------------------- + @Test(expected = IllegalArgumentException.class) + public void testOr_primitive_nullInput() { + BooleanUtils.or((boolean[]) null); + } + + @Test(expected = IllegalArgumentException.class) + public void testOr_primitive_emptyInput() { + BooleanUtils.or(new boolean[] {}); + } + + @Test + public void testOr_primitive_validInput_2items() { + assertTrue( + "False result for (true, true)", + BooleanUtils.or(new boolean[] { true, true })); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils.or(new boolean[] { false, false })); + + assertTrue( + "False result for (true, false)", + BooleanUtils.or(new boolean[] { true, false })); + + assertTrue( + "False result for (false, true)", + BooleanUtils.or(new boolean[] { false, true })); + } + + @Test + public void testOr_primitive_validInput_3items() { + assertTrue( + "False result for (false, false, true)", + BooleanUtils.or(new boolean[] { false, false, true })); + + assertTrue( + "False result for (false, true, false)", + BooleanUtils.or(new boolean[] { false, true, false })); + + assertTrue( + "False result for (true, false, false)", + BooleanUtils.or(new boolean[] { true, false, false })); + + assertTrue( + "False result for (true, true, true)", + BooleanUtils.or(new boolean[] { true, true, true })); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils.or(new boolean[] { false, false, false })); + + assertTrue( + "False result for (true, true, false)", + BooleanUtils.or(new boolean[] { true, true, false })); + + assertTrue( + "False result for (true, false, true)", + BooleanUtils.or(new boolean[] { true, false, true })); + + assertTrue( + "False result for (false, true, true)", + BooleanUtils.or(new boolean[] { false, true, true })); + + } + @Test(expected = IllegalArgumentException.class) + public void testOr_object_nullInput() { + BooleanUtils.or((Boolean[]) null); + } + + @Test(expected = IllegalArgumentException.class) + public void testOr_object_emptyInput() { + BooleanUtils.or(new Boolean[] {}); + } + + @Test(expected = IllegalArgumentException.class) + public void testOr_object_nullElementInput() { + BooleanUtils.or(new Boolean[] {null}); + } + + @Test + public void testOr_object_validInput_2items() { + assertTrue( + "False result for (true, true)", + BooleanUtils + .or(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils + .or(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (true, false)", + BooleanUtils + .or(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (false, true)", + BooleanUtils + .or(new Boolean[] { Boolean.FALSE, Boolean.TRUE }) + .booleanValue()); + } + + @Test + public void testOr_object_validInput_3items() { + assertTrue( + "False result for (false, false, true)", + BooleanUtils + .or( + new Boolean[] { + Boolean.FALSE, + Boolean.FALSE, + Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "False result for (false, true, false)", + BooleanUtils + .or( + new Boolean[] { + Boolean.FALSE, + Boolean.TRUE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (true, false, false)", + BooleanUtils + .or( + new Boolean[] { + Boolean.TRUE, + Boolean.FALSE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (true, true, true)", + BooleanUtils + .or(new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils.or( + new Boolean[] { + Boolean.FALSE, + Boolean.FALSE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (true, true, false)", + BooleanUtils.or( + new Boolean[] { + Boolean.TRUE, + Boolean.TRUE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (true, false, true)", + BooleanUtils.or( + new Boolean[] { + Boolean.TRUE, + Boolean.FALSE, + Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "False result for (false, true, true)", + BooleanUtils.or( + new Boolean[] { + Boolean.FALSE, + Boolean.TRUE, + Boolean.TRUE }) + .booleanValue()); + } + }