From 9cfd4409f9231736b3db262fa11229ff1a77fc5d Mon Sep 17 00:00:00 2001
From: Stephen Colebourne
Negates the specified boolean.
* @@ -95,7 +95,6 @@ public class BooleanUtils { // boolean Boolean methods //-------------------------------------------------------------------------- - /** *Boolean factory that avoids creating new Boolean objecs all the time.
* @@ -137,7 +136,6 @@ public class BooleanUtils { // Integer to Boolean methods //-------------------------------------------------------------------------- - /** *Convert an int to a boolean using the convention that zero is false.
* @@ -271,7 +269,6 @@ public class BooleanUtils { // Boolean to Integer methods //-------------------------------------------------------------------------- - /** *Convert a boolean to an int using the convention that zero is false.
* @@ -365,7 +362,6 @@ public class BooleanUtils { // String to Boolean methods //-------------------------------------------------------------------------- - /** *Converts a String to a Boolean.
* @@ -425,7 +421,6 @@ public class BooleanUtils { // String to boolean methods //-------------------------------------------------------------------------- - /** *Converts a String to a boolean.
* @@ -476,7 +471,6 @@ public class BooleanUtils { // Boolean to String methods //-------------------------------------------------------------------------- - /** *Converts a Boolean to a String returning 'true', 'false', or null
.
Converts a boolean to a String returning 'true' or 'false'.
* @@ -568,4 +561,53 @@ public class BooleanUtils { return (bool ? trueString : falseString); } + // xor methods + // -------------------------------------------------------------------------- + /** + * Performs an xor on a set of booleans. + * + * @param array an array ofbooleans
+ * @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 @@ import junit.textui.TestRunner;
* 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 class BooleanUtilsTest extends TestCase {
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());
+ }
+
}