Top Description Fields Constructors Methods
org.apache.commons.lang3

public Class BooleanUtils

extends Object
Class Inheritance
Imports
java.util.Arrays, .Collections, .List, java.util.function.Consumer, org.apache.commons.lang3.math.NumberUtils

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 behavior in more detail.

#ThreadSafe#

Since
2.0

Field Summary

Modifier and TypeField and Description
private static final List<Boolean>
public static final String
FALSE

The false String "false".

public static final String
NO

The no String "no".

public static final String
OFF

The off String "off".

public static final String
ON

The on String "on".

public static final String
TRUE

The true String "true".

public static final String
YES

The yes String "yes".

Constructor Summary

AccessConstructor and Description
public
BooleanUtils()
Deprecated TODO Make private in 4.0.

BooleanUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

the result of the logical 'and' operation. That is false if any of the parameters is false and true otherwise.
and
(final boolean...
an array of booleans
array
)

Performs an 'and' operation on a set of booleans.

public static Boolean

Returns:

the result of the logical 'and' operation. That is false if any of the parameters is false and true otherwise.
and
(final Boolean...
an array of Booleans
array
)

Performs an 'and' operation on an array of Booleans.

public static Boolean[]

Returns:

a new array of possible values (like an enum would).
booleanValues
()

Returns a new array of possible values (like an enum would).

public static int

Returns:

the value 0 if x == y; a value less than 0 if !x && y; and a value greater than 0 if x && !y
compare
(final boolean
the first boolean to compare
x
,
final boolean
the second boolean to compare
y
)

Compares two boolean values.

public static void
forEach(final Consumer<Boolean>
The action to be performed for each element
action
)

Performs the given action for each Boolean BooleanUtils#values().

public static boolean

Returns:

true only if the input is non-null and false
isFalse
(final Boolean
the boolean to check, null returns false
bool
)

Checks if a Boolean value is false, handling null by returning false.

public static boolean

Returns:

true if the input is null or true
isNotFalse
(final Boolean
the boolean to check, null returns true
bool
)

Checks if a Boolean value is not false, handling null by returning true.

public static boolean

Returns:

true if the input is null or false
isNotTrue
(final Boolean
the boolean to check, null returns true
bool
)

Checks if a Boolean value is not true, handling null by returning true.

public static boolean

Returns:

true only if the input is non-null and true
isTrue
(final Boolean
the boolean to check, null returns false
bool
)

Checks if a Boolean value is true, handling null by returning false.

public static Boolean

Returns:

the negated Boolean, or null if null input
negate
(final Boolean
the Boolean to negate, may be null
bool
)

Negates the specified boolean.

public static boolean

Returns:

the result of the one-hot operations
oneHot
(final boolean...
an array of booleans
array
)

Performs a one-hot on an array of booleans.

public static Boolean

Returns:

the result of the one-hot operations
oneHot
(final Boolean...
an array of booleans
array
)

Performs a one-hot on an array of booleans.

public static boolean

Returns:

true if any of the arguments is true, and it returns false otherwise.
or
(final boolean...
an array of booleans
array
)

Performs an 'or' operation on a set of booleans.

public static Boolean

Returns:

true if any of the arguments is true, and it returns false otherwise.
or
(final Boolean...
an array of Booleans
array
)

Performs an 'or' operation on an array of Booleans.

public static boolean[]

Returns:

a new array of possible values (like an enum would).
primitiveValues
()

Returns a new array of possible values (like an enum would).

public static boolean

Returns:

true or false, null returns false
toBoolean
(final Boolean
the boolean to convert
bool
)

Converts a Boolean to a boolean handling null by returning false.

public static boolean

Returns:

true if non-zero, false if zero
toBoolean
(final int
the int to convert
value
)

Converts an int to a boolean using the convention that zero is false, everything else is true.

public static boolean

Returns:

true or false
toBoolean
(final int
the Integer to convert
value
,
final int
the value to match for true
trueValue
,
final int
the value to match for false
falseValue
)

Converts an int to a boolean specifying the conversion values.

public static boolean

Returns:

true or false
toBoolean
(final Integer
the Integer to convert
value
,
final Integer
the value to match for true, may be null
trueValue
,
final Integer
the value to match for false, may be null
falseValue
)

Converts an Integer to a boolean specifying the conversion values.

public static boolean

