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 asfalsevalues (ignoring case): "false", "0", "false", "off", "disabled".The default set of strings that count astruevalues (ignoring case): "true", "1", "yes", "on", "enabled". -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Booleanfrom(byte n) Converts the specifiedNumberto aBooleanvalue.static Booleanfrom(char c) Converts the specifiedcharacterto aBooleanvalue.static Booleanfrom(double n) Converts the specifieddoubleto aBooleanvalue.static Booleanfrom(float n) Converts the specifiedfloatto aBooleanvalue.static Booleanfrom(int n) Converts the specifiedintto aBooleanvalue.static Booleanfrom(long n) Converts the specifiedlongto aBooleanvalue.static Booleanfrom(short n) Converts the specifiedshortto aBooleanvalue.static BooleanConverts the specifiedNumberto aBooleanvalue.static BooleanAttempts to convert the specified object to aBoolean.static BooleanConverts the specifiedStringto aBooleanvalue.booleanReturnstrueif the specified string isnullor one of the string that count astrueorfalse.static booleanReturnstrueif the specified string isnullor one of the string that count astrueorfalse.test(byte n) Converts the specifiedbyteto aBooleanvalue.test(char c) Converts the specifiedcharto aBooleanvalue.test(double n) Converts the specifieddoubleto aBooleanvalue.test(float n) Converts the specifiedfloatto aBooleanvalue.test(int n) Converts the specifiedintto aBooleanvalue.test(long n) Converts the specifiedlongto aBooleanvalue.test(short n) Converts the specifiedshortto aBooleanvalue.Converts the specifiedNumberto aBooleanvalue.Attempts to convert the specified object to aBoolean.Converts the specifiedStringto aBooleanvalue.
-
Field Details
-
TRUE_STRINGS
The default set of strings that count astruevalues (ignoring case): "true", "1", "yes", "on", "enabled". -
FALSE_STRINGS
The default set of strings that count asfalsevalues (ignoring case): "false", "0", "false", "off", "disabled".
-
-
Constructor Details
-
Bool
Creates a newBoolinstance that will use the providedtruestrings andfalsestrings to evaluateStringarguments.- Parameters:
trueStrings- the string values that must count astruefalseStrings- 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 specificfrommethods, according to the type of the argument. If the object's type is not covered by any of the otherfrommethods anIllegalArgumentExceptionis thrown.- Parameters:
obj- the object to convert- Returns:
- the corresponding
Booleanvalue
-
from
Converts the specifiedStringto aBooleanvalue. This method checks whether the argument is either one ofTRUE_STRINGSor one ofFALSE_STRINGS(ignoring case).- Parameters:
s- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
isConvertible
Returnstrueif the specified string isnullor one of the string that count astrueorfalse.- Parameters:
s- the string to evaluate- Returns:
- whether the specified string is one of the string that count as
trueorfalse
-
from
Converts the specifiedNumberto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
from
Converts the specifiedintto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
from
Converts the specifiedcharacterto aBooleanvalue.- Parameters:
c- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
from
Converts the specifieddoubleto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
from
Converts the specifiedlongto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
from
Converts the specifiedfloatto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
from
Converts the specifiedshortto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
from
Converts the specifiedNumberto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
test
Attempts to convert the specified object to aBoolean. This is done by delegating to one of the more specificgetBooleanmethods, according to the type of the argument. If the object's type is not covered by any of the othergetBooleanmethods, aTypeConversionExceptionis thrown.- Parameters:
obj- the object to convert- Returns:
- the corresponding
Booleanvalue
-
test
Converts the specifiedStringto aBooleanvalue. Null values are allowed and will result inBoolean.FALSEbeing returns.- Parameters:
s- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
isBoolean
Returnstrueif the specified string isnullor one of the string that count astrueorfalse.- Parameters:
s- the string to evaluate- Returns:
- whether the specified string is
nullor one of the string that count astrueorfalse
-
test
Converts the specifiedNumberto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
test
Converts the specifiedintto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
test
Converts the specifieddoubleto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
test
Converts the specifiedlongto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
test
Converts the specifiedfloatto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
test
Converts the specifiedshortto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
test
Converts the specifiedbyteto aBooleanvalue.- Parameters:
n- the value to be converted- Returns:
- the corresponding
Booleanvalue
-
test
Converts the specifiedcharto aBooleanvalue. Returnstrueif the argument equals '1';falseif the argument equals '0'; otherwise throws aTypeConversionException.- Parameters:
c- The argument- Returns:
- the corresponding
Booleanvalue
-