一、前言
基于apache-rat(0.9)包中的org.apache.commons.collections.MapUtils工具类,对获取默认值及其类型自动处理转换(getObject、getString、getBoolean、getNumber、getByte、getShort、getInteger、getLong、getFloat、getDouble、getMap)及toProperties转换配置类型、空判断isEmpty\isNotEmpty等通用操作处理。
二、源码说明
package org.apache.commons.collections;@b@@b@import java.io.PrintStream;@b@import java.text.NumberFormat;@b@import java.text.ParseException;@b@import java.util.ArrayList;@b@import java.util.Collections;@b@import java.util.Enumeration;@b@import java.util.HashMap;@b@import java.util.Hashtable;@b@import java.util.Iterator;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Properties;@b@import java.util.ResourceBundle;@b@import java.util.Set;@b@import java.util.SortedMap;@b@import java.util.TreeMap;@b@import org.apache.commons.collections.map.FixedSizeMap;@b@import org.apache.commons.collections.map.FixedSizeSortedMap;@b@import org.apache.commons.collections.map.LazyMap;@b@import org.apache.commons.collections.map.LazySortedMap;@b@import org.apache.commons.collections.map.ListOrderedMap;@b@import org.apache.commons.collections.map.MultiValueMap;@b@import org.apache.commons.collections.map.PredicatedMap;@b@import org.apache.commons.collections.map.PredicatedSortedMap;@b@import org.apache.commons.collections.map.TransformedMap;@b@import org.apache.commons.collections.map.TransformedSortedMap;@b@import org.apache.commons.collections.map.TypedMap;@b@import org.apache.commons.collections.map.TypedSortedMap;@b@import org.apache.commons.collections.map.UnmodifiableMap;@b@import org.apache.commons.collections.map.UnmodifiableSortedMap;@b@@b@public class MapUtils@b@{@b@ public static final Map EMPTY_MAP = UnmodifiableMap.decorate(new HashMap(1));@b@ public static final SortedMap EMPTY_SORTED_MAP = UnmodifiableSortedMap.decorate(new TreeMap());@b@ private static final String INDENT_STRING = " ";@b@@b@ public static Object getObject(Map map, Object key)@b@ {@b@ if (map != null)@b@ return map.get(key);@b@@b@ return null;@b@ }@b@@b@ public static String getString(Map map, Object key)@b@ {@b@ if (map != null) {@b@ Object answer = map.get(key);@b@ if (answer != null)@b@ return answer.toString();@b@ }@b@@b@ return null;@b@ }@b@@b@ public static Boolean getBoolean(Map map, Object key)@b@ {@b@ if (map != null) {@b@ Object answer = map.get(key);@b@ if (answer != null) {@b@ if (answer instanceof Boolean)@b@ return ((Boolean)answer);@b@@b@ if (answer instanceof String)@b@ return new Boolean((String)answer);@b@@b@ if (answer instanceof Number) {@b@ Number n = (Number)answer;@b@ return ((n.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE);@b@ }@b@ }@b@ }@b@ return null;@b@ }@b@@b@ public static Number getNumber(Map map, Object key)@b@ {@b@ if (map != null) {@b@ Object answer = map.get(key);@b@ if (answer != null) {@b@ if (answer instanceof Number)@b@ return ((Number)answer);@b@@b@ if (answer instanceof String)@b@ try {@b@ String text = (String)answer;@b@ return NumberFormat.getInstance().parse(text);@b@ }@b@ catch (ParseException e) {@b@ logInfo(e);@b@ }@b@ }@b@ }@b@@b@ return null;@b@ }@b@@b@ public static Byte getByte(Map map, Object key)@b@ {@b@ Number answer = getNumber(map, key);@b@ if (answer == null)@b@ return null;@b@ if (answer instanceof Byte)@b@ return ((Byte)answer);@b@@b@ return new Byte(answer.byteValue());@b@ }@b@@b@ public static Short getShort(Map map, Object key)@b@ {@b@ Number answer = getNumber(map, key);@b@ if (answer == null)@b@ return null;@b@ if (answer instanceof Short)@b@ return ((Short)answer);@b@@b@ return new Short(answer.shortValue());@b@ }@b@@b@ public static Integer getInteger(Map map, Object key)@b@ {@b@ Number answer = getNumber(map, key);@b@ if (answer == null)@b@ return null;@b@ if (answer instanceof Integer)@b@ return ((Integer)answer);@b@@b@ return new Integer(answer.intValue());@b@ }@b@@b@ public static Long getLong(Map map, Object key)@b@ {@b@ Number answer = getNumber(map, key);@b@ if (answer == null)@b@ return null;@b@ if (answer instanceof Long)@b@ return ((Long)answer);@b@@b@ return new Long(answer.longValue());@b@ }@b@@b@ public static Float getFloat(Map map, Object key)@b@ {@b@ Number answer = getNumber(map, key);@b@ if (answer == null)@b@ return null;@b@ if (answer instanceof Float)@b@ return ((Float)answer);@b@@b@ return new Float(answer.floatValue());@b@ }@b@@b@ public static Double getDouble(Map map, Object key)@b@ {@b@ Number answer = getNumber(map, key);@b@ if (answer == null)@b@ return null;@b@ if (answer instanceof Double)@b@ return ((Double)answer);@b@@b@ return new Double(answer.doubleValue());@b@ }@b@@b@ public static Map getMap(Map map, Object key)@b@ {@b@ if (map != null) {@b@ Object answer = map.get(key);@b@ if ((answer != null) && (answer instanceof Map))@b@ return ((Map)answer);@b@ }@b@@b@ return null;@b@ }@b@@b@ public static Object getObject(Map map, Object key, Object defaultValue)@b@ {@b@ if (map != null) {@b@ Object answer = map.get(key);@b@ if (answer != null)@b@ return answer;@b@ }@b@@b@ return defaultValue;@b@ }@b@@b@ public static String getString(Map map, Object key, String defaultValue)@b@ {@b@ String answer = getString(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static Boolean getBoolean(Map map, Object key, Boolean defaultValue)@b@ {@b@ Boolean answer = getBoolean(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static Number getNumber(Map map, Object key, Number defaultValue)@b@ {@b@ Number answer = getNumber(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static Byte getByte(Map map, Object key, Byte defaultValue)@b@ {@b@ Byte answer = getByte(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static Short getShort(Map map, Object key, Short defaultValue)@b@ {@b@ Short answer = getShort(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static Integer getInteger(Map map, Object key, Integer defaultValue)@b@ {@b@ Integer answer = getInteger(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static Long getLong(Map map, Object key, Long defaultValue)@b@ {@b@ Long answer = getLong(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static Float getFloat(Map map, Object key, Float defaultValue)@b@ {@b@ Float answer = getFloat(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static Double getDouble(Map map, Object key, Double defaultValue)@b@ {@b@ Double answer = getDouble(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static Map getMap(Map map, Object key, Map defaultValue)@b@ {@b@ Map answer = getMap(map, key);@b@ if (answer == null)@b@ answer = defaultValue;@b@@b@ return answer;@b@ }@b@@b@ public static boolean getBooleanValue(Map map, Object key)@b@ {@b@ Boolean booleanObject = getBoolean(map, key);@b@ if (booleanObject == null)@b@ return false;@b@@b@ return booleanObject.booleanValue();@b@ }@b@@b@ public static byte getByteValue(Map map, Object key)@b@ {@b@ Byte byteObject = getByte(map, key);@b@ if (byteObject == null)@b@ return 0;@b@@b@ return byteObject.byteValue();@b@ }@b@@b@ public static short getShortValue(Map map, Object key)@b@ {@b@ Short shortObject = getShort(map, key);@b@ if (shortObject == null)@b@ return 0;@b@@b@ return shortObject.shortValue();@b@ }@b@@b@ public static int getIntValue(Map map, Object key)@b@ {@b@ Integer integerObject = getInteger(map, key);@b@ if (integerObject == null)@b@ return 0;@b@@b@ return integerObject.intValue();@b@ }@b@@b@ public static long getLongValue(Map map, Object key)@b@ {@b@ Long longObject = getLong(map, key);@b@ if (longObject == null)@b@ return 0L;@b@@b@ return longObject.longValue();@b@ }@b@@b@ public static float getFloatValue(Map map, Object key)@b@ {@b@ Float floatObject = getFloat(map, key);@b@ if (floatObject == null)@b@ return 0.0F;@b@@b@ return floatObject.floatValue();@b@ }@b@@b@ public static double getDoubleValue(Map map, Object key)@b@ {@b@ Double doubleObject = getDouble(map, key);@b@ if (doubleObject == null)@b@ return 0.0D;@b@@b@ return doubleObject.doubleValue();@b@ }@b@@b@ public static boolean getBooleanValue(Map map, Object key, boolean defaultValue)@b@ {@b@ Boolean booleanObject = getBoolean(map, key);@b@ if (booleanObject == null)@b@ return defaultValue;@b@@b@ return booleanObject.booleanValue();@b@ }@b@@b@ public static byte getByteValue(Map map, Object key, byte defaultValue)@b@ {@b@ Byte byteObject = getByte(map, key);@b@ if (byteObject == null)@b@ return defaultValue;@b@@b@ return byteObject.byteValue();@b@ }@b@@b@ public static short getShortValue(Map map, Object key, short defaultValue)@b@ {@b@ Short shortObject = getShort(map, key);@b@ if (shortObject == null)@b@ return defaultValue;@b@@b@ return shortObject.shortValue();@b@ }@b@@b@ public static int getIntValue(Map map, Object key, int defaultValue)@b@ {@b@ Integer integerObject = getInteger(map, key);@b@ if (integerObject == null)@b@ return defaultValue;@b@@b@ return integerObject.intValue();@b@ }@b@@b@ public static long getLongValue(Map map, Object key, long defaultValue)@b@ {@b@ Long longObject = getLong(map, key);@b@ if (longObject == null)@b@ return defaultValue;@b@@b@ return longObject.longValue();@b@ }@b@@b@ public static float getFloatValue(Map map, Object key, float defaultValue)@b@ {@b@ Float floatObject = getFloat(map, key);@b@ if (floatObject == null)@b@ return defaultValue;@b@@b@ return floatObject.floatValue();@b@ }@b@@b@ public static double getDoubleValue(Map map, Object key, double defaultValue)@b@ {@b@ Double doubleObject = getDouble(map, key);@b@ if (doubleObject == null)@b@ return defaultValue;@b@@b@ return doubleObject.doubleValue();@b@ }@b@@b@ public static Properties toProperties(Map map)@b@ {@b@ Properties answer = new Properties();@b@ if (map != null)@b@ for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {@b@ Map.Entry entry = (Map.Entry)iter.next();@b@ Object key = entry.getKey();@b@ Object value = entry.getValue();@b@ answer.put(key, value);@b@ }@b@@b@ return answer;@b@ }@b@@b@ public static Map toMap(ResourceBundle resourceBundle)@b@ {@b@ Enumeration enumeration = resourceBundle.getKeys();@b@ Map map = new HashMap();@b@@b@ while (enumeration.hasMoreElements()) {@b@ String key = (String)enumeration.nextElement();@b@ Object value = resourceBundle.getObject(key);@b@ map.put(key, value);@b@ }@b@@b@ return map;@b@ }@b@@b@ public static void verbosePrint(PrintStream out, Object label, Map map)@b@ {@b@ verbosePrintInternal(out, label, map, new ArrayStack(), false);@b@ }@b@@b@ public static void debugPrint(PrintStream out, Object label, Map map)@b@ {@b@ verbosePrintInternal(out, label, map, new ArrayStack(), true);@b@ }@b@@b@ protected static void logInfo(Exception ex)@b@ {@b@ System.out.println("INFO: Exception: " + ex);@b@ }@b@@b@ private static void verbosePrintInternal(PrintStream out, Object label, Map map, ArrayStack lineage, boolean debug)@b@ {@b@ printIndent(out, lineage.size());@b@@b@ if (map == null) {@b@ if (label != null) {@b@ out.print(label);@b@ out.print(" = ");@b@ }@b@ out.println("null");@b@ return;@b@ }@b@ if (label != null) {@b@ out.print(label);@b@ out.println(" = ");@b@ }@b@@b@ printIndent(out, lineage.size());@b@ out.println("{");@b@@b@ lineage.push(map);@b@@b@ for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {@b@ Map.Entry entry = (Map.Entry)it.next();@b@ Object childKey = entry.getKey();@b@ Object childValue = entry.getValue();@b@ if ((childValue instanceof Map) && (!(lineage.contains(childValue)))) {@b@ verbosePrintInternal(out, (childKey == null) ? "null" : childKey, (Map)childValue, lineage, debug); break label295:@b@ }@b@@b@ printIndent(out, lineage.size());@b@ out.print(childKey);@b@ out.print(" = ");@b@@b@ int lineageIndex = lineage.indexOf(childValue);@b@ if (lineageIndex == -1)@b@ out.print(childValue);@b@ else if (lineage.size() - 1 == lineageIndex)@b@ out.print("(this Map)");@b@ else {@b@ out.print("(ancestor[" + (lineage.size() - 1 - lineageIndex - 1) + "] Map)");@b@ }@b@@b@ if ((debug) && (childValue != null)) {@b@ out.print(' ');@b@ out.println(childValue.getClass().getName()); break label295:@b@ }@b@ label295: out.println();@b@ }@b@@b@ lineage.pop();@b@@b@ printIndent(out, lineage.size());@b@ out.println((debug) ? "} " + map.getClass().getName() : "}");@b@ }@b@@b@ private static void printIndent(PrintStream out, int indent)@b@ {@b@ for (int i = 0; i < indent; ++i)@b@ out.print(" ");@b@ }@b@@b@ public static Map invertMap(Map map)@b@ {@b@ Map out = new HashMap(map.size());@b@ for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {@b@ Map.Entry entry = (Map.Entry)it.next();@b@ out.put(entry.getValue(), entry.getKey());@b@ }@b@ return out;@b@ }@b@@b@ public static void safeAddToMap(Map map, Object key, Object value)@b@ throws NullPointerException@b@ {@b@ if (value == null)@b@ map.put(key, "");@b@ else@b@ map.put(key, value);@b@ }@b@@b@ public static Map putAll(Map map, Object[] array)@b@ {@b@ int i;@b@ map.size();@b@ if ((array == null) || (array.length == 0))@b@ return map;@b@@b@ Object obj = array[0];@b@ if (obj instanceof Map.Entry)@b@ for (i = 0; i < array.length; ++i) {@b@ Map.Entry entry = (Map.Entry)array[i];@b@ map.put(entry.getKey(), entry.getValue());@b@ }@b@ else if (obj instanceof KeyValue)@b@ for (i = 0; i < array.length; ++i) {@b@ KeyValue keyval = (KeyValue)array[i];@b@ map.put(keyval.getKey(), keyval.getValue());@b@ }@b@ else if (obj instanceof Object[])@b@ for (i = 0; i < array.length; ++i) {@b@ Object[] sub = (Object[])array[i];@b@ if ((sub == null) || (sub.length < 2))@b@ throw new IllegalArgumentException("Invalid array element: " + i);@b@@b@ map.put(sub[0], sub[1]);@b@ }@b@ else@b@ for (i = 0; i < array.length - 1; )@b@ map.put(array[(i++)], array[(i++)]);@b@@b@@b@ return map;@b@ }@b@@b@ public static boolean isEmpty(Map map)@b@ {@b@ return ((map == null) || (map.isEmpty()));@b@ }@b@@b@ public static boolean isNotEmpty(Map map)@b@ {@b@ return (!(isEmpty(map)));@b@ }@b@@b@ public static Map synchronizedMap(Map map)@b@ {@b@ return Collections.synchronizedMap(map);@b@ }@b@@b@ public static Map unmodifiableMap(Map map)@b@ {@b@ return UnmodifiableMap.decorate(map);@b@ }@b@@b@ public static Map predicatedMap(Map map, Predicate keyPred, Predicate valuePred)@b@ {@b@ return PredicatedMap.decorate(map, keyPred, valuePred);@b@ }@b@@b@ public static Map typedMap(Map map, Class keyType, Class valueType)@b@ {@b@ return TypedMap.decorate(map, keyType, valueType);@b@ }@b@@b@ public static Map transformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer)@b@ {@b@ return TransformedMap.decorate(map, keyTransformer, valueTransformer);@b@ }@b@@b@ public static Map fixedSizeMap(Map map)@b@ {@b@ return FixedSizeMap.decorate(map);@b@ }@b@@b@ public static Map lazyMap(Map map, Factory factory)@b@ {@b@ return LazyMap.decorate(map, factory);@b@ }@b@@b@ public static Map lazyMap(Map map, Transformer transformerFactory)@b@ {@b@ return LazyMap.decorate(map, transformerFactory);@b@ }@b@@b@ public static Map orderedMap(Map map)@b@ {@b@ return ListOrderedMap.decorate(map);@b@ }@b@@b@ public static Map multiValueMap(Map map)@b@ {@b@ return MultiValueMap.decorate(map);@b@ }@b@@b@ public static Map multiValueMap(Map map, Class collectionClass)@b@ {@b@ return MultiValueMap.decorate(map, collectionClass);@b@ }@b@@b@ public static Map multiValueMap(Map map, Factory collectionFactory)@b@ {@b@ return MultiValueMap.decorate(map, collectionFactory);@b@ }@b@@b@ public static Map synchronizedSortedMap(SortedMap map)@b@ {@b@ return Collections.synchronizedSortedMap(map);@b@ }@b@@b@ public static Map unmodifiableSortedMap(SortedMap map)@b@ {@b@ return UnmodifiableSortedMap.decorate(map);@b@ }@b@@b@ public static SortedMap predicatedSortedMap(SortedMap map, Predicate keyPred, Predicate valuePred)@b@ {@b@ return PredicatedSortedMap.decorate(map, keyPred, valuePred);@b@ }@b@@b@ public static SortedMap typedSortedMap(SortedMap map, Class keyType, Class valueType)@b@ {@b@ return TypedSortedMap.decorate(map, keyType, valueType);@b@ }@b@@b@ public static SortedMap transformedSortedMap(SortedMap map, Transformer keyTransformer, Transformer valueTransformer)@b@ {@b@ return TransformedSortedMap.decorate(map, keyTransformer, valueTransformer);@b@ }@b@@b@ public static SortedMap fixedSizeSortedMap(SortedMap map)@b@ {@b@ return FixedSizeSortedMap.decorate(map);@b@ }@b@@b@ public static SortedMap lazySortedMap(SortedMap map, Factory factory)@b@ {@b@ return LazySortedMap.decorate(map, factory);@b@ }@b@@b@ public static SortedMap lazySortedMap(SortedMap map, Transformer transformerFactory)@b@ {@b@ return LazySortedMap.decorate(map, transformerFactory);@b@ }@b@}