Returns:

the boolean value of the string, false if no match or the String is null
toBoolean
(final String
the String to check
str
)

Converts a String to a boolean (optimized for performance).

public static boolean

Returns:

the boolean value of the string
toBoolean
(final String
the String to check
str
,
final String
the String to match for true (case-sensitive), may be null
trueString
,
final String
the String to match for false (case-sensitive), may be null
falseString
)

Converts a String to a Boolean throwing an exception if no match found.

public static boolean

Returns:

true or false
toBooleanDefaultIfNull
(final Boolean
the boolean object to convert to primitive
bool
,
final boolean
the boolean value to return if the parameter bool is null
valueIfNull
)

Converts a Boolean to a boolean handling null.

public static Boolean

Returns:

Boolean.TRUE if non-zero, Boolean.FALSE if zero, null if null
toBooleanObject
(final int
the int to convert
value
)

Converts an int to a Boolean using the convention that zero is false, everything else is true.

public static Boolean

Returns:

Boolean.TRUE, Boolean.FALSE, or null
toBooleanObject
(final int
the Integer to convert
value
,
final int
the value to match for true
trueValue
,
final int
the value to match for false
falseValue
,
final int
the value to match for null
nullValue
)

Converts an int to a Boolean specifying the conversion values.

public static Boolean

Returns:

Boolean.TRUE if non-zero, Boolean.FALSE if zero, null if null input
toBooleanObject
(final Integer
the Integer to convert
value
)

Converts an Integer to a Boolean using the convention that zero is false, every other numeric value is true.

public static Boolean

Returns:

Boolean.TRUE, Boolean.FALSE, or null
toBooleanObject
(final Integer
the Integer to convert
value
,
final Integer
the value to match for true, may be null
trueValue
,
final Integer
the value to match for false, may be null
falseValue
,
final Integer
the value to match for null, may be null
nullValue
)

Converts an Integer to a Boolean specifying the conversion values.

public static Boolean

Returns:

the Boolean value of the string, null if no match or null input
toBooleanObject
(final String
the String to check; upper and lower case are treated as the same
str
)

Converts a String to a Boolean.

public static Boolean

Returns:

the Boolean value of the string, null if either the String matches nullString or if null input and nullString is null
toBooleanObject
(final String
the String to check
str
,
final String
the String to match for true (case-sensitive), may be null
trueString
,
final String
the String to match for false (case-sensitive), may be null
falseString
,
final String
the String to match for null (case-sensitive), may be null
nullString
)

Converts a String to a Boolean throwing an exception if no match.

public static int

Returns:

one if true, zero if false
toInteger
(final boolean
the boolean to convert
bool
)

Converts a boolean to an int using the convention that true is 1 and false is 0.

public static int

Returns:

the appropriate value
toInteger
(final boolean
the to convert
bool
,
final int
the value to return if true
trueValue
,
final int
the value to return if false
falseValue
)

Converts a boolean to an int specifying the conversion values.

public static int

Returns:

the appropriate value
toInteger
(final Boolean
the Boolean to convert
bool
,
final int
the value to return if true
trueValue
,
final int
the value to return if false
falseValue
,
final int
the value to return if null
nullValue
)

Converts a Boolean to an int specifying the conversion values.

public static Integer

Returns:

one if true, zero if false
toIntegerObject
(final boolean
the boolean to convert
bool
)

Converts a boolean to an Integer using the convention that true is 1 and false is 0.

public static Integer

Returns:

the appropriate value
toIntegerObject
(final boolean
the to convert
bool
,
final Integer
the value to return if true, may be null
trueValue
,
final Integer
the value to return if false, may be null
falseValue
)

Converts a boolean to an Integer specifying the conversion values.

public static Integer

Returns:

one if Boolean.TRUE, zero if Boolean.FALSE, null if null
toIntegerObject
(final Boolean
the Boolean to convert
bool
)

Converts a Boolean to an Integer using the convention that zero is false.

public static Integer

Returns:

the appropriate value
toIntegerObject
(final Boolean
the Boolean to convert
bool
,
final Integer
the value to return if true, may be null
trueValue
,
final Integer
the value to return if false, may be null
falseValue
,
final Integer
the value to return if null, may be null
nullValue
)

Converts a Boolean to an Integer specifying the conversion values.

public static String

Returns:

