/** * Looks up the given key in the given map, converting the result into * a boolean, using the default value if the the conversion fails. * * @param <K> the key type * @param map the map whose value to look up * @param key the key of the value to look up in that map * @param defaultValue what to return if the value is null or if the * conversion fails * @return the value in the map as a boolean, or defaultValue if the * original value is null, the map is null or the boolean conversion fails */ public static <K> Boolean getBoolean(final Map<? super K, ?> map, final K key, final Boolean defaultValue) { Boolean answer = getBoolean(map, key); if (answer == null) { answer = defaultValue; } return answer; }
/** * Gets a boolean from a Map in a null-safe manner. * <p> * If the value is a <code>Boolean</code> its value is returned. * If the value is a <code>String</code> and it equals 'true' ignoring case * then <code>true</code> is returned, otherwise <code>false</code>. * If the value is a <code>Number</code> an integer zero value returns * <code>false</code> and non-zero returns <code>true</code>. * Otherwise, <code>false</code> is returned. * * @param <K> the key type * @param map the map to use * @param key the key to look up * @return the value in the Map as a Boolean, <code>false</code> if null map input */ public static <K> boolean getBooleanValue(final Map<? super K, ?> map, final K key) { return Boolean.TRUE.equals(getBoolean(map, key)); }
/** * Gets a boolean from a Map in a null-safe manner, * using the default value if the the conversion fails. * <p> * If the value is a <code>Boolean</code> its value is returned. * If the value is a <code>String</code> and it equals 'true' ignoring case * then <code>true</code> is returned, otherwise <code>false</code>. * If the value is a <code>Number</code> an integer zero value returns * <code>false</code> and non-zero returns <code>true</code>. * Otherwise, <code>defaultValue</code> is returned. * * @param <K> the key type * @param map the map to use * @param key the key to look up * @param defaultValue return if the value is null or if the conversion fails * @return the value in the Map as a Boolean, <code>defaultValue</code> if null map input */ public static <K> boolean getBooleanValue(final Map<? super K, ?> map, final K key, final boolean defaultValue) { final Boolean booleanObject = getBoolean(map, key); if (booleanObject == null) { return defaultValue; } return booleanObject.booleanValue(); }