public class BooleanUtils extends Object
Operations on boolean primitives and Boolean objects.
This class tries to handle null input gracefully.
An exception will not be thrown for a null input.
Each method documents its behaviour in more detail.
#ThreadSafe#
| Constructor and Description |
|---|
BooleanUtils()
BooleanUtils instances should NOT be constructed in standard programming. |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
and(boolean... array)
Performs an and on a set of booleans.
|
static Boolean |
and(Boolean... array)
Performs an and on an array of Booleans.
|
static boolean |
isFalse(Boolean bool)
Checks if a
Boolean value is false,
handling null by returning false. |
static boolean |
isNotFalse(Boolean bool)
Checks if a
Boolean value is not false,
handling null by returning true. |
static boolean |
isNotTrue(Boolean bool)
Checks if a
Boolean value is not true,
handling null by returning true. |
static boolean |
isTrue(Boolean bool)
Checks if a
Boolean value is true,
handling null by returning false. |
static Boolean |
negate(Boolean bool)
Negates the specified boolean.
|
static boolean |
or(boolean... array)
Performs an or on a set of booleans.
|
static Boolean |
or(Boolean... array)
Performs an or on an array of Booleans.
|
static boolean |
toBoolean(Boolean bool)
Converts a Boolean to a boolean handling
null
by returning false. |
static boolean |
toBoolean(int value)
Converts an int to a boolean using the convention that
zero
is false. |
static boolean |
toBoolean(Integer value,
Integer trueValue,
Integer falseValue)
Converts an Integer to a boolean specifying the conversion values.
|
static boolean |
toBoolean(int value,
int trueValue,
int falseValue)
Converts an int to a boolean specifying the conversion values.
|
static boolean |
toBoolean(String str)
Converts a String to a boolean (optimised for performance).
|
static boolean |
toBoolean(String str,
String trueString,
String falseString)
Converts a String to a Boolean throwing an exception if no match found.
|
static boolean |
toBooleanDefaultIfNull(Boolean bool,
boolean valueIfNull)
Converts a Boolean to a boolean handling
null. |
static Boolean |
toBooleanObject(int value)
Converts an int to a Boolean using the convention that
zero
is false. |
static Boolean |
toBooleanObject(Integer value)
Converts an Integer to a Boolean using the convention that
zero
is false. |
static Boolean |
toBooleanObject(Integer value,
Integer trueValue,
Integer falseValue,
Integer nullValue)
Converts an Integer to a Boolean specifying the conversion values.
|
static Boolean |
toBooleanObject(int value,
int trueValue,
int falseValue,
int nullValue)
Converts an int to a Boolean specifying the conversion values.
|
static Boolean |
toBooleanObject(String str)
Converts a String to a Boolean.
|
static Boolean |
toBooleanObject(String str,
String trueString,
String falseString,
String nullString)
Converts a String to a Boolean throwing an exception if no match.
|
static int |
toInteger(boolean bool)
Converts a boolean to an int using the convention that
zero is false. |
static int |
toInteger(boolean bool,
int trueValue,
int falseValue)
Converts a boolean to an int specifying the conversion values.
|
static int |
toInteger(Boolean bool,
int trueValue,
int falseValue,
int nullValue)
Converts a Boolean to an int specifying the conversion values.
|
static Integer |
toIntegerObject(boolean bool)
Converts a boolean to an Integer using the convention that
zero is false. |
static Integer |
toIntegerObject(Boolean bool)
Converts a Boolean to a Integer using the convention that
zero is false. |
static Integer |
toIntegerObject(boolean bool,
Integer trueValue,
Integer falseValue)
Converts a boolean to an Integer specifying the conversion values.
|
static Integer |
toIntegerObject(Boolean bool,
Integer trueValue,
Integer falseValue,
Integer nullValue)
Converts a Boolean to an Integer specifying the conversion values.
|
static String |
toString(boolean bool,
String trueString,
String falseString)
Converts a boolean to a String returning one of the input Strings.
|
static String |
toString(Boolean bool,
String trueString,
String falseString,
String nullString)
Converts a Boolean to a String returning one of the input Strings.
|
static String |
toStringOnOff(boolean bool)
Converts a boolean to a String returning
'on'
or 'off'. |
static String |
toStringOnOff(Boolean bool)
Converts a Boolean to a String returning
'on',
'off', or null. |
static String |
toStringTrueFalse(boolean bool)
Converts a boolean to a String returning
'true'
or 'false'. |
static String |
toStringTrueFalse(Boolean bool)
Converts a Boolean to a String returning
'true',
'false', or null. |
static String |
toStringYesNo(boolean bool)
Converts a boolean to a String returning
'yes'
or 'no'. |
static String |
toStringYesNo(Boolean bool)
Converts a Boolean to a String returning
'yes',
'no', or null. |
static boolean |
xor(boolean... array)
Performs an xor on a set of booleans.
|
static Boolean |
xor(Boolean... array)
Performs an xor on an array of Booleans.
|
public BooleanUtils()
BooleanUtils instances should NOT be constructed in standard programming.
Instead, the class should be used as BooleanUtils.negate(true);.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static Boolean negate(Boolean bool)
Negates the specified boolean.
If null is passed in, null will be returned.
NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; BooleanUtils.negate(null) = null;
bool - the Boolean to negate, may be nullnull if null inputpublic static boolean isTrue(Boolean bool)
Checks if a Boolean value is true,
handling null by returning false.
BooleanUtils.isTrue(Boolean.TRUE) = true BooleanUtils.isTrue(Boolean.FALSE) = false BooleanUtils.isTrue(null) = false
bool - the boolean to check, null returns falsetrue only if the input is non-null and truepublic static boolean isNotTrue(Boolean bool)
Checks if a Boolean value is not true,
handling null by returning true.
BooleanUtils.isNotTrue(Boolean.TRUE) = false BooleanUtils.isNotTrue(Boolean.FALSE) = true BooleanUtils.isNotTrue(null) = true
bool - the boolean to check, null returns truetrue if the input is null or falsepublic static boolean isFalse(Boolean bool)
Checks if a Boolean value is false,
handling null by returning false.
BooleanUtils.isFalse(Boolean.TRUE) = false BooleanUtils.isFalse(Boolean.FALSE) = true BooleanUtils.isFalse(null) = false
bool - the boolean to check, null returns falsetrue only if the input is non-null and falsepublic static boolean isNotFalse(Boolean bool)
Checks if a Boolean value is not false,
handling null by returning true.
BooleanUtils.isNotFalse(Boolean.TRUE) = true BooleanUtils.isNotFalse(Boolean.FALSE) = false BooleanUtils.isNotFalse(null) = true
bool - the boolean to check, null returns truetrue if the input is null or truepublic static boolean toBoolean(Boolean bool)
Converts a Boolean to a boolean handling null
by returning false.
BooleanUtils.toBoolean(Boolean.TRUE) = true BooleanUtils.toBoolean(Boolean.FALSE) = false BooleanUtils.toBoolean(null) = false
bool - the boolean to converttrue or false, null returns falsepublic static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull)
Converts a Boolean to a boolean handling null.
BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false BooleanUtils.toBooleanDefaultIfNull(null, true) = true
bool - the boolean to convertvalueIfNull - the boolean value to return if nulltrue or falsepublic static boolean toBoolean(int value)
Converts an int to a boolean using the convention that zero
is false.
BooleanUtils.toBoolean(0) = false BooleanUtils.toBoolean(1) = true BooleanUtils.toBoolean(2) = true
value - the int to converttrue if non-zero, false
if zeropublic static Boolean toBooleanObject(int value)
Converts an int to a Boolean using the convention that zero
is false.
BooleanUtils.toBoolean(0) = Boolean.FALSE BooleanUtils.toBoolean(1) = Boolean.TRUE BooleanUtils.toBoolean(2) = Boolean.TRUE
value - the int to convertnull if nullpublic static Boolean toBooleanObject(Integer value)
Converts an Integer to a Boolean using the convention that zero
is false.
null will be converted to null.
NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE BooleanUtils.toBoolean(Integer.valueOf(null)) = null
value - the Integer to convertnull if null inputpublic static boolean toBoolean(int value,
int trueValue,
int falseValue)
Converts an int to a boolean specifying the conversion values.
BooleanUtils.toBoolean(0, 1, 0) = false BooleanUtils.toBoolean(1, 1, 0) = true BooleanUtils.toBoolean(2, 1, 2) = false BooleanUtils.toBoolean(2, 2, 0) = true
value - the Integer to converttrueValue - the value to match for truefalseValue - the value to match for falsetrue or falseIllegalArgumentException - if no matchpublic static boolean toBoolean(Integer value, Integer trueValue, Integer falseValue)
Converts an Integer to a boolean specifying the conversion values.
BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true
value - the Integer to converttrueValue - the value to match for true, may be nullfalseValue - the value to match for false, may be nulltrue or falseIllegalArgumentException - if no matchpublic static Boolean toBooleanObject(int value, int trueValue, int falseValue, int nullValue)
Converts an int to a Boolean specifying the conversion values.
NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE BooleanUtils.toBooleanObject(3, 1, 2, 3) = null
value - the Integer to converttrueValue - the value to match for truefalseValue - the value to match for falsenullValue - the value to to match for nullnullIllegalArgumentException - if no matchpublic static Boolean toBooleanObject(Integer value, Integer trueValue, Integer falseValue, Integer nullValue)
Converts an Integer to a Boolean specifying the conversion values.
NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null
value - the Integer to converttrueValue - the value to match for true, may be nullfalseValue - the value to match for false, may be nullnullValue - the value to to match for null, may be nullnullIllegalArgumentException - if no matchpublic static int toInteger(boolean bool)
Converts a boolean to an int using the convention that
zero is false.
BooleanUtils.toInteger(true) = 1 BooleanUtils.toInteger(false) = 0
bool - the boolean to converttrue, zero if falsepublic static Integer toIntegerObject(boolean bool)
Converts a boolean to an Integer using the convention that
zero is false.
BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) BooleanUtils.toIntegerObject(false) = Integer.valueOf(0)
bool - the boolean to converttrue, zero if falsepublic static Integer toIntegerObject(Boolean bool)
Converts a Boolean to a Integer using the convention that
zero is false.
null will be converted to null.
BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0)
bool - the Boolean to convertnull if nullpublic static int toInteger(boolean bool,
int trueValue,
int falseValue)
Converts a boolean to an int specifying the conversion values.
BooleanUtils.toInteger(true, 1, 0) = 1 BooleanUtils.toInteger(false, 1, 0) = 0
bool - the to converttrueValue - the value to return if truefalseValue - the value to return if falsepublic static int toInteger(Boolean bool, int trueValue, int falseValue, int nullValue)
Converts a Boolean to an int specifying the conversion values.
BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 BooleanUtils.toInteger(null, 1, 0, 2) = 2
bool - the Boolean to converttrueValue - the value to return if truefalseValue - the value to return if falsenullValue - the value to return if nullpublic static Integer toIntegerObject(boolean bool, Integer trueValue, Integer falseValue)
Converts a boolean to an Integer specifying the conversion values.
BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0)
bool - the to converttrueValue - the value to return if true, may be nullfalseValue - the value to return if false, may be nullpublic static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer falseValue, Integer nullValue)
Converts a Boolean to an Integer specifying the conversion values.
BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2)
bool - the Boolean to converttrueValue - the value to return if true, may be nullfalseValue - the value to return if false, may be nullnullValue - the value to return if null, may be nullpublic static Boolean toBooleanObject(String str)
Converts a String to a Boolean.
'true', 'on' or 'yes'
(case insensitive) will return true.
'false', 'off' or 'no'
(case insensitive) will return false.
Otherwise, null is returned.
NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
BooleanUtils.toBooleanObject(null) = null
BooleanUtils.toBooleanObject("true") = Boolean.TRUE
BooleanUtils.toBooleanObject("false") = Boolean.FALSE
BooleanUtils.toBooleanObject("on") = Boolean.TRUE
BooleanUtils.toBooleanObject("ON") = Boolean.TRUE
BooleanUtils.toBooleanObject("off") = Boolean.FALSE
BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE
BooleanUtils.toBooleanObject("blue") = null
str - the String to checknull if no match or null inputpublic static Boolean toBooleanObject(String str, String trueString, String falseString, String nullString)
Converts a String to a Boolean throwing an exception if no match.
NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE
BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE
BooleanUtils.toBooleanObject("null", "true", "false", "null") = null
str - the String to checktrueString - the String to match for true (case sensitive), may be nullfalseString - the String to match for false (case sensitive), may be nullnullString - the String to match for null (case sensitive), may be nullnull if either the String matches nullString
or if null input and nullString is nullIllegalArgumentException - if the String doesn't matchpublic static boolean toBoolean(String str)
Converts a String to a boolean (optimised for performance).
'true', 'on' or 'yes'
(case insensitive) will return true. Otherwise,
false is returned.
This method performs 4 times faster (JDK1.4) than
Boolean.valueOf(String). However, this method accepts
'on' and 'yes' as true values.
BooleanUtils.toBoolean(null) = false
BooleanUtils.toBoolean("true") = true
BooleanUtils.toBoolean("TRUE") = true
BooleanUtils.toBoolean("tRUe") = true
BooleanUtils.toBoolean("on") = true
BooleanUtils.toBoolean("yes") = true
BooleanUtils.toBoolean("false") = false
BooleanUtils.toBoolean("x gti") = false
str - the String to checkfalse if no match or the String is nullpublic static boolean toBoolean(String str, String trueString, String falseString)
Converts a String to a Boolean throwing an exception if no match found.
BooleanUtils.toBoolean("true", "true", "false") = true
BooleanUtils.toBoolean("false", "true", "false") = false
str - the String to checktrueString - the String to match for true (case sensitive), may be nullfalseString - the String to match for false (case sensitive), may be nullIllegalArgumentException - if the String doesn't matchpublic static String toStringTrueFalse(Boolean bool)
Converts a Boolean to a String returning 'true',
'false', or null.
BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" BooleanUtils.toStringTrueFalse(null) = null;
bool - the Boolean to check'true', 'false', or nullpublic static String toStringOnOff(Boolean bool)
Converts a Boolean to a String returning 'on',
'off', or null.
BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" BooleanUtils.toStringOnOff(null) = null;
bool - the Boolean to check'on', 'off', or nullpublic static String toStringYesNo(Boolean bool)
Converts a Boolean to a String returning 'yes',
'no', or null.
BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" BooleanUtils.toStringYesNo(null) = null;
bool - the Boolean to check'yes', 'no', or nullpublic static String toString(Boolean bool, String trueString, String falseString, String nullString)
Converts a Boolean to a String returning one of the input Strings.
BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" BooleanUtils.toString(null, "true", "false", null) = null;
bool - the Boolean to checktrueString - the String to return if true, may be nullfalseString - the String to return if false, may be nullnullString - the String to return if null, may be nullpublic static String toStringTrueFalse(boolean bool)
Converts a boolean to a String returning 'true'
or 'false'.
BooleanUtils.toStringTrueFalse(true) = "true" BooleanUtils.toStringTrueFalse(false) = "false"
bool - the Boolean to check'true', 'false', or nullpublic static String toStringOnOff(boolean bool)
Converts a boolean to a String returning 'on'
or 'off'.
BooleanUtils.toStringOnOff(true) = "on" BooleanUtils.toStringOnOff(false) = "off"
bool - the Boolean to check'on', 'off', or nullpublic static String toStringYesNo(boolean bool)
Converts a boolean to a String returning 'yes'
or 'no'.
BooleanUtils.toStringYesNo(true) = "yes" BooleanUtils.toStringYesNo(false) = "no"
bool - the Boolean to check'yes', 'no', or nullpublic static String toString(boolean bool, String trueString, String falseString)
Converts a boolean to a String returning one of the input Strings.
BooleanUtils.toString(true, "true", "false") = "true" BooleanUtils.toString(false, "true", "false") = "false"
bool - the Boolean to checktrueString - the String to return if true, may be nullfalseString - the String to return if false, may be nullpublic static boolean and(boolean... array)
Performs an and on a set of booleans.
BooleanUtils.and(true, true) = true BooleanUtils.and(false, false) = false BooleanUtils.and(true, false) = false BooleanUtils.and(true, true, false) = false BooleanUtils.and(true, true, true) = true
array - an array of booleanstrue if the and is successful.IllegalArgumentException - if array is nullIllegalArgumentException - if array is empty.public static Boolean and(Boolean... array)
Performs an and on an array of Booleans.
BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE
array - an array of Booleanstrue if the and is successful.IllegalArgumentException - if array is nullIllegalArgumentException - if array is empty.IllegalArgumentException - if array contains a nullpublic static boolean or(boolean... array)
Performs an or on a set of booleans.
BooleanUtils.or(true, true) = true BooleanUtils.or(false, false) = false BooleanUtils.or(true, false) = true BooleanUtils.or(true, true, false) = true BooleanUtils.or(true, true, true) = true BooleanUtils.or(false, false, false) = false
array - an array of booleanstrue if the or is successful.IllegalArgumentException - if array is nullIllegalArgumentException - if array is empty.public static Boolean or(Boolean... array)
Performs an or on an array of Booleans.
BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE
array - an array of Booleanstrue if the or is successful.IllegalArgumentException - if array is nullIllegalArgumentException - if array is empty.IllegalArgumentException - if array contains a nullpublic static boolean xor(boolean... array)
Performs an xor on a set of booleans.
BooleanUtils.xor(true, true) = false BooleanUtils.xor(false, false) = false BooleanUtils.xor(true, false) = true BooleanUtils.xor(true, true) = false BooleanUtils.xor(false, false) = false BooleanUtils.xor(true, false) = true
array - an array of booleanstrue if the xor is successful.IllegalArgumentException - if array is nullIllegalArgumentException - if array is empty.public static Boolean xor(Boolean... array)
Performs an xor on an array of Booleans.
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
array - an array of Booleanstrue if the xor is successful.IllegalArgumentException - if array is nullIllegalArgumentException - if array is empty.IllegalArgumentException - if array contains a nullCopyright © 2021 Internet2. All rights reserved.