/** * Looks up the given key in the given map, converting the result into * a number, 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 number, or defaultValue if the * original value is null, the map is null or the number conversion fails */ public static <K> Number getNumber(final Map<? super K, ?> map, final K key, final Number defaultValue) { Number answer = getNumber(map, key); if (answer == null) { answer = defaultValue; } return answer; }
/** * Gets a Byte from a Map in a null-safe manner. * <p> * The Byte is obtained from the results of {@link #getNumber(Map,Object)}. * * @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 Byte, <code>null</code> if null map input */ public static <K> Byte getByte(final Map<? super K, ?> map, final K key) { final Number answer = getNumber(map, key); if (answer == null) { return null; } if (answer instanceof Byte) { return (Byte) answer; } return Byte.valueOf(answer.byteValue()); }
/** * Gets a Float from a Map in a null-safe manner. * <p> * The Float is obtained from the results of {@link #getNumber(Map,Object)}. * * @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 Float, <code>null</code> if null map input */ public static <K> Float getFloat(final Map<? super K, ?> map, final K key) { final Number answer = getNumber(map, key); if (answer == null) { return null; } if (answer instanceof Float) { return (Float) answer; } return Float.valueOf(answer.floatValue()); }
/** * Gets a Long from a Map in a null-safe manner. * <p> * The Long is obtained from the results of {@link #getNumber(Map,Object)}. * * @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 Long, <code>null</code> if null map input */ public static <K> Long getLong(final Map<? super K, ?> map, final K key) { final Number answer = getNumber(map, key); if (answer == null) { return null; } if (answer instanceof Long) { return (Long) answer; } return Long.valueOf(answer.longValue()); }
/** * Gets a Double from a Map in a null-safe manner. * <p> * The Double is obtained from the results of {@link #getNumber(Map,Object)}. * * @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 Double, <code>null</code> if null map input */ public static <K> Double getDouble(final Map<? super K, ?> map, final K key) { final Number answer = getNumber(map, key); if (answer == null) { return null; } if (answer instanceof Double) { return (Double) answer; } return Double.valueOf(answer.doubleValue()); }
/** * Gets a Short from a Map in a null-safe manner. * <p> * The Short is obtained from the results of {@link #getNumber(Map,Object)}. * * @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 Short, <code>null</code> if null map input */ public static <K> Short getShort(final Map<? super K, ?> map, final K key) { final Number answer = getNumber(map, key); if (answer == null) { return null; } if (answer instanceof Short) { return (Short) answer; } return Short.valueOf(answer.shortValue()); }
/** * Gets a Integer from a Map in a null-safe manner. * <p> * The Integer is obtained from the results of {@link #getNumber(Map,Object)}. * * @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 Integer, <code>null</code> if null map input */ public static <K> Integer getInteger(final Map<? super K, ?> map, final K key) { final Number answer = getNumber(map, key); if (answer == null) { return null; } if (answer instanceof Integer) { return (Integer) answer; } return Integer.valueOf(answer.intValue()); }