From 936dd601b777454e45df38fcc117e623b21d3e5c Mon Sep 17 00:00:00 2001
From: Peter Verhas If {@code null} is passed in, {@code null} will be returned. NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean. NOTE: This returns {@code null} and will throw a {@code NullPointerException}
+ * if unboxed to a boolean. Converts a Boolean to a boolean handling {@code null}. Converts an int to a boolean using the convention that {@code zero}
- * is {@code false}.
* BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE;
@@ -78,7 +79,7 @@ public class BooleanUtils {
* BooleanUtils.isTrue(null) = false
*
*
- * @param bool the boolean to check, null returns {@code false}
+ * @param bool the boolean to check, {@code null} returns {@code false}
* @return {@code true} only if the input is non-null and true
* @since 2.1
*/
@@ -115,7 +116,7 @@ public class BooleanUtils {
*
*
* @param bool the boolean to check, null returns {@code false}
- * @return {@code true} only if the input is non-null and false
+ * @return {@code true} only if the input is non-{@code null} and {@code false}
* @since 2.1
*/
public static boolean isFalse(final Boolean bool) {
@@ -133,7 +134,7 @@ public class BooleanUtils {
*
*
* @param bool the boolean to check, null returns {@code true}
- * @return {@code true} if the input is null or true
+ * @return {@code true} if the input is {@code null} or {@code true}
* @since 2.3
*/
public static boolean isNotFalse(final Boolean bool) {
@@ -162,13 +163,16 @@ public class BooleanUtils {
*
- * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true
- * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false
- * BooleanUtils.toBooleanDefaultIfNull(null, true) = true
+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true
+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, true) = true
+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false
+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, false) = false
+ * BooleanUtils.toBooleanDefaultIfNull(null, true) = true
+ * BooleanUtils.toBooleanDefaultIfNull(null, false) = false
*
*
- * @param bool the boolean to convert
- * @param valueIfNull the boolean value to return if {@code null}
+ * @param bool the boolean object to convert to primitive
+ * @param valueIfNull the boolean value to return if the parameter {@code bool} is {@code null}
* @return {@code true} or {@code false}
*/
public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) {
@@ -182,7 +186,7 @@ public class BooleanUtils {
//-----------------------------------------------------------------------
/**
*
* BooleanUtils.toBoolean(0) = false @@ -200,7 +204,7 @@ public class BooleanUtils { /** *Converts an int to a Boolean using the convention that {@code zero} - * is {@code false}.
+ * is {@code false}, everything else is {@code true}. * ** BooleanUtils.toBoolean(0) = Boolean.FALSE @@ -218,11 +222,12 @@ public class BooleanUtils { /** *Converts an Integer to a Boolean using the convention that {@code zero} - * is {@code false}.
+ * is {@code false}, every other numeric value is {@code true}. * *{@code null} will be converted to {@code null}.
* - *NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean.
+ *NOTE: This method may return {@code null} and may throw a {@code NullPointerException} + * if unboxed to a {@code boolean}.
* ** BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE @@ -244,18 +249,23 @@ public class BooleanUtils { /** *Converts an int to a boolean specifying the conversion values.
* + *If the {@code trueValue} and {@code falseValue} are the same number then + * the return value will be {@code true} in case {@code value} matches it.
+ * ** BooleanUtils.toBoolean(0, 1, 0) = false * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(1, 1, 1) = true * BooleanUtils.toBoolean(2, 1, 2) = false * BooleanUtils.toBoolean(2, 2, 0) = true ** - * @param value the Integer to convert + * @param value the {@code Integer} to convert * @param trueValue the value to match for {@code true} * @param falseValue the value to match for {@code false} * @return {@code true} or {@code false} - * @throws IllegalArgumentException if no match + * @throws IllegalArgumentException if {@code value} does not match neither + * {@code trueValue} no {@code falseValue} */ public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { if (value == trueValue) { @@ -264,7 +274,6 @@ public class BooleanUtils { if (value == falseValue) { return false; } - // no match throw new IllegalArgumentException("The Integer did not match either specified value"); } @@ -298,18 +307,24 @@ public class BooleanUtils { } else if (value.equals(falseValue)) { return false; } - // no match throw new IllegalArgumentException("The Integer did not match either specified value"); } /** *Converts an int to a Boolean specifying the conversion values.
* - *NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean.
+ *NOTE: This method may return {@code null} and may throw a {@code NullPointerException} + * if unboxed to a {@code boolean}.
+ * + *The checks are done first for the {@code trueValue}, then for the {@code falseValue} and + * finally for the {@code nullValue}.
* ** BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(0, 0, 0, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(0, 0, 0, 0) = Boolean.TRUE * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(2, 1, 2, 2) = Boolean.FALSE * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null ** @@ -330,18 +345,24 @@ public class BooleanUtils { if (value == nullValue) { return null; } - // no match throw new IllegalArgumentException("The Integer did not match any specified value"); } /** *Converts an Integer to a Boolean specifying the conversion values.
* - *NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean.
+ *NOTE: This method may return {@code null} and may throw a {@code NullPointerException} + * if unboxed to a {@code boolean}.
* + *The checks are done first for the {@code trueValue}, then for the {@code falseValue} and + * finally for the {@code nullValue}.
+ ** ** BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0)) = Boolean.TRUE * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(2)) = Boolean.FALSE * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null ** @@ -370,7 +391,6 @@ public class BooleanUtils { } else if (value.equals(nullValue)) { return null; } - // no match throw new IllegalArgumentException("The Integer did not match any specified value"); } @@ -378,7 +398,7 @@ public class BooleanUtils { //----------------------------------------------------------------------- /** *Converts a boolean to an int using the convention that - * {@code zero} is {@code false}.
+ * {@code true} is {@code 1} and {@code false} is {@code 0}. * ** BooleanUtils.toInteger(true) = 1 @@ -394,7 +414,7 @@ public class BooleanUtils { /** *Converts a boolean to an Integer using the convention that - * {@code zero} is {@code false}.
+ * {@code true} is {@code 1} and {@code false} is {@code 0}. * ** BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) @@ -518,7 +538,8 @@ public class BooleanUtils { * (case insensitive) will return {@code false}. * Otherwise, {@code null} is returned. * - ** * @param array an array of {@code Boolean}s - * @return {@code true} if the xor is successful. + * @return the result of the xor operations * @throws IllegalArgumentException if {@code array} is {@code null} * @throws IllegalArgumentException if {@code array} is empty. * @throws IllegalArgumentException if {@code array} contains a {@code null}NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean.
+ *NOTE: This method may return {@code null} and may throw a {@code NullPointerException} + * if unboxed to a {@code boolean}.
* ** // N.B. case is not significant @@ -636,12 +657,19 @@ public class BooleanUtils { /** ** * @param array an array of {@code boolean}s - * @return {@code true} if the xor is successful. + * @return the result of the xor operations * @throws IllegalArgumentException if {@code array} is {@code null} * @throws IllegalArgumentException if {@code array} is empty. */ public static boolean xor(final boolean... array) { - // Validates input if (array == null) { throw new IllegalArgumentException("The Array must not be null"); } @@ -1058,10 +1085,11 @@ public class BooleanUtils { * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + * BooleanUtils.xor(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE) = Boolean.TRUE *Converts a String to a Boolean throwing an exception if no match.
* - *NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean.
+ *NOTE: This method may return {@code null} and may throw a {@code NullPointerException} + * if unboxed to a {@code boolean}.
* *- * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE - * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE - * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject(null, null, "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject(null, null, null, "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject(null, null, null, null) = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("false", "true", "false", "false") = Boolean.FALSE + * BooleanUtils.toBooleanObject(null, "true", null, "false") = Boolean.FALSE + * BooleanUtils.toBooleanObject(null, "true", null, null) = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null ** * @param str the String to check @@ -735,7 +763,6 @@ public class BooleanUtils { return false; } } - // no match throw new IllegalArgumentException("The String did not match either specified value"); } @@ -884,7 +911,7 @@ public class BooleanUtils { // logical operations // ---------------------------------------------------------------------- /** - *Performs an and on a set of booleans.
+ *Performs an 'and' operation on a set of booleans.
* ** BooleanUtils.and(true, true) = true @@ -895,7 +922,8 @@ public class BooleanUtils { ** * @param array an array of {@code boolean}s - * @return {@code true} if the and is successful. + * @return the result of the logical 'and' operation. That is {@code false} + * if any of the parameters is {@code false} and {@code true} otherwise. * @throws IllegalArgumentException if {@code array} is {@code null} * @throws IllegalArgumentException if {@code array} is empty. * @since 3.0.1 @@ -917,7 +945,7 @@ public class BooleanUtils { } /** - *Performs an and on an array of Booleans.
+ *Performs an 'and' operation on an array of Booleans.
* ** BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE @@ -929,7 +957,8 @@ public class BooleanUtils { ** * @param array an array of {@code Boolean}s - * @return {@code true} if the and is successful. + * @return the result of the logical 'and' operation. That is {@code false} + * if any of the parameters is {@code false} and {@code true} otherwise. * @throws IllegalArgumentException if {@code array} is {@code null} * @throws IllegalArgumentException if {@code array} is empty. * @throws IllegalArgumentException if {@code array} contains a {@code null} @@ -951,7 +980,7 @@ public class BooleanUtils { } /** - *Performs an or on a set of booleans.
+ *Performs an 'or' operation on a set of booleans.
* ** BooleanUtils.or(true, true) = true @@ -963,13 +992,12 @@ public class BooleanUtils { ** * @param array an array of {@code boolean}s - * @return {@code true} if the or is successful. + * @return {@code true} if any of the arguments is {@code true}, and it returns {@code false} otherwise. * @throws IllegalArgumentException if {@code array} is {@code null} * @throws IllegalArgumentException if {@code array} is empty. * @since 3.0.1 */ public static boolean or(final boolean... array) { - // Validates input if (array == null) { throw new IllegalArgumentException("The Array must not be null"); } @@ -985,7 +1013,7 @@ public class BooleanUtils { } /** - *Performs an or on an array of Booleans.
+ *Performs an 'or' operation on an array of Booleans.
* ** BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE @@ -998,7 +1026,7 @@ public class BooleanUtils { ** * @param array an array of {@code Boolean}s - * @return {@code true} if the or is successful. + * @return {@code true} if any of the arguments is {@code true}, and it returns {@code false} otherwise. * @throws IllegalArgumentException if {@code array} is {@code null} * @throws IllegalArgumentException if {@code array} is empty. * @throws IllegalArgumentException if {@code array} contains a {@code null} @@ -1029,12 +1057,11 @@ public class BooleanUtils { *