Class Bool
java.lang.Object
org.klojang.convert.Bool
Converts values from various non-boolean types to boolean values. Where
applicable,
null
is accepted as an argument and evaluates to
false
. Values evaluating to true
and values evaluating to
false
will both be tightly defined, rather than (for example) 1 counting
as true
and anything else as false
. If an argument is neither a
true
value nor a false
value, an IllegalArgumentException
is thrown. The static method use the TRUE_STRINGS
and
FALSE_STRINGS
sets to determine if a String
is true-ish or falsy.
You can also instantiate the Bool
class with your own true
strings
and false
strings.- Author:
- Ayco Holleman
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe default set of strings that count asfalse
values (ignoring case): "false", "0", "false", "off", "disabled".The default set of strings that count astrue
values (ignoring case): "true", "1", "yes", "on", "enabled". -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Boolean
from
(byte n) Converts the specifiedNumber
to aBoolean
value.static Boolean
from
(char c) Converts the specifiedcharacter
to aBoolean
value.static Boolean
from
(double n) Converts the specifieddouble
to aBoolean
value.static Boolean
from
(float n) Converts the specifiedfloat
to aBoolean
value.static Boolean
from
(int n) Converts the specifiedint
to aBoolean
value.static Boolean
from
(long n) Converts the specifiedlong
to aBoolean
value.static Boolean
from
(short n) Converts the specifiedshort
to aBoolean
value.static Boolean
Converts the specifiedNumber
to aBoolean
value.static Boolean
Attempts to convert the specified object to aBoolean
.static Boolean
Converts the specifiedString
to aBoolean
value.boolean
Returnstrue
if the specified string isnull
or one of the string that count astrue
orfalse
.static boolean
Returnstrue
if the specified string isnull
or one of the string that count astrue
orfalse
.test
(byte n) Converts the specifiedbyte
to aBoolean
value.test
(char c) Converts the specifiedchar
to aBoolean
value.test
(double n) Converts the specifieddouble
to aBoolean
value.test
(float n) Converts the specifiedfloat
to aBoolean
value.test
(int n) Converts the specifiedint
to aBoolean
value.test
(long n) Converts the specifiedlong
to aBoolean
value.test
(short n) Converts the specifiedshort
to aBoolean
value.Converts the specifiedNumber
to aBoolean
value.Attempts to convert the specified object to aBoolean
.Converts the specifiedString
to aBoolean
value.
-
Field Details
-
TRUE_STRINGS
-
FALSE_STRINGS
-
-
Constructor Details
-
Bool
Creates a newBool
instance that will use the providedtrue
strings andfalse
strings to evaluateString
arguments.- Parameters:
trueStrings
- the string values that must count astrue
falseStrings
- the string values that must count asfalse
-
-
Method Details
-
from
Attempts to convert the specified object to aBoolean
. This is done by delegating to one of the more specificfrom
methods, according to the type of the argument. If the object's type is not covered by any of the otherfrom
methods anIllegalArgumentException
is thrown.- Parameters:
obj
- the object to convert- Returns:
- the corresponding
Boolean
value
-
from
Converts the specifiedString
to aBoolean
value. This method checks whether the argument is either one ofTRUE_STRINGS
or one ofFALSE_STRINGS
(ignoring case).- Parameters:
s
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
isConvertible
Returnstrue
if the specified string isnull
or one of the string that count astrue
orfalse
.- Parameters:
s
- the string to evaluate- Returns:
- whether the specified string is one of the string that count as
true
orfalse
-
from
-
from
Converts the specifiedint
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
from
Converts the specifiedcharacter
to aBoolean
value.- Parameters:
c
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
from
Converts the specifieddouble
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
from
Converts the specifiedlong
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
from
Converts the specifiedfloat
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
from
Converts the specifiedshort
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
from
Converts the specifiedNumber
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
test
Attempts to convert the specified object to aBoolean
. This is done by delegating to one of the more specificgetBoolean
methods, according to the type of the argument. If the object's type is not covered by any of the othergetBoolean
methods, aTypeConversionException
is thrown.- Parameters:
obj
- the object to convert- Returns:
- the corresponding
Boolean
value
-
test
-
isBoolean
Returnstrue
if the specified string isnull
or one of the string that count astrue
orfalse
.- Parameters:
s
- the string to evaluate- Returns:
- whether the specified string is
null
or one of the string that count astrue
orfalse
-
test
-
test
Converts the specifiedint
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
test
Converts the specifieddouble
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
test
Converts the specifiedlong
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
test
Converts the specifiedfloat
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
test
Converts the specifiedshort
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
test
Converts the specifiedbyte
to aBoolean
value.- Parameters:
n
- the value to be converted- Returns:
- the corresponding
Boolean
value
-
test
Converts the specifiedchar
to aBoolean
value. Returnstrue
if the argument equals '1';false
if the argument equals '0'; otherwise throws aTypeConversionException
.- Parameters:
c
- The argument- Returns:
- the corresponding
Boolean
value
-