one of the two input Strings
toString
(final boolean
the Boolean to check
bool
,
final String
the String to return if true, may be null
trueString
,
final String
the String to return if false, may be null
falseString
)

Converts a boolean to a String returning one of the input Strings.

public static String

Returns:

one of the three input Strings
toString
(final Boolean
the Boolean to check
bool
,
final String
the String to return if true, may be null
trueString
,
final String
the String to return if false, may be null
falseString
,
final String
the String to return if null, may be null
nullString
)

Converts a Boolean to a String returning one of the input Strings.

public static String

Returns:

'on', 'off', or null
toStringOnOff
(final boolean
the Boolean to check
bool
)

Converts a boolean to a String returning 'on' or 'off'.

public static String

Returns:

'on', 'off', or null
toStringOnOff
(final Boolean
the Boolean to check
bool
)

Converts a Boolean to a String returning 'on', 'off', or null.

public static String

Returns:

'true', 'false', or null
toStringTrueFalse
(final boolean
the Boolean to check
bool
)

Converts a boolean to a String returning 'true' or 'false'.

public static String

Returns:

'true', 'false', or null
toStringTrueFalse
(final Boolean
the Boolean to check
bool
)

Converts a Boolean to a String returning 'true', 'false', or null.

public static String

Returns:

'yes', 'no', or null
toStringYesNo
(final boolean
the Boolean to check
bool
)

Converts a boolean to a String returning 'yes' or 'no'.

public static String

Returns:

'yes', 'no', or null
toStringYesNo
(final Boolean
the Boolean to check
bool
)

Converts a Boolean to a String returning 'yes', 'no', or null.

public static List<Boolean>

Returns:

an unmodifiable list of Booleans [false, true].
values
()

Returns an unmodifiable list of Booleans [false, true].

public static boolean

Returns:

true if the number of true values in the array is odd; otherwise returns false.
xor
(final boolean...
an array of booleans
array
)

Performs an xor on a set of booleans.

public static Boolean

Returns:

the result of the xor operations
xor
(final Boolean...
an array of Booleans
array
)

Performs an xor on an array of Booleans.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BOOLEAN_LISTback to summary
private static final List<Boolean> BOOLEAN_LIST
FALSEback to summary
public static final String FALSE

The false String "false".

Since
3.12.0
NOback to summary
public static final String NO

The no String "no".

Since
3.12.0
OFFback to summary
public static final String OFF

The off String "off".

Since
3.12.0
ONback to summary
public static final String ON

The on String "on".

Since
3.12.0
TRUEback to summary
public static final String TRUE

The true String "true".

Since
3.12.0
YESback to summary
public static final String YES

The yes String "yes".

Since
3.12.0

Constructor Detail

BooleanUtilsback to summary
public BooleanUtils()

Deprecated

TODO Make private in 4.0.

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.

Annotations
@Deprecated

Method Detail

andback to summary
public static boolean and(final boolean... array)

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:boolean[]

an array of booleans

Returns:boolean

the result of the logical 'and' operation. That is false if any of the parameters is false and true otherwise.

Exceptions
NullPointerException:
if array is null
IllegalArgumentException:
if array is empty.
Since
3.0.1
andback to summary
public static Boolean and(final Boolean... array)

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:Boolean[]

an array of Booleans

Returns:Boolean

the result of the logical 'and' operation. That is false if any of the parameters is false and true otherwise.

Exceptions
NullPointerException:
if array is null
IllegalArgumentException:
if array is empty.
Since
3.0.1
booleanValuesback to summary
public static Boolean[] booleanValues()

Returns a new array of possible values (like an enum would).

Returns:Boolean[]

a new array of possible values (like an enum would).

Since
3.12.0
compareback to summary
public static int compare(final boolean x, final boolean y)

Compares two boolean values. This is the same functionality as provided in Java 7.

Parameters
x:boolean

the first boolean to compare

y:boolean

the second boolean to compare

Returns:int

the value 0 if x == y; a value less than 0 if !x && y; and a value greater than 0 if x && !y

Since
3.4
forEachback to summary
public static void forEach(final Consumer<Boolean> action)

Performs the given action for each Boolean BooleanUtils#values().

Parameters
action:Consumer<Boolean>

The action to be performed for each element

