/** * Gets a value and converts it to a Float. * * @param key the value to get * @return the Float value, or null if the value is missing or cannot be converted */ public Float getAsFloat(String key) { Object value = mValues.get(key); try { return value != null ? ((Number) value).floatValue() : null; } catch (ClassCastException e) { if (value instanceof CharSequence) { try { return Float.valueOf(value.toString()); } catch (NumberFormatException e2) { LOG.warn("Cannot parse Float value for " + value + " at key " + key); return null; } } else { LOG.warn("Cannot cast value for " + key + " to a Float: " + value, e); return null; } } }
/** * Gets a value and converts it to a Boolean. * * @param key the value to get * @return the Boolean value, or null if the value is missing or cannot be converted */ public Boolean getAsBoolean(String key) { Object value = mValues.get(key); try { return (Boolean) value; } catch (ClassCastException e) { if (value instanceof CharSequence) { return Boolean.valueOf(value.toString()); } else if (value instanceof Number) { return ((Number) value).intValue() != 0; } else { LOG.warn("Cannot cast value for " + key + " to a Boolean: " + value, e); return null; } } }