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
This commit is contained in:
Stephen Colebourne 2003-06-28 18:16:03 +00:00
parent 48cea59f38
commit 9cfd4409f9
2 changed files with 237 additions and 10 deletions

View File

@ -60,8 +60,9 @@
* boolean and Boolean objects.</p>
*
* @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
//--------------------------------------------------------------------------
/**
* <p>Negates the specified boolean.</p>
*
@ -95,7 +95,6 @@ public static Boolean negate(Boolean bool) {
// boolean Boolean methods
//--------------------------------------------------------------------------
/**
* <p>Boolean factory that avoids creating new Boolean objecs all the time.</p>
*
@ -137,7 +136,6 @@ public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull)
// Integer to Boolean methods
//--------------------------------------------------------------------------
/**
* <p>Convert an int to a boolean using the convention that zero is false.</p>
*
@ -271,7 +269,6 @@ public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer
// Boolean to Integer methods
//--------------------------------------------------------------------------
/**
* <p>Convert a boolean to an int using the convention that zero is false.</p>
*
@ -365,7 +362,6 @@ public static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer f
// String to Boolean methods
//--------------------------------------------------------------------------
/**
* <p>Converts a String to a Boolean.</p>
*
@ -425,7 +421,6 @@ public static Boolean toBooleanObject(String str, String trueString, String fals
// String to boolean methods
//--------------------------------------------------------------------------
/**
* <p>Converts a String to a boolean.</p>
*
@ -476,7 +471,6 @@ public static boolean toBoolean(String str, String trueString, String falseStrin
// Boolean to String methods
//--------------------------------------------------------------------------
/**
* <p>Converts a Boolean to a String returning 'true', 'false', or <code>null</code>.</p>
*
@ -525,7 +519,6 @@ public static String toString(Boolean bool, String trueString, String falseStrin
// boolean to String methods
//--------------------------------------------------------------------------
/**
* <p>Converts a boolean to a String returning 'true' or 'false'.</p>
*
@ -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 <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)));
}
}

View File

@ -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());
}
}