scolebourne 2003/06/28 11:16:03 Modified: lang/src/test/org/apache/commons/lang BooleanUtilsTest.java lang/src/java/org/apache/commons/lang BooleanUtils.java Log: Add XOR methods bug 21068, from Matthew Hawthorne Revision Changes Path 1.4 +186 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/BooleanUtilsTest.java Index: BooleanUtilsTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/BooleanUtilsTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- BooleanUtilsTest.java 23 Mar 2003 21:47:30 -0000 1.3 +++ BooleanUtilsTest.java 28 Jun 2003 18:16:03 -0000 1.4 @@ -62,6 +62,7 @@ * Unit tests [EMAIL PROTECTED] org.apache.commons.lang.BooleanUtils}. * * @author Stephen Colebourne + * @author Matthew Hawthorne * @version $Id$ */ public class BooleanUtilsTest extends TestCase { @@ -337,4 +338,188 @@ 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()); + } + } 1.6 +51 -9 jakarta-commons/lang/src/java/org/apache/commons/lang/BooleanUtils.java Index: BooleanUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/BooleanUtils.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- BooleanUtils.java 24 Jun 2003 21:14:50 -0000 1.5 +++ BooleanUtils.java 28 Jun 2003 18:16:03 -0000 1.6 @@ -60,6 +60,7 @@ * boolean and Boolean objects.</p> * * @author Stephen Colebourne + * @author Matthew Hawthorne * @since 2.0 * @version $Id$ */ @@ -77,7 +78,6 @@ // Boolean utilities //-------------------------------------------------------------------------- - /** * <p>Negates the specified boolean.</p> * @@ -95,7 +95,6 @@ // boolean Boolean methods //-------------------------------------------------------------------------- - /** * <p>Boolean factory that avoids creating new Boolean objecs all the time.</p> * @@ -137,7 +136,6 @@ // Integer to Boolean methods //-------------------------------------------------------------------------- - /** * <p>Convert an int to a boolean using the convention that zero is false.</p> * @@ -271,7 +269,6 @@ // Boolean to Integer methods //-------------------------------------------------------------------------- - /** * <p>Convert a boolean to an int using the convention that zero is false.</p> * @@ -365,7 +362,6 @@ // String to Boolean methods //-------------------------------------------------------------------------- - /** * <p>Converts a String to a Boolean.</p> * @@ -425,7 +421,6 @@ // String to boolean methods //-------------------------------------------------------------------------- - /** * <p>Converts a String to a boolean.</p> * @@ -476,7 +471,6 @@ // Boolean to String methods //-------------------------------------------------------------------------- - /** * <p>Converts a Boolean to a String returning 'true', 'false', or <code>null</code>.</p> * @@ -525,7 +519,6 @@ // boolean to String methods //-------------------------------------------------------------------------- - /** * <p>Converts a boolean to a String returning 'true' or 'false'.</p> * @@ -568,4 +561,53 @@ return (bool ? trueString : falseString); } + // xor methods + // -------------------------------------------------------------------------- + /** + * Performs an xor on a set of booleans. + * + * @param array an array of <code>boolean<code>s + * @return <code>true</code> if the xor is successful. + * @throws NullArgumentException if <code>array</code> is <code>null</code> + * @throws IllegalArgumentException if <code>array</code> 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 <code>Boolean<code>s + * @return <code>true</code> if the xor is successful. + * @throws NullPointerException if <code>array</code> contains a <code>null</code> + * @throws NullArgumentException if <code>array</code> is <code>null</code> + * @throws IllegalArgumentException if <code>array</code> is empty. + */ + public static Boolean xor(Boolean[] array) { + return new Boolean(xor(ArrayUtils.toPrimitive(array))); + } + }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]