protected static final Set FALSE_STRINGS = new HashSet(Arrays.asList(new String[] { "false", "null", "nul", "nil",
"off", "no", "n" }));
public Object convert(Object value, ConvertChain chain) {
if (value == null) {
throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
}
if (value instanceof Boolean) {
return value;
}
if (value instanceof Number) {
return Math.abs(((Number) value).doubleValue()) < Float.MIN_VALUE ? Boolean.FALSE : Boolean.TRUE;
}
if (value instanceof String) {
String strValue = ((String) value).trim();
try {
return Integer.decode(strValue).intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
} catch (NumberFormatException e) {
if (strValue.length() == 0) {
throw new ConvertFailedException().setDefaultValue(DEFAULT_VALUE);
}
strValue = strValue.toLowerCase();
if (TRUE_STRINGS.contains(strValue)) {
return Boolean.TRUE;
}
if (FALSE_STRINGS.contains(strValue)) {
return Boolean.FALSE;
}
throw new ConvertFailedException(e);
}
}
return chain.convert(value);
}