From 9cfd4409f9231736b3db262fa11229ff1a77fc5d Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 28 Jun 2003 18:16:03 +0000 Subject: [PATCH] Add XOR methods bug 21068, from Matthew Hawthorne git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137384 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/BooleanUtils.java | 60 +++++- .../apache/commons/lang/BooleanUtilsTest.java | 187 +++++++++++++++++- 2 files changed, 237 insertions(+), 10 deletions(-) diff --git a/src/java/org/apache/commons/lang/BooleanUtils.java b/src/java/org/apache/commons/lang/BooleanUtils.java index a162cc5fe..9b1d24c90 100644 --- a/src/java/org/apache/commons/lang/BooleanUtils.java +++ b/src/java/org/apache/commons/lang/BooleanUtils.java @@ -60,8 +60,9 @@ * boolean and Boolean objects.

* * @author Stephen Colebourne + * @author Matthew Hawthorne * @since 2.0 - * @version $Id: BooleanUtils.java,v 1.5 2003/06/24 21:14:50 scolebourne Exp $ + * @version $Id: BooleanUtils.java,v 1.6 2003/06/28 18:16:03 scolebourne Exp $ */ public class BooleanUtils { @@ -77,7 +78,6 @@ public BooleanUtils() { // Boolean utilities //-------------------------------------------------------------------------- - /** *

Negates the specified boolean.

* @@ -95,7 +95,6 @@ public static Boolean negate(Boolean bool) { // boolean Boolean methods //-------------------------------------------------------------------------- - /** *

Boolean factory that avoids creating new Boolean objecs all the time.

* @@ -137,7 +136,6 @@ public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull) // Integer to Boolean methods //-------------------------------------------------------------------------- - /** *

Convert an int to a boolean using the convention that zero is false.

* @@ -271,7 +269,6 @@ public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer // Boolean to Integer methods //-------------------------------------------------------------------------- - /** *

Convert a boolean to an int using the convention that zero is false.

* @@ -365,7 +362,6 @@ public static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer f // String to Boolean methods //-------------------------------------------------------------------------- - /** *

Converts a String to a Boolean.

* @@ -425,7 +421,6 @@ public static Boolean toBooleanObject(String str, String trueString, String fals // String to boolean methods //-------------------------------------------------------------------------- - /** *

Converts a String to a boolean.

* @@ -476,7 +471,6 @@ public static boolean toBoolean(String str, String trueString, String falseStrin // Boolean to String methods //-------------------------------------------------------------------------- - /** *

Converts a Boolean to a String returning 'true', 'false', or null.

* @@ -525,7 +519,6 @@ public static String toString(Boolean bool, String trueString, String falseStrin // boolean to String methods //-------------------------------------------------------------------------- - /** *

Converts a boolean to a String returning 'true' or 'false'.

* @@ -568,4 +561,53 @@ public static String toString(boolean bool, String trueString, String falseStrin return (bool ? trueString : falseString); } + // xor methods + // -------------------------------------------------------------------------- + /** + * Performs an xor on a set of booleans. + * + * @param array an array of booleans + * @return true if the xor is successful. + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty. + */ + public static boolean xor(boolean[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // Loops through array, comparing each item + int trueCount = 0; + for (int i = 0; i < array.length; i++) { + // If item is true, and trueCount is < 1, increments count + // Else, xor fails + if (array[i]) { + if (trueCount < 1) { + trueCount++; + } else { + return false; + } + } + } + + // Returns true if there was exactly 1 true item + return trueCount == 1; + } + + /** + * Performs an xor on an array of Booleans. + * + * @param array an array of Booleans + * @return true if the xor is successful. + * @throws NullPointerException if array contains a null + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty. + */ + public static Boolean xor(Boolean[] array) { + return new Boolean(xor(ArrayUtils.toPrimitive(array))); + } + } diff --git a/src/test/org/apache/commons/lang/BooleanUtilsTest.java b/src/test/org/apache/commons/lang/BooleanUtilsTest.java index f420efdc2..99dd7d70f 100644 --- a/src/test/org/apache/commons/lang/BooleanUtilsTest.java +++ b/src/test/org/apache/commons/lang/BooleanUtilsTest.java @@ -62,7 +62,8 @@ * Unit tests {@link org.apache.commons.lang.BooleanUtils}. * * @author Stephen Colebourne - * @version $Id: BooleanUtilsTest.java,v 1.3 2003/03/23 21:47:30 scolebourne Exp $ + * @author Matthew Hawthorne + * @version $Id: BooleanUtilsTest.java,v 1.4 2003/06/28 18:16:03 scolebourne Exp $ */ public class BooleanUtilsTest extends TestCase { @@ -337,4 +338,188 @@ public void test_toString_boolean_String_String_String() { assertEquals("N", BooleanUtils.toString(false, "Y", "N")); } + // testXor + // ----------------------------------------------------------------------- + public void testXor_primitive_nullInput() { + final boolean[] b = null; + try { + BooleanUtils.xor(b); + fail("Exception was not thrown for null input."); + } catch (IllegalArgumentException ex) {} + } + + public void testXor_primitive_emptyInput() { + try { + BooleanUtils.xor(new boolean[] {}); + fail("Exception was not thrown for empty input."); + } catch (IllegalArgumentException ex) {} + } + + public void testXor_primitive_validInput_2items() { + assertTrue( + "True result for (true, true)", + ! BooleanUtils.xor(new boolean[] { true, true })); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils.xor(new boolean[] { false, false })); + + assertTrue( + "False result for (true, false)", + BooleanUtils.xor(new boolean[] { true, false })); + + assertTrue( + "False result for (false, true)", + BooleanUtils.xor(new boolean[] { false, true })); + } + + public void testXor_primitive_validInput_3items() { + assertTrue( + "False result for (false, false, true)", + BooleanUtils.xor(new boolean[] { false, false, true })); + + assertTrue( + "False result for (false, true, false)", + BooleanUtils.xor(new boolean[] { false, true, false })); + + assertTrue( + "False result for (true, false, false)", + BooleanUtils.xor(new boolean[] { true, false, false })); + + assertTrue( + "True result for (true, true, true)", + ! BooleanUtils.xor(new boolean[] { true, true, true })); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils.xor(new boolean[] { false, false, false })); + + assertTrue( + "True result for (true, true, false)", + ! BooleanUtils.xor(new boolean[] { true, true, false })); + + assertTrue( + "True result for (true, false, true)", + ! BooleanUtils.xor(new boolean[] { true, false, true })); + + assertTrue( + "False result for (false, true, true)", + ! BooleanUtils.xor(new boolean[] { false, true, true })); + } + + public void testXor_object_nullInput() { + final Boolean[] b = null; + try { + BooleanUtils.xor(b); + fail("Exception was not thrown for null input."); + } catch (IllegalArgumentException ex) {} + } + + public void testXor_object_emptyInput() { + try { + BooleanUtils.xor(new Boolean[] {}); + fail("Exception was not thrown for empty input."); + } catch (IllegalArgumentException ex) {} + } + + public void testXor_object_validInput_2items() { + assertTrue( + "True result for (true, true)", + ! BooleanUtils + .xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils + .xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (true, false)", + BooleanUtils + .xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (false, true)", + BooleanUtils + .xor(new Boolean[] { Boolean.FALSE, Boolean.TRUE }) + .booleanValue()); + } + + public void testXor_object_validInput_3items() { + assertTrue( + "False result for (false, false, true)", + BooleanUtils + .xor( + new Boolean[] { + Boolean.FALSE, + Boolean.FALSE, + Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "False result for (false, true, false)", + BooleanUtils + .xor( + new Boolean[] { + Boolean.FALSE, + Boolean.TRUE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "False result for (true, false, false)", + BooleanUtils + .xor( + new Boolean[] { + Boolean.TRUE, + Boolean.FALSE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "True result for (true, true, true)", + ! BooleanUtils + .xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "True result for (false, false)", + ! BooleanUtils.xor( + new Boolean[] { + Boolean.FALSE, + Boolean.FALSE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "True result for (true, true, false)", + ! BooleanUtils.xor( + new Boolean[] { + Boolean.TRUE, + Boolean.TRUE, + Boolean.FALSE }) + .booleanValue()); + + assertTrue( + "True result for (true, false, true)", + ! BooleanUtils.xor( + new Boolean[] { + Boolean.TRUE, + Boolean.FALSE, + Boolean.TRUE }) + .booleanValue()); + + assertTrue( + "False result for (false, true, true)", + ! BooleanUtils.xor( + new Boolean[] { + Boolean.FALSE, + Boolean.TRUE, + Boolean.TRUE }) + .booleanValue()); + } + }