Removing isExactlyOneTrue, since there is no consensus about this method. See also LANG-922.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1534738 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
dac619c84d
commit
01d67b1bda
|
@ -141,82 +141,6 @@ public class BooleanUtils {
|
|||
return !isFalse(bool);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if exactly one of the given booleans is true.</p>
|
||||
*
|
||||
* <pre>
|
||||
* BooleanUtils.isExactlyOneTrue(true, true) = false
|
||||
* BooleanUtils.isExactlyOneTrue(false, false) = false
|
||||
* BooleanUtils.isExactlyOneTrue(true, false) = true
|
||||
* BooleanUtils.isExactlyOneTrue(true, true) = false
|
||||
* BooleanUtils.isExactlyOneTrue(false, false) = false
|
||||
* BooleanUtils.isExactlyOneTrue(true, false) = true
|
||||
* </pre>
|
||||
*
|
||||
* @param array an array of {@code boolean}s
|
||||
* @return {@code true} if the array contains 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 isExactlyOneTrue(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, isExactlyOneTrue 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.isExactlyOneTrue(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE
|
||||
* BooleanUtils.isExactlyOneTrue(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE
|
||||
* BooleanUtils.isExactlyOneTrue(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE
|
||||
* </pre>
|
||||
*
|
||||
* @param array an array of {@code Boolean}s
|
||||
* @return {@code true} if the array contains 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 isExactlyOneTrue(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 isExactlyOneTrue(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,189 +53,6 @@ public class BooleanUtilsTest {
|
|||
assertSame(Boolean.FALSE, BooleanUtils.negate(Boolean.TRUE));
|
||||
}
|
||||
|
||||
// test isExactlyOneTrue
|
||||
// -----------------------------------------------------------------------
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsExactlyOneTrue_primitive_nullInput() {
|
||||
BooleanUtils.isExactlyOneTrue((boolean[]) null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsExactlyOneTrue_primitive_emptyInput() {
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsExactlyOneTrue_primitive_validInput_2items() {
|
||||
assertFalse(
|
||||
"true, true",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{true, true}));
|
||||
|
||||
assertFalse(
|
||||
"false, false",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{false, false}));
|
||||
|
||||
assertTrue(
|
||||
"true, false",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{true, false}));
|
||||
|
||||
assertTrue(
|
||||
"false, true",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{false, true}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsExactlyOneTrue_primitive_validInput_3items() {
|
||||
assertFalse(
|
||||
"false, false, false",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{false, false, false}));
|
||||
|
||||
assertTrue(
|
||||
"false, false, true",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{false, false, true}));
|
||||
|
||||
assertTrue(
|
||||
"false, true, false",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{false, true, false}));
|
||||
|
||||
assertFalse(
|
||||
"false, true, true",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{false, true, true}));
|
||||
|
||||
assertTrue(
|
||||
"true, false, false",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{true, false, false}));
|
||||
|
||||
assertFalse(
|
||||
"true, false, true",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{true, false, true}));
|
||||
|
||||
assertFalse(
|
||||
"true, true, false",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{true, true, false}));
|
||||
|
||||
assertFalse(
|
||||
"true, true, true",
|
||||
BooleanUtils.isExactlyOneTrue(new boolean[]{true, true, true}));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsExactlyOneTrue_object_nullInput() {
|
||||
BooleanUtils.isExactlyOneTrue((Boolean[]) null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsExactlyOneTrue_object_emptyInput() {
|
||||
BooleanUtils.isExactlyOneTrue(new Boolean[]{});
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIsExactlyOneTrue_object_nullElementInput() {
|
||||
BooleanUtils.isExactlyOneTrue(new Boolean[]{null});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsExactlyOneTrue_object_validInput_2items() {
|
||||
assertFalse(
|
||||
"false, false",
|
||||
BooleanUtils
|
||||
.isExactlyOneTrue(new Boolean[]{Boolean.FALSE, Boolean.FALSE})
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"false, true",
|
||||
BooleanUtils
|
||||
.isExactlyOneTrue(new Boolean[]{Boolean.FALSE, Boolean.TRUE})
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"true, false",
|
||||
BooleanUtils
|
||||
.isExactlyOneTrue(new Boolean[]{Boolean.TRUE, Boolean.FALSE})
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"true, true",
|
||||
BooleanUtils
|
||||
.isExactlyOneTrue(new Boolean[]{Boolean.TRUE, Boolean.TRUE})
|
||||
.booleanValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsExactlyOneTrue_object_validInput_3items() {
|
||||
assertFalse(
|
||||
"false, false, false",
|
||||
BooleanUtils.isExactlyOneTrue(
|
||||
new Boolean[]{
|
||||
Boolean.FALSE,
|
||||
Boolean.FALSE,
|
||||
Boolean.FALSE})
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"false, false, true",
|
||||
BooleanUtils
|
||||
.isExactlyOneTrue(
|
||||
new Boolean[]{
|
||||
Boolean.FALSE,
|
||||
Boolean.FALSE,
|
||||
Boolean.TRUE})
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"false, true, false",
|
||||
BooleanUtils
|
||||
.isExactlyOneTrue(
|
||||
new Boolean[]{
|
||||
Boolean.FALSE,
|
||||
Boolean.TRUE,
|
||||
Boolean.FALSE})
|
||||
.booleanValue());
|
||||
|
||||
assertTrue(
|
||||
"true, false, false",
|
||||
BooleanUtils
|
||||
.isExactlyOneTrue(
|
||||
new Boolean[]{
|
||||
Boolean.TRUE,
|
||||
Boolean.FALSE,
|
||||
Boolean.FALSE})
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"true, false, true",
|
||||
BooleanUtils.isExactlyOneTrue(
|
||||
new Boolean[]{
|
||||
Boolean.TRUE,
|
||||
Boolean.FALSE,
|
||||
Boolean.TRUE})
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"true, true, false",
|
||||
BooleanUtils.isExactlyOneTrue(
|
||||
new Boolean[]{
|
||||
Boolean.TRUE,
|
||||
Boolean.TRUE,
|
||||
Boolean.FALSE})
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"false, true, true",
|
||||
BooleanUtils.isExactlyOneTrue(
|
||||
new Boolean[]{
|
||||
Boolean.FALSE,
|
||||
Boolean.TRUE,
|
||||
Boolean.TRUE})
|
||||
.booleanValue());
|
||||
|
||||
assertFalse(
|
||||
"true, true, true",
|
||||
BooleanUtils
|
||||
.isExactlyOneTrue(new Boolean[]{Boolean.TRUE, Boolean.TRUE, Boolean.TRUE})
|
||||
.booleanValue());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void test_isTrue_Boolean() {
|
||||
|
|
Loading…
Reference in New Issue