From a9bee7354a578d8b23e38801d1f7bfc2e819b235 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Thu, 11 Dec 2003 23:45:02 +0000 Subject: [PATCH] Add access method for primitive types from Ashwin S git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131433 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/collections/MapUtils.java | 279 +++++++++++++++++- 1 file changed, 277 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/commons/collections/MapUtils.java b/src/java/org/apache/commons/collections/MapUtils.java index e978ae721..161dde452 100644 --- a/src/java/org/apache/commons/collections/MapUtils.java +++ b/src/java/org/apache/commons/collections/MapUtils.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.40 2003/12/03 11:37:44 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.41 2003/12/11 23:45:02 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -109,7 +109,7 @@ import org.apache.commons.collections.map.UnmodifiableSortedMap; * * * @since Commons Collections 1.0 - * @version $Revision: 1.40 $ $Date: 2003/12/03 11:37:44 $ + * @version $Revision: 1.41 $ $Date: 2003/12/11 23:45:02 $ * * @author James Strachan * @author Nissim Karpenstein @@ -120,6 +120,7 @@ import org.apache.commons.collections.map.UnmodifiableSortedMap; * @author Arun Mammen Thomas * @author Janek Bogucki * @author Max Rydahl Andersen + * @author Ashwin S */ public class MapUtils { @@ -601,6 +602,280 @@ public class MapUtils { } return answer; } + + + // Type safe primitive getters + //------------------------------------------------------------------------- + /** + * Gets a boolean from a Map in a null-safe manner. + *

+ * If the value is a Boolean its value is returned. + * If the value is a String and it equals 'true' ignoring case + * then true is returned, otherwise false. + * If the value is a Number an integer zero value returns + * false and non-zero returns true. + * Otherwise, false is returned. + * + * @param map the map to use + * @param key the key to look up + * @return the value in the Map as a Boolean, false if null map input + */ + public static boolean getBooleanValue(final Map map, final Object key) { + Boolean booleanObject = getBoolean(map, key); + if (booleanObject == null) { + return false; + } + return booleanObject.booleanValue(); + } + + /** + * Gets a byte from a Map in a null-safe manner. + *

+ * The byte is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @param map the map to use + * @param key the key to look up + * @return the value in the Map as a byte, 0 if null map input + */ + public static byte getByteValue(final Map map, final Object key) { + Byte byteObject = getByte(map, key); + if (byteObject == null) { + return 0; + } + return byteObject.byteValue(); + } + + /** + * Gets a short from a Map in a null-safe manner. + *

+ * The short is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @param map the map to use + * @param key the key to look up + * @return the value in the Map as a short, 0 if null map input + */ + public static short getShortValue(final Map map, final Object key) { + Short shortObject = getShort(map, key); + if (shortObject == null) { + return 0; + } + return shortObject.shortValue(); + } + + /** + * Gets an int from a Map in a null-safe manner. + *

+ * The int is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @param map the map to use + * @param key the key to look up + * @return the value in the Map as an int, 0 if null map input + */ + public static int getIntValue(final Map map, final Object key) { + Integer integerObject = getInteger(map, key); + if (integerObject == null) { + return 0; + } + return integerObject.intValue(); + } + + /** + * Gets a long from a Map in a null-safe manner. + *

+ * The long is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @param map the map to use + * @param key the key to look up + * @return the value in the Map as a long, 0L if null map input + */ + public static long getLongValue(final Map map, final Object key) { + Long longObject = getLong(map, key); + if (longObject == null) { + return 0L; + } + return longObject.longValue(); + } + + /** + * Gets a float from a Map in a null-safe manner. + *

+ * The float is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @param map the map to use + * @param key the key to look up + * @return the value in the Map as a float, 0.0F if null map input + */ + public static float getFloatValue(final Map map, final Object key) { + Float floatObject = getFloat(map, key); + if (floatObject == null) { + return 0f; + } + return floatObject.floatValue(); + } + + /** + * Gets a double from a Map in a null-safe manner. + *

+ * The double is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @param map the map to use + * @param key the key to look up + * @return the value in the Map as a double, 0.0 if null map input + */ + public static double getDoubleValue(final Map map, final Object key) { + Double doubleObject = getDouble(map, key); + if (doubleObject == null) { + return 0d; + } + return doubleObject.doubleValue(); + } + + // Type safe primitive getters with default values + //------------------------------------------------------------------------- + /** + * Gets a boolean from a Map in a null-safe manner, + * using the default value if the the conversion fails. + *

+ * If the value is a Boolean its value is returned. + * If the value is a String and it equals 'true' ignoring case + * then true is returned, otherwise false. + * If the value is a Number an integer zero value returns + * false and non-zero returns true. + * Otherwise, defaultValue is returned. + * + * @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, defaultValue if null map input + */ + public static boolean getBooleanValue(final Map map, final Object key, boolean defaultValue) { + Boolean booleanObject = getBoolean(map, key); + if (booleanObject == null) { + return defaultValue; + } + return booleanObject.booleanValue(); + } + + /** + * Gets a byte from a Map in a null-safe manner, + * using the default value if the the conversion fails. + *

+ * The byte is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @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 byte, defaultValue if null map input + */ + public static byte getByteValue(final Map map, final Object key, byte defaultValue) { + Byte byteObject = getByte(map, key); + if (byteObject == null) { + return defaultValue; + } + return byteObject.byteValue(); + } + + /** + * Gets a short from a Map in a null-safe manner, + * using the default value if the the conversion fails. + *

+ * The short is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @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 short, defaultValue if null map input + */ + public static short getShortValue(final Map map, final Object key, short defaultValue) { + Short shortObject = getShort(map, key); + if (shortObject == null) { + return defaultValue; + } + return shortObject.shortValue(); + } + + /** + * Gets an int from a Map in a null-safe manner, + * using the default value if the the conversion fails. + *

+ * The int is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @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 an int, defaultValue if null map input + */ + public static int getIntValue(final Map map, final Object key, int defaultValue) { + Integer integerObject = getInteger(map, key); + if (integerObject == null) { + return defaultValue; + } + return integerObject.intValue(); + } + + /** + * Gets a long from a Map in a null-safe manner, + * using the default value if the the conversion fails. + *

+ * The long is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @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 long, defaultValue if null map input + */ + public static long getLongValue(final Map map, final Object key, long defaultValue) { + Long longObject = getLong(map, key); + if (longObject == null) { + return defaultValue; + } + return longObject.longValue(); + } + + /** + * Gets a float from a Map in a null-safe manner, + * using the default value if the the conversion fails. + *

+ * The float is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @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 float, defaultValue if null map input + */ + public static float getFloatValue(final Map map, final Object key, float defaultValue) { + Float floatObject = getFloat(map, key); + if (floatObject == null) { + return defaultValue; + } + return floatObject.floatValue(); + } + + /** + * Gets a double from a Map in a null-safe manner, + * using the default value if the the conversion fails. + *

+ * The double is obtained from the results of {@link #getNumber(Map,Object)}. + * + * @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 double, defaultValue if null map input + */ + public static double getDoubleValue(final Map map, final Object key, double defaultValue) { + Double doubleObject = getDouble(map, key); + if (doubleObject == null) { + return defaultValue; + } + return doubleObject.doubleValue(); + } // Conversion methods //-------------------------------------------------------------------------