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.
Converts a String to a Boolean throwing an exception if no match.
* ** BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null ** * @param str the String to check * @param trueString the String to match for
true
* (case sensitive), may be null
* @param falseString the String to match for false
* (case sensitive), may be null
* @param nullString the String to match for null
* (case sensitive), may be null
* @return the Boolean value of the string,
* null
if either the String matches nullString
* or if null
input and nullString
is
* null
* @throws IllegalArgumentException if the String doesn't match
*/
public static Boolean toBooleanObject(String str, String trueString, String falseString, String nullString) {
if (str == null) {
if (trueString == null) {
return Boolean.TRUE;
} else if (falseString == null) {
return Boolean.FALSE;
} else if (nullString == null) {
return null;
}
} else if (str.equals(trueString)) {
return Boolean.TRUE;
} else if (str.equals(falseString)) {
return Boolean.FALSE;
} else if (str.equals(nullString)) {
return null;
}
// no match
throw new IllegalArgumentException("The String did not match any specified value");
}
}