LANG-922 - Add isOneTrue(booleans...) to BooleanUtils to preserve old behavior of BooleanUtils.xor(booleans...). Added isOneTrue for primitives and wrappers, isOneFalse still missing.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1532491 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
aadbea734d
commit
0de9354f50
|
@ -141,6 +141,82 @@ public class BooleanUtils {
|
|||
return !isFalse(bool);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if exactly one of the given booleans is true.</p>
|
||||
*
|
||||
* <pre>
|
||||
* BooleanUtils.isOneTrue(true, true) = false
|
||||
* BooleanUtils.isOneTrue(false, false) = false
|
||||
* BooleanUtils.isOneTrue(true, false) = true
|
||||
* BooleanUtils.isOneTrue(true, true) = false
|
||||
* BooleanUtils.isOneTrue(false, false) = false
|
||||
* BooleanUtils.isOneTrue(true, false) = true
|
||||
* </pre>
|
||||
*
|
||||
* @param array an array of {@code boolean}s
|
||||
* @return {@code true} if the array containes the value true only once.
|
||||
* @throws IllegalArgumentException if {@code array} is {@code null}
|
||||
* @throws IllegalArgumentException if {@code array} is empty.
|
||||
* @since 3.2
|
||||
*/
|
||||
public static boolean isOneTrue(final 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");
|
||||
}
|
||||
|
||||
// Loops through array, comparing each item
|
||||
int trueCount = 0;
|
||||
for (final boolean element : array) {
|
||||
// If item is true, and trueCount is < 1, increments count
|
||||
// Else, isOneTrue fails
|
||||
if (element) {
|
||||
if (trueCount < 1) {
|
||||
trueCount++;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if there was exactly 1 true item
|
||||
return trueCount == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if exactly one of the given Booleans is true.</p>
|
||||
*
|
||||
* <pre>
|
||||
* BooleanUtils.isOneTrue(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE
|
||||
* BooleanUtils.isOneTrue(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE
|
||||
* BooleanUtils.isOneTrue(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE
|
||||
* </pre>
|
||||
*
|
||||
* @param array an array of {@code Boolean}s
|
||||
* @return {@code true} if the array containes a Boolean with value true only once.
|
||||
* @throws IllegalArgumentException if {@code array} is {@code null}
|
||||
* @throws IllegalArgumentException if {@code array} is empty.
|
||||
* @throws IllegalArgumentException if {@code array} contains a {@code null}
|
||||
* @since 3.2
|
||||
*/
|
||||
public static Boolean isOneTrue(final 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 {
|
||||
final boolean[] primitive = ArrayUtils.toPrimitive(array);
|
||||
return isOneTrue(primitive) ? Boolean.TRUE : Boolean.FALSE;
|
||||
} catch (final NullPointerException ex) {
|
||||
throw new IllegalArgumentException("The array must not contain any null elements");
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Converts a Boolean to a boolean handling {@code null}
|
||||
|
|
|
@ -53,6 +53,189 @@ public class BooleanUtilsTest {
|
|||
assertSame(Boolean.FALSE, BooleanUtils.negate(Boolean.TRUE));
|
||||
}
|
||||
|
||||
// test isOneTrue
|
||||
// -----------------------------------------------------------------------
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsOneTrue_primitive_nullInput() {
|
||||
BooleanUtils.isOneTrue((boolean[]) null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsOneTrue_primitive_emptyInput() {
|
||||
BooleanUtils.isOneTrue(new boolean[] {});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsOneTrue_primitive_validInput_2items() {
|
||||
assertFalse(
|
||||
"true, true",
|
||||
BooleanUtils.isOneTrue(new boolean[] { true, true }));
|
||||
|
||||
assertFalse(
|
||||
"false, false",
|
||||
BooleanUtils.isOneTrue(new boolean[] { false, false }));
|
||||
|
||||
assertTrue(
|
||||
"true, false",
|
||||
BooleanUtils.isOneTrue(new boolean[] { true, false }));
|
||||
|
||||
assertTrue(
|
||||
"false, true",
|
||||
BooleanUtils.isOneTrue(new boolean[] { false, true }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsOneTrue_primitive_validInput_3items() {
|
||||
assertFalse(
|
||||
"false, false, false",
|
||||
BooleanUtils.isOneTrue(new boolean[] { false, false, false }));
|
||||
|
||||
assertTrue(
|
||||
"false, false, true",
|
||||
BooleanUtils.isOneTrue(new boolean[] { false, false, true }));
|
||||
|
||||
assertTrue(
|
||||
"false, true, false",
|
||||
BooleanUtils.isOneTrue(new boolean[] { false, true, false }));
|
||||
|
||||
assertFalse(
|
||||
"false, true, true",
|
||||
BooleanUtils.isOneTrue(new boolean[] { false, true, true }));
|
||||
|
||||
assertTrue(
|
||||
"true, false, false",
|
||||
BooleanUtils.isOneTrue(new boolean[] { true, false, false }));
|
||||
|
||||
assertFalse(
|
||||
"true, false, true",
|
||||
BooleanUtils.isOneTrue(new boolean[] { true, false, true }));
|
||||
|
||||
assertFalse(
|
||||
"true, true, false",
|
||||
BooleanUtils.isOneTrue(new boolean[] { true, true, false }));
|
||||
|
||||
assertFalse(
|
||||
"true, true, true",
|
||||
BooleanUtils.isOneTrue(new boolean[] { true, true, true }));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsOneTrue_object_nullInput() {
|
||||
BooleanUtils.isOneTrue((Boolean[]) null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsOneTrue_object_emptyInput() {
|
||||
BooleanUtils.isOneTrue(new Boolean[] {});
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsOneTrue_object_nullElementInput() {
|
||||
BooleanUtils.isOneTrue(new Boolean[] {null});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsOneTrue_object_validInput_2items() {
|
||||
assertFalse(
|
||||
"false, false",
|
||||
BooleanUtils
|
||||
.isOneTrue(new Boolean[] { Boolean.FALSE, Boolean.FALSE })
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"false, true",
|
||||
BooleanUtils
|
||||
.isOneTrue(new Boolean[] { Boolean.FALSE, Boolean.TRUE })
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"true, false",
|
||||
BooleanUtils
|
||||
.isOneTrue(new Boolean[] { Boolean.TRUE, Boolean.FALSE })
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"true, true",
|
||||
BooleanUtils
|
||||
.isOneTrue(new Boolean[] { Boolean.TRUE, Boolean.TRUE })
|
||||
.booleanValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsOneTrue_object_validInput_3items() {
|
||||
assertFalse(
|
||||
"false, false, false",
|
||||
BooleanUtils.isOneTrue(
|
||||
new Boolean[] {
|
||||
Boolean.FALSE,
|
||||
Boolean.FALSE,
|
||||
Boolean.FALSE })
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"false, false, true",
|
||||
BooleanUtils
|
||||
.isOneTrue(
|
||||
new Boolean[] {
|
||||
Boolean.FALSE,
|
||||
Boolean.FALSE,
|
||||
Boolean.TRUE })
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"false, true, false",
|
||||
BooleanUtils
|
||||
.isOneTrue(
|
||||
new Boolean[] {
|
||||
Boolean.FALSE,
|
||||
Boolean.TRUE,
|
||||
Boolean.FALSE })
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"true, false, false",
|
||||
BooleanUtils
|
||||
.isOneTrue(
|
||||
new Boolean[] {
|
||||
Boolean.TRUE,
|
||||
Boolean.FALSE,
|
||||
Boolean.FALSE })
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"true, false, true",
|
||||
BooleanUtils.isOneTrue(
|
||||
new Boolean[] {
|
||||
Boolean.TRUE,
|
||||
Boolean.FALSE,
|
||||
Boolean.TRUE })
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"true, true, false",
|
||||
BooleanUtils.isOneTrue(
|
||||
new Boolean[] {
|
||||
Boolean.TRUE,
|
||||
Boolean.TRUE,
|
||||
Boolean.FALSE })
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"false, true, true",
|
||||
BooleanUtils.isOneTrue(
|
||||
new Boolean[] {
|
||||
Boolean.FALSE,
|
||||
Boolean.TRUE,
|
||||
Boolean.TRUE })
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"true, true, true",
|
||||
BooleanUtils
|
||||
.isOneTrue(new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE })
|
||||
.booleanValue());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void test_isTrue_Boolean() {
|
||||
|
|
Loading…
Reference in New Issue