Class BooleanUtils
This class tries to handle null
input gracefully.
An exception will not be thrown for a null
input.
Each method documents its behavior in more detail.
#ThreadSafe#
- Since:
- 2.0
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
and
(boolean... array) Performs an 'and' operation on a set of booleans.static Boolean
Performs an 'and' operation on an array of Booleans.static Boolean[]
Returns a new array of possible values (like an enum would).static int
compare
(boolean x, boolean y) Compares twoboolean
values.static void
Performs the given action for each Booleanvalues()
.static boolean
static boolean
isNotFalse
(Boolean bool) static boolean
static boolean
static Boolean
Negates the specified boolean.static boolean
oneHot
(boolean... array) Performs a one-hot on an array of booleans.static Boolean
Performs a one-hot on an array of booleans.static boolean
or
(boolean... array) Performs an 'or' operation on a set of booleans.static Boolean
Performs an 'or' operation on an array of Booleans.static boolean[]
Returns a new array of possible values (like an enum would).static boolean
toBoolean
(int value) Converts an int to a boolean using the convention thatzero
isfalse
, everything else istrue
.static boolean
toBoolean
(int value, int trueValue, int falseValue) Converts an int to a boolean specifying the conversion values.static boolean
Converts a Boolean to a boolean handlingnull
by returningfalse
.static boolean
Converts an Integer to a boolean specifying the conversion values.static boolean
Converts a String to a boolean (optimized for performance).static boolean
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 handlingnull
.static Boolean
toBooleanObject
(int value) Converts an int to a Boolean using the convention thatzero
isfalse
, everything else istrue
.static Boolean
toBooleanObject
(int value, int trueValue, int falseValue, int nullValue) Converts an int to a Boolean specifying the conversion values.static Boolean
toBooleanObject
(Integer value) Converts an Integer to a Boolean using the convention thatzero
isfalse
, every other numeric value istrue
.static Boolean
toBooleanObject
(Integer value, Integer trueValue, Integer falseValue, Integer nullValue) Converts an Integer 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 thattrue
is1
andfalse
is0
.static int
toInteger
(boolean bool, int trueValue, int falseValue) Converts a boolean to an int specifying the conversion values.static int
Converts a Boolean to an int specifying the conversion values.static Integer
toIntegerObject
(boolean bool) Converts a boolean to an Integer using the convention thattrue
is1
andfalse
is0
.static Integer
toIntegerObject
(boolean bool, Integer trueValue, Integer falseValue) Converts a boolean to an Integer specifying the conversion values.static Integer
toIntegerObject
(Boolean bool) Converts a Boolean to an Integer using the convention thatzero
isfalse
.static Integer
toIntegerObject
(Boolean bool, Integer trueValue, Integer falseValue, Integer nullValue) Converts a Boolean to an Integer specifying the conversion values.static String
Converts a boolean to a String returning one of the input Strings.static String
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'
, ornull
.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'
, ornull
.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'
, ornull
.values()
Returns an unmodifiable list of Booleans[false, true]
.static boolean
xor
(boolean... array) Performs an xor on a set of booleans.static Boolean
Performs an xor on an array of Booleans.
-
Field Details
-
FALSE
The false String"false"
.- Since:
- 3.12.0
- See Also:
-
NO
The no String"no"
.- Since:
- 3.12.0
- See Also:
-
OFF
The off String"off"
.- Since:
- 3.12.0
- See Also:
-
ON
The on String"on"
.- Since:
- 3.12.0
- See Also:
-
TRUE
The true String"true"
.- Since:
- 3.12.0
- See Also:
-
YES
The yes String"yes"
.- Since:
- 3.12.0
- See Also:
-
-
Constructor Details
-
BooleanUtils
Deprecated.TODO Make private in 4.0.BooleanUtils
instances should NOT be constructed in standard programming. Instead, the class should be used asBooleanUtils.negate(true);
.This constructor is public to permit tools that require a JavaBean instance to operate.
-
-
Method Details
-
and
Performs an 'and' operation 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
- Parameters:
array
- an array ofboolean
s- Returns:
- the result of the logical 'and' operation. That is
false
if any of the parameters isfalse
andtrue
otherwise. - Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty.- Since:
- 3.0.1
-
and
Performs an 'and' operation 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 BooleanUtils.and(null, null) = Boolean.FALSE
Null array elements map to false, like
Boolean.parseBoolean(null)
and its callers return false.- Parameters:
array
- an array ofBoolean
s- Returns:
- the result of the logical 'and' operation. That is
false
if any of the parameters isfalse
andtrue
otherwise. - Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty.- Since:
- 3.0.1
-
booleanValues
Returns a new array of possible values (like an enum would).- Returns:
- a new array of possible values (like an enum would).
- Since:
- 3.12.0
-
compare
Compares twoboolean
values. This is the same functionality as provided in Java 7.- Parameters:
x
- the firstboolean
to comparey
- the secondboolean
to compare- Returns:
- the value
0
ifx == y
; a value less than0
if!x && y
; and a value greater than0
ifx && !y
- Since:
- 3.4
-
forEach
Performs the given action for each Booleanvalues()
.- Parameters:
action
- The action to be performed for each element- Since:
- 3.13.0
-
isFalse
Checks if aBoolean
value isfalse
, handlingnull
by returningfalse
.BooleanUtils.isFalse(Boolean.TRUE) = false BooleanUtils.isFalse(Boolean.FALSE) = true BooleanUtils.isFalse(null) = false
- Parameters:
bool
- the boolean to check, null returnsfalse
- Returns:
true
only if the input is non-null
andfalse
- Since:
- 2.1
-
isNotFalse
Checks if aBoolean
value is notfalse
, handlingnull
by returningtrue
.BooleanUtils.isNotFalse(Boolean.TRUE) = true BooleanUtils.isNotFalse(Boolean.FALSE) = false BooleanUtils.isNotFalse(null) = true
- Parameters:
bool
- the boolean to check, null returnstrue
- Returns:
true
if the input isnull
ortrue
- Since:
- 2.3
-
isNotTrue
Checks if aBoolean
value is nottrue
, handlingnull
by returningtrue
.BooleanUtils.isNotTrue(Boolean.TRUE) = false BooleanUtils.isNotTrue(Boolean.FALSE) = true BooleanUtils.isNotTrue(null) = true
- Parameters:
bool
- the boolean to check, null returnstrue
- Returns:
true
if the input is null or false- Since:
- 2.3
-
isTrue
Checks if aBoolean
value istrue
, handlingnull
by returningfalse
.BooleanUtils.isTrue(Boolean.TRUE) = true BooleanUtils.isTrue(Boolean.FALSE) = false BooleanUtils.isTrue(null) = false
- Parameters:
bool
- the boolean to check,null
returnsfalse
- Returns:
true
only if the input is non-null and true- Since:
- 2.1
-
negate
Negates the specified boolean.If
null
is passed in,null
will be returned.NOTE: This returns
null
and will throw aNullPointerException
if unboxed to a boolean.BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; BooleanUtils.negate(null) = null;
- Parameters:
bool
- the Boolean to negate, may be null- Returns:
- the negated Boolean, or
null
ifnull
input
-
oneHot
Performs a one-hot on an array of booleans.This implementation returns true if one, and only one, of the supplied values is true.
See also One-hot.
- Parameters:
array
- an array ofboolean
s- Returns:
- the result of the one-hot operations
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty.
-
oneHot
Performs a one-hot on an array of booleans.This implementation returns true if one, and only one, of the supplied values is true.
Null array elements map to false, like
Boolean.parseBoolean(null)
and its callers return false.See also One-hot.
- Parameters:
array
- an array ofboolean
s- Returns:
- the result of the one-hot operations
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty.
-
or
Performs an 'or' operation 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
- Parameters:
array
- an array ofboolean
s- Returns:
true
if any of the arguments istrue
, and it returnsfalse
otherwise.- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty.- Since:
- 3.0.1
-
or
Performs an 'or' operation 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 BooleanUtils.or(Boolean.TRUE, null) = Boolean.TRUE BooleanUtils.or(Boolean.FALSE, null) = Boolean.FALSE
Null array elements map to false, like
Boolean.parseBoolean(null)
and its callers return false.- Parameters:
array
- an array ofBoolean
s- Returns:
true
if any of the arguments istrue
, and it returnsfalse
otherwise.- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty.- Since:
- 3.0.1
-
primitiveValues
Returns a new array of possible values (like an enum would).- Returns:
- a new array of possible values (like an enum would).
- Since:
- 3.12.0
-
toBoolean
Converts a Boolean to a boolean handlingnull
by returningfalse
.BooleanUtils.toBoolean(Boolean.TRUE) = true BooleanUtils.toBoolean(Boolean.FALSE) = false BooleanUtils.toBoolean(null) = false
- Parameters:
bool
- the boolean to convert- Returns:
true
orfalse
,null
returnsfalse
-
toBoolean
Converts an int to a boolean using the convention thatzero
isfalse
, everything else istrue
.BooleanUtils.toBoolean(0) = false BooleanUtils.toBoolean(1) = true BooleanUtils.toBoolean(2) = true
- Parameters:
value
- the int to convert- Returns:
true
if non-zero,false
if zero
-
toBoolean
Converts an int to a boolean specifying the conversion values.If the
trueValue
andfalseValue
are the same number then the return value will betrue
in casevalue
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
- Parameters:
value
- theInteger
to converttrueValue
- the value to match fortrue
falseValue
- the value to match forfalse
- Returns:
true
orfalse
- Throws:
IllegalArgumentException
- ifvalue
does not match neithertrueValue
nofalseValue
-
toBoolean
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
- Parameters:
value
- the Integer to converttrueValue
- the value to match fortrue
, may benull
falseValue
- the value to match forfalse
, may benull
- Returns:
true
orfalse
- Throws:
IllegalArgumentException
- if no match
-
toBoolean
Converts a String to a boolean (optimized for performance).'true'
,'on'
,'y'
,'t'
or'yes'
(case insensitive) will returntrue
. Otherwise,false
is returned.This method performs 4 times faster (JDK1.4) than
Boolean.valueOf(String)
. However, this method accepts 'on' and 'yes', 't', 'y' 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 BooleanUtils.toBoolean("y") = true BooleanUtils.toBoolean("n") = false BooleanUtils.toBoolean("t") = true BooleanUtils.toBoolean("f") = false
- Parameters:
str
- the String to check- Returns:
- the boolean value of the string,
false
if no match or the String is null
-
toBoolean
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
- Parameters:
str
- the String to checktrueString
- the String to match fortrue
(case-sensitive), may benull
falseString
- the String to match forfalse
(case-sensitive), may benull
- Returns:
- the boolean value of the string
- Throws:
IllegalArgumentException
- if the String doesn't match
-
toBooleanDefaultIfNull
Converts a Boolean to a boolean handlingnull
.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
- Parameters:
bool
- the boolean object to convert to primitivevalueIfNull
- the boolean value to return if the parameterbool
isnull
- Returns:
true
orfalse
-
toBooleanObject
Converts an int to a Boolean using the convention thatzero
isfalse
, everything else istrue
.BooleanUtils.toBoolean(0) = Boolean.FALSE BooleanUtils.toBoolean(1) = Boolean.TRUE BooleanUtils.toBoolean(2) = Boolean.TRUE
- Parameters:
value
- the int to convert- Returns:
- Boolean.TRUE if non-zero, Boolean.FALSE if zero,
null
ifnull
-
toBooleanObject
Converts an int to a Boolean specifying the conversion values.NOTE: This method may return
null
and may throw aNullPointerException
if unboxed to aboolean
.The checks are done first for the
trueValue
, then for thefalseValue
and finally for thenullValue
.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
- Parameters:
value
- the Integer to converttrueValue
- the value to match fortrue
falseValue
- the value to match forfalse
nullValue
- the value to match fornull
- Returns:
- Boolean.TRUE, Boolean.FALSE, or
null
- Throws:
IllegalArgumentException
- if no match
-
toBooleanObject
Converts an Integer to a Boolean using the convention thatzero
isfalse
, every other numeric value istrue
.null
will be converted tonull
.NOTE: This method may return
null
and may throw aNullPointerException
if unboxed to aboolean
.BooleanUtils.toBooleanObject(Integer.valueOf(0)) = Boolean.FALSE BooleanUtils.toBooleanObject(Integer.valueOf(1)) = Boolean.TRUE BooleanUtils.toBooleanObject(Integer.valueOf(null)) = null
- Parameters:
value
- the Integer to convert- Returns:
- Boolean.TRUE if non-zero, Boolean.FALSE if zero,
null
ifnull
input
-
toBooleanObject
public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer falseValue, Integer nullValue) Converts an Integer to a Boolean specifying the conversion values.NOTE: This method may return
null
and may throw aNullPointerException
if unboxed to aboolean
.The checks are done first for the
trueValue
, then for thefalseValue
and finally for thenullValue
.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
- Parameters:
value
- the Integer to converttrueValue
- the value to match fortrue
, may benull
falseValue
- the value to match forfalse
, may benull
nullValue
- the value to match fornull
, may benull
- Returns:
- Boolean.TRUE, Boolean.FALSE, or
null
- Throws:
IllegalArgumentException
- if no match
-
toBooleanObject
Converts a String to a Boolean.'true'
,'on'
,'y'
,'t'
,'yes'
or'1'
(case insensitive) will returntrue
.'false'
,'off'
,'n'
,'f'
,'no'
or'0'
(case insensitive) will returnfalse
. Otherwise,null
is returned.NOTE: This method may return
null
and may throw aNullPointerException
if unboxed to aboolean
.// N.B. case is not significant BooleanUtils.toBooleanObject(null) = null BooleanUtils.toBooleanObject("true") = Boolean.TRUE BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] BooleanUtils.toBooleanObject("false") = Boolean.FALSE BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] BooleanUtils.toBooleanObject("No") = Boolean.FALSE BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] BooleanUtils.toBooleanObject("on") = Boolean.TRUE BooleanUtils.toBooleanObject("ON") = Boolean.TRUE BooleanUtils.toBooleanObject("off") = Boolean.FALSE BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE BooleanUtils.toBooleanObject("yes") = Boolean.TRUE BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] BooleanUtils.toBooleanObject("1") = Boolean.TRUE BooleanUtils.toBooleanObject("0") = Boolean.FALSE BooleanUtils.toBooleanObject("blue") = null BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) BooleanUtils.toBooleanObject("ono") = null // does not match on or no
- Parameters:
str
- the String to check; upper and lower case are treated as the same- Returns:
- the Boolean value of the string,
null
if no match ornull
input
-
toBooleanObject
public 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 method may return
null
and may throw aNullPointerException
if unboxed to aboolean
.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
- Parameters:
str
- the String to checktrueString
- the String to match fortrue
(case-sensitive), may benull
falseString
- the String to match forfalse
(case-sensitive), may benull
nullString
- the String to match fornull
(case-sensitive), may benull
- Returns:
- the Boolean value of the string,
null
if either the String matchesnullString
or ifnull
input andnullString
isnull
- Throws:
IllegalArgumentException
- if the String doesn't match
-
toInteger
Converts a boolean to an int using the convention thattrue
is1
andfalse
is0
.BooleanUtils.toInteger(true) = 1 BooleanUtils.toInteger(false) = 0
- Parameters:
bool
- the boolean to convert- Returns:
- one if
true
, zero iffalse
-
toInteger
Converts a boolean to an int specifying the conversion values.BooleanUtils.toInteger(true, 1, 0) = 1 BooleanUtils.toInteger(false, 1, 0) = 0
- Parameters:
bool
- the to converttrueValue
- the value to return iftrue
falseValue
- the value to return iffalse
- Returns:
- the appropriate value
-
toInteger
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
- Parameters:
bool
- the Boolean to converttrueValue
- the value to return iftrue
falseValue
- the value to return iffalse
nullValue
- the value to return ifnull
- Returns:
- the appropriate value
-
toIntegerObject
Converts a boolean to an Integer using the convention thattrue
is1
andfalse
is0
.BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) BooleanUtils.toIntegerObject(false) = Integer.valueOf(0)
- Parameters:
bool
- the boolean to convert- Returns:
- one if
true
, zero iffalse
-
toIntegerObject
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)
- Parameters:
bool
- the to converttrueValue
- the value to return iftrue
, may benull
falseValue
- the value to return iffalse
, may benull
- Returns:
- the appropriate value
-
toIntegerObject
Converts a Boolean to an Integer using the convention thatzero
isfalse
.null
will be converted tonull
.BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0)
- Parameters:
bool
- the Boolean to convert- Returns:
- one if Boolean.TRUE, zero if Boolean.FALSE,
null
ifnull
-
toIntegerObject
public 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)
- Parameters:
bool
- the Boolean to converttrueValue
- the value to return iftrue
, may benull
falseValue
- the value to return iffalse
, may benull
nullValue
- the value to return ifnull
, may benull
- Returns:
- the appropriate value
-
toString
Converts a boolean to a String returning one of the input Strings.BooleanUtils.toString(true, "true", "false") = "true" BooleanUtils.toString(false, "true", "false") = "false"
- Parameters:
bool
- the Boolean to checktrueString
- the String to return iftrue
, may benull
falseString
- the String to return iffalse
, may benull
- Returns:
- one of the two input Strings
-
toString
public 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;
- Parameters:
bool
- the Boolean to checktrueString
- the String to return iftrue
, may benull
falseString
- the String to return iffalse
, may benull
nullString
- the String to return ifnull
, may benull
- Returns:
- one of the three input Strings
-
toStringOnOff
Converts a boolean to a String returning'on'
or'off'
.BooleanUtils.toStringOnOff(true) = "on" BooleanUtils.toStringOnOff(false) = "off"
- Parameters:
bool
- the Boolean to check- Returns:
'on'
,'off'
, ornull
-
toStringOnOff
Converts a Boolean to a String returning'on'
,'off'
, ornull
.BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" BooleanUtils.toStringOnOff(null) = null;
- Parameters:
bool
- the Boolean to check- Returns:
'on'
,'off'
, ornull
-
toStringTrueFalse
Converts a boolean to a String returning'true'
or'false'
.BooleanUtils.toStringTrueFalse(true) = "true" BooleanUtils.toStringTrueFalse(false) = "false"
- Parameters:
bool
- the Boolean to check- Returns:
'true'
,'false'
, ornull
-
toStringTrueFalse
Converts a Boolean to a String returning'true'
,'false'
, ornull
.BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" BooleanUtils.toStringTrueFalse(null) = null;
- Parameters:
bool
- the Boolean to check- Returns:
'true'
,'false'
, ornull
-
toStringYesNo
Converts a boolean to a String returning'yes'
or'no'
.BooleanUtils.toStringYesNo(true) = "yes" BooleanUtils.toStringYesNo(false) = "no"
- Parameters:
bool
- the Boolean to check- Returns:
'yes'
,'no'
, ornull
-
toStringYesNo
Converts a Boolean to a String returning'yes'
,'no'
, ornull
.BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" BooleanUtils.toStringYesNo(null) = null;
- Parameters:
bool
- the Boolean to check- Returns:
'yes'
,'no'
, ornull
-
values
Returns an unmodifiable list of Booleans[false, true]
.- Returns:
- an unmodifiable list of Booleans
[false, true]
. - Since:
- 3.13.0
-
xor
Performs an xor on a set of booleans.This behaves like an XOR gate; it returns true if the number of true values is odd, and false if the number of true values is zero or even.
BooleanUtils.xor(true, true) = false BooleanUtils.xor(false, false) = false BooleanUtils.xor(true, false) = true BooleanUtils.xor(true, false, false) = true BooleanUtils.xor(true, true, true) = true BooleanUtils.xor(true, true, true, true) = false
- Parameters:
array
- an array ofboolean
s- Returns:
- true if the number of true values in the array is odd; otherwise returns false.
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty.
-
xor
Performs an xor on an array of Booleans.BooleanUtils.xor(Boolean.TRUE, Boolean.TRUE) = Boolean.FALSE BooleanUtils.xor(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE BooleanUtils.xor(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE BooleanUtils.xor(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE) = Boolean.TRUE BooleanUtils.xor(Boolean.FALSE, null) = Boolean.FALSE BooleanUtils.xor(Boolean.TRUE, null) = Boolean.TRUE
Null array elements map to false, like
Boolean.parseBoolean(null)
and its callers return false.- Parameters:
array
- an array ofBoolean
s- Returns:
- the result of the xor operations
- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty.
-