Author: britter Date: Tue Oct 22 19:23:21 2013 New Revision: 1534738 URL: http://svn.apache.org/r1534738 Log: Removing isExactlyOneTrue, since there is no consensus about this method. See also LANG-922.
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java?rev=1534738&r1=1534737&r2=1534738&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java Tue Oct 22 19:23:21 2013 @@ -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} Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java?rev=1534738&r1=1534737&r2=1534738&view=diff ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java (original) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java Tue Oct 22 19:23:21 2013 @@ -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() {