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 found.
* *null is returned if there is no match.
* ** BooleanUtils.toBoolean("true", "true", "false") = true * BooleanUtils.toBoolean("false", "true", "false") = false ** * @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
* @return the boolean value of the string
* @throws IllegalArgumentException if the String doesn't match
*/
public static boolean toBoolean(String str, String trueString, String falseString) {
if (str == null) {
if (trueString == null) {
return true;
} else if (falseString == null) {
return false;
}
} else if (str.equals(trueString)) {
return true;
} else if (str.equals(falseString)) {
return false;
}
// no match
throw new IllegalArgumentException("The String did not match either specified value");
}
}