Since
3.13.0
isFalseback to summary
public static boolean isFalse(final 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
Parameters
bool:Boolean

the boolean to check, null returns false

Returns:boolean

true only if the input is non-null and false

Since
2.1
isNotFalseback to summary
public static boolean isNotFalse(final 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
Parameters
bool:Boolean

the boolean to check, null returns true

Returns:boolean

true if the input is null or true

Since
2.3
isNotTrueback to summary
public static boolean isNotTrue(final 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
Parameters
bool:Boolean

the boolean to check, null returns true

Returns:boolean

true if the input is null or false

Since
2.3
isTrueback to summary
public static boolean isTrue(final 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
Parameters
bool:Boolean

the boolean to check, null returns false

Returns:boolean

true only if the input is non-null and true

Since
2.1
negateback to summary
public static Boolean negate(final 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 unboxed to a boolean.

  BooleanUtils.negate(Boolean.TRUE)  = Boolean.FALSE;
  BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE;
  BooleanUtils.negate(null)          = null;
Parameters
bool:Boolean

the Boolean to negate, may be null

Returns:Boolean

the negated Boolean, or null if null input

oneHotback to summary
public static boolean oneHot(final boolean... array)

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:boolean[]

an array of booleans

Returns:boolean

the result of the one-hot operations

Exceptions
NullPointerException:
if array is null
IllegalArgumentException:
if array is empty.
oneHotback to summary
public static Boolean oneHot(final Boolean... array)

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:Boolean[]

an array of booleans

Returns:Boolean

the result of the one-hot operations

Exceptions
NullPointerException:
if array is null
IllegalArgumentException:
if array is empty.
orback to summary
public static boolean or(final boolean... array)

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:boolean[]

an array of booleans

Returns:boolean

true if any of the arguments is true, and it returns false otherwise.

Exceptions
NullPointerException:
if array is null
IllegalArgumentException:
if array is empty.
Since
3.0.1
orback to summary
public static Boolean or(final Boolean... array)

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:Boolean[]

an array of Booleans

Returns:Boolean

true if any of the arguments is true, and it returns false otherwise.

Exceptions
NullPointerException:
if array is null
IllegalArgumentException:
if array is empty.
Since
3.0.1
primitiveValuesback to summary
public static boolean[] primitiveValues()

Returns a new array of possible values (like an enum would).

Returns:boolean[]

a new array of possible values (like an enum would).

Since
3.12.0
toBooleanback to summary
public static boolean toBoolean(final 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
Parameters
bool:Boolean

the boolean to convert

Returns:boolean

true or false, null returns false

toBooleanback to summary
public static boolean toBoolean(final int value)

Converts an int to a boolean using the convention that zero is false, everything else is true.

  BooleanUtils.toBoolean(0) = false
  BooleanUtils.toBoolean(1) = true
  BooleanUtils.toBoolean(2) = true
Parameters
value:int

the int to convert

Returns:boolean

true if non-zero, false if zero

toBooleanback to summary
public static boolean toBoolean(final int value, final int trueValue, final int falseValue)

Converts an int to a boolean specifying the conversion values.

If the trueValue and falseValue are the same number then the return value will be true in case 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
Parameters
value:int

the Integer to convert

trueValue:int

the value to match for true

falseValue:int

the value to match for false

Returns:boolean

true or false

Exceptions
IllegalArgumentException:
if value does not match neither trueValue no falseValue
toBooleanback to summary
public static boolean toBoolean(final Integer value, final Integer trueValue, final 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
Parameters
value:Integer

the Integer to convert

trueValue:Integer

the value to match for true, may be null

falseValue:Integer

the value to match for false, may be null

Returns:boolean

true or false

Exceptions
IllegalArgumentException:
if no match
toBooleanback to summary
public static boolean toBoolean(final String str)

Converts a String to a boolean (optimized for performance).

'true', 'on', 'y', 't' 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', '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:String

the String to check

Returns:boolean

the boolean value of the string, false if no match or the String is null

toBooleanback to summary
public static boolean toBoolean(final String str, final String trueString, final 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
Parameters
str:String

the String to check

trueString:String

the String to match for true (case-sensitive), may be null

falseString:String

the String to match for false (case-sensitive), may be null

Returns:boolean

the boolean value of the string

Exceptions
IllegalArgumentException:
if the String doesn't match
toBooleanDefaultIfNullback to summary
public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull)

Converts a Boolean to a boolean handling null.

  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:Boolean

the boolean object to convert to primitive

valueIfNull:boolean

the boolean value to return if the parameter bool is null

Returns:boolean

true or false

toBooleanObjectback to summary
public static Boolean toBooleanObject(final int value)

Converts an int to a Boolean using the convention that zero is false, everything else is true.

  BooleanUtils.toBoolean(0) = Boolean.FALSE
  BooleanUtils.toBoolean(1) = Boolean.TRUE
  BooleanUtils.toBoolean(2) = Boolean.TRUE
Parameters
value:int

the int to convert

Returns:Boolean

Boolean.TRUE if non-zero, Boolean.FALSE if zero, null if null

toBooleanObjectback to summary
public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue)

Converts an int to a Boolean specifying the conversion values.

Note

This method may return null and may throw a NullPointerException if unboxed to a boolean.

The checks are done first for the trueValue, then for the falseValue and finally for the 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
Parameters
value:int

the Integer to convert

trueValue:int

the value to match for true

falseValue:int

the value to match for false

nullValue:int

the value to match for null

Returns:Boolean

Boolean.TRUE, Boolean.FALSE, or null

Exceptions
IllegalArgumentException:
if no match
toBooleanObjectback to summary
public static Boolean toBooleanObject(final Integer value)

Converts an Integer to a Boolean using the convention that zero is false, every other numeric value is true.

null will be converted to null.

Note

This method may return null and may throw a NullPointerException if unboxed to a boolean.

  BooleanUtils.toBooleanObject(Integer.valueOf(0))    = Boolean.FALSE
  BooleanUtils.toBooleanObject(Integer.valueOf(1))    = Boolean.TRUE
  BooleanUtils.toBooleanObject(Integer.valueOf(null)) = null
Parameters
value:Integer

the Integer to convert

Returns:Boolean

Boolean.TRUE if non-zero, Boolean.FALSE if zero, null if null input

toBooleanObjectback to summary
public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue)

Converts an Integer to a Boolean specifying the conversion values.

Note

This method may return null and may throw a NullPointerException if unboxed to a boolean.

The checks are done first for the trueValue, then for the falseValue and finally for the 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
Parameters
value:Integer

the Integer to convert

trueValue:Integer

the value to match for true, may be null

falseValue:Integer

the value to match for false, may be null

nullValue:Integer

the value to match for null, may be null

Returns:Boolean

Boolean.TRUE, Boolean.FALSE, or null

Exceptions
IllegalArgumentException:
if no match
toBooleanObjectback to summary
public static Boolean toBooleanObject(final String str)

Converts a String to a Boolean.

'true', 'on', 'y', 't', 'yes' or '1' (case insensitive) will return true. 'false', 'off', 'n', 'f', 'no' or '0' (case insensitive) will return false. Otherwise, null is returned.

Note

This method may return null and may throw a NullPointerException if unboxed to a boolean.

  // 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:String

the String to check; upper and lower case are treated as the same

Returns:Boolean

the Boolean value of the string, null if no match or null input

toBooleanObjectback to summary
public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString)

Converts a String to a Boolean throwing an exception if no match.

Note

This method may return null and may throw a NullPointerException if unboxed to a boolean.

  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:String

the String to check

trueString:String

the String to match for true (case-sensitive), may be null

falseString:String

the String to match for false (case-sensitive), may be null

nullString:String

the String to match for null (case-sensitive), may be null

Returns:Boolean

the Boolean value of the string, null if either the String matches nullString or if null input and nullString is null

Exceptions
IllegalArgumentException:
if the String doesn't match
toIntegerback to summary
public static int toInteger(final boolean bool)

Converts a boolean to an int using the convention that true is 1 and false is 0.

  BooleanUtils.toInteger(true)  = 1
  BooleanUtils.toInteger(false) = 0
Parameters
bool:boolean

the boolean to convert

Returns:int

one if true, zero if false

toIntegerback to summary
public static int toInteger(final boolean bool, final int trueValue, final int falseValue)

Converts a boolean to an int specifying the conversion values.

  BooleanUtils.toInteger(true, 1, 0)  = 1
  BooleanUtils.toInteger(false, 1, 0) = 0
Parameters
bool:boolean

the to convert

trueValue:int

the value to return if true

falseValue:int

the value to return if false

Returns:int

the appropriate value

toIntegerback to summary
public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final 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
Parameters
bool:Boolean

the Boolean to convert

trueValue:int

the value to return if true

falseValue:int

the value to return if false

nullValue:int

the value to return if null

Returns:int

the appropriate value

toIntegerObjectback to summary
public static Integer toIntegerObject(final boolean bool)

Converts a boolean to an Integer using the convention that true is 1 and false is 0.

  BooleanUtils.toIntegerObject(true)  = Integer.valueOf(1)
  BooleanUtils.toIntegerObject(false) = Integer.valueOf(0)
Parameters
bool:boolean

the boolean to convert

Returns:Integer

one if true, zero if false

toIntegerObjectback to summary
public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final 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)
Parameters
bool:boolean

the to convert

trueValue:Integer

the value to return if true, may be null

falseValue:Integer

the value to return if false, may be null

Returns:Integer

the appropriate value

toIntegerObjectback to summary
public static Integer toIntegerObject(final Boolean bool)

Converts a Boolean to an 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)
Parameters
bool:Boolean

the Boolean to convert

Returns:Integer

one if Boolean.TRUE, zero if Boolean.FALSE, null if null

toIntegerObjectback to summary
public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final 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:Boolean

the Boolean to convert

trueValue:Integer

the value to return if true, may be null

falseValue:Integer

the value to return if false, may be null

nullValue:Integer

the value to return if null, may be null

Returns:Integer

the appropriate value

toStringback to summary
public static String toString(final boolean bool, final String trueString, final 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"
Parameters
bool:boolean

the Boolean to check

trueString:String

the String to return if true, may be null

falseString:String

the String to return if false, may be null

Returns:String

one of the two input Strings

toStringback to summary
public static String toString(final Boolean bool, final String trueString, final String falseString, final 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:Boolean

the Boolean to check

trueString:String

the String to return if true, may be null

falseString:String

the String to return if false, may be null

nullString:String

the String to return if null, may be null

Returns:String

one of the three input Strings

toStringOnOffback to summary
public static String toStringOnOff(final boolean bool)

Converts a boolean to a String returning 'on' or 'off'.

  BooleanUtils.toStringOnOff(true)   = "on"
  BooleanUtils.toStringOnOff(false)  = "off"
Parameters
bool:boolean

the Boolean to check

Returns:String

'on', 'off', or null

toStringOnOffback to summary
public static String toStringOnOff(final 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;
Parameters
bool:Boolean

the Boolean to check

Returns:String

'on', 'off', or null

toStringTrueFalseback to summary
public static String toStringTrueFalse(final boolean bool)

Converts a boolean to a String returning 'true' or 'false'.

  BooleanUtils.toStringTrueFalse(true)   = "true"
  BooleanUtils.toStringTrueFalse(false)  = "false"
Parameters
bool:boolean

the Boolean to check

Returns:String

'true', 'false', or null

toStringTrueFalseback to summary
public static String toStringTrueFalse(final 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;
Parameters
bool:Boolean

the Boolean to check

Returns:String

'true', 'false', or null

toStringYesNoback to summary
public static String toStringYesNo(final boolean bool)

Converts a boolean to a String returning 'yes' or 'no'.

  BooleanUtils.toStringYesNo(true)   = "yes"
  BooleanUtils.toStringYesNo(false)  = "no"
Parameters
bool:boolean

the Boolean to check

Returns:String

'yes', 'no', or null

toStringYesNoback to summary
public static String toStringYesNo(final 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;
Parameters
bool:Boolean

the Boolean to check

Returns:String

'yes', 'no', or null

valuesback to summary
public static List<Boolean> values()

Returns an unmodifiable list of Booleans [false, true].

Returns:List<Boolean>

an unmodifiable list of Booleans [false, true].

Since
3.13.0
xorback to summary
public static boolean xor(final boolean... array)

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:boolean[]

an array of booleans

Returns:boolean

true if the number of true values in the array is odd; otherwise returns false.

Exceptions
NullPointerException:
if array is null
IllegalArgumentException:
if array is empty.
xorback to summary
public static Boolean xor(final Boolean... array)

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:Boolean[]

an array of Booleans

Returns:Boolean

the result of the xor operations

Exceptions
NullPointerException:
if array is null
IllegalArgumentException:
if array is empty.