Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
org.apache.commons.collections.MapUtils
public class MapUtils
extends java.lang.Object
Map
instances.
It contains various typesafe methods
as well as other useful features like deep copying.
It also provides the following decorators:
Constructor Summary | |
|
Method Summary | |
static void |
|
protected static void |
|
static Map |
|
static SortedMap |
|
static Boolean |
|
static Boolean |
|
static Byte |
|
static Byte |
|
static Double |
|
static Double |
|
static Float |
|
static Float |
|
static Integer |
|
static Integer |
|
static Long |
|
static Long |
|
static Map |
|
static Map |
|
static Number |
|
static Number |
|
static Object |
|
static Object |
|
static Short |
|
static Short |
|
static String |
|
static String |
|
static Map | |
static SortedMap |
|
protected static void |
|
static Map |
|
static SortedMap |
|
static void |
|
static Properties |
|
static void |
|
public static void debugPrint(PrintStream out, Object key, Map map)
Prints the given map with nice line breaks.
- Parameters:
out
- the stream to print tokey
- the key that maps to the map in some other mapmap
- the map to print
protected static void debugPrintIndent(PrintStream out)
Writes indentation to the given stream.
- Parameters:
out
- the stream to indent
public static Map fixedSizeMap(Map map)
Returns a fixed-sized map backed by the given map. Elements may not be added or removed from the returned map, but existing elements can be changed (for instance, via theMap.put(Object,Object)
method).
- Parameters:
map
- the map whose size to fix, must not be null
- Returns:
- a fixed-size map backed by that map
public static SortedMap fixedSizeSortedMap(SortedMap map)
Returns a fixed-sized sorted map backed by the given sorted map. Elements may not be added or removed from the returned map, but existing elements can be changed (for instance, via theMap.put(Object,Object)
method).
- Parameters:
map
- the map whose size to fix, must not be null
- Returns:
- a fixed-size map backed by that map
public static Boolean getBoolean(Map map, Object key)
Looks up the given key in the given map, converting the result into aBoolean
. If the map is null, this method returns null. If the value mapped by the given key is aBoolean
, then it is returned as-is. Otherwise, if the value is a string, then if that string ignoring case equals "true", then a trueBoolean
is returned. Any other string value will result in a falseBoolean
being returned. OR, if the value is aNumber
, and thatNumber
is 0, then a falseBoolean
is returned. Any otherNumber
value results in a trueBoolean
being returned. Any value that is not aBoolean
,String
orNumber
results in null being returned.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- a
Boolean
or null
public static Boolean getBoolean(Map map, Object key, Boolean defaultValue)
Looks up the given key in the given map, converting the result into a boolean, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- 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 Byte getByte(Map map, Object key)
Looks up the given key in the given map, converting the result into aByte
. First,getNumber(Map,Object)
is invoked. If the result is null, then null is returned. Otherwise, the byte value of the resultingNumber
is returned.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- a
Byte
or null
public static Byte getByte(Map map, Object key, Byte defaultValue)
Looks up the given key in the given map, converting the result into a byte, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- 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 Double getDouble(Map map, Object key)
Looks up the given key in the given map, converting the result into aDouble
. First,getNumber(Map,Object)
is invoked. If the result is null, then null is returned. Otherwise, the double value of the resultingNumber
is returned.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- a
Double
or null
public static Double getDouble(Map map, Object key, Double defaultValue)
Looks up the given key in the given map, converting the result into a double, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- 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 Float getFloat(Map map, Object key)
Looks up the given key in the given map, converting the result into aFloat
. First,getNumber(Map,Object)
is invoked. If the result is null, then null is returned. Otherwise, the float value of the resultingNumber
is returned.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- a
Float
or null
public static Float getFloat(Map map, Object key, Float defaultValue)
Looks up the given key in the given map, converting the result into a float, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- 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 Integer getInteger(Map map, Object key)
Looks up the given key in the given map, converting the result into anInteger
. First,getNumber(Map,Object)
is invoked. If the result is null, then null is returned. Otherwise, the integer value of the resultingNumber
is returned.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- an
Integer
or null
public static Integer getInteger(Map map, Object key, Integer defaultValue)
Looks up the given key in the given map, converting the result into an integer, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- 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 Long getLong(Map map, Object key)
Looks up the given key in the given map, converting the result into aLong
. First,getNumber(Map,Object)
is invoked. If the result is null, then null is returned. Otherwise, the long value of the resultingNumber
is returned.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- a
Long
or null
public static Long getLong(Map map, Object key, Long defaultValue)
Looks up the given key in the given map, converting the result into a long, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- 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 Map getMap(Map map, Object key)
Looks up the given key in the given map, returning another map. If the given map is null or if the given key doesn't map to another map, then this method returns null. Otherwise the mapped map is returned.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- a
Map
or null
public static Map getMap(Map map, Object key, Map defaultValue)
Looks up the given key in the given map, converting the result into a map, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- the value in the map as a number, or defaultValue if the original value is null, the map is null or the map conversion fails
public static Number getNumber(Map map, Object key)
Looks up the given key in the given map, converting the result into aNumber
. If the map is null, this method returns null. Otherwise, if the key maps to aNumber
, then that number is returned as-is. Otherwise, if the key maps to aString
, that string is parsed into a number using the system defaultNumberFormat
. If the value is not aNumber
or aString
, or if the value is aString
that cannot be parsed into aNumber
, then null is returned.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- a
Number
or null
public static Number getNumber(Map map, Object key, Number defaultValue)
Looks up the given key in the given map, converting the result into a number, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- 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 Object getObject(Map map, Object key)
Synonym forMap.get(Object)
.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- null if the map is null; or the result of
map.get(key)
public static Object getObject(Map map, Object key, Object defaultValue)
Looks up the given key in the given map, converting null into the given default value.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null
- Returns:
- the value in the map, or defaultValue if the original value is null or the map is null
public static Short getShort(Map map, Object key)
Looks up the given key in the given map, converting the result into aShort
. First,getNumber(Map,Object)
is invoked. If the result is null, then null is returned. Otherwise, the short value of the resultingNumber
is returned.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- a
Short
or null
public static Short getShort(Map map, Object key, Short defaultValue)
Looks up the given key in the given map, converting the result into a short, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- 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 String getString(Map map, Object key)
Looks up the given key in the given map, converting the result into a string.
- Parameters:
map
- the map whose value to look upkey
- the key whose value to look up in that map
- Returns:
- null if the map is null; null if the value mapped by that key is null; or the
toString()
result of the value for that key
public static String getString(Map map, Object key, String defaultValue)
Looks up the given key in the given map, converting the result into a string, using the default value if the the conversion fails.
- Parameters:
map
- the map whose value to look upkey
- the key of the value to look up in that mapdefaultValue
- what to return if the value is null or if the conversion fails
- Returns:
- the value in the map as a string, or defaultValue if the original value is null, the map is null or the string conversion fails
public static Map lazyMap(Map map, Factory factory)
Returns a "lazy" map whose values will be created on demand. When the key passed to the returned map'sMap.get(Object)
method is not present in the map, then the factory will be used to create a new object and that object will become the value associated with that key. For instance:Factory factory = new Factory() { public Object create() { return new Date(); } } Map lazy = MapUtils.lazyMap(new HashMap(), factory); Object obj = lazy.get("test");After the above code is executed,obj
will contain a newDate
instance. Furthermore, thatDate
instance is the value for thetest
key.
- Parameters:
map
- the map to make lazy, must not be nullfactory
- the factory for creating new objects, must not be null
- Returns:
- a lazy map backed by the given map
public static SortedMap lazySortedMap(SortedMap map, Factory factory)
Returns a "lazy" sorted map whose values will be created on demand. When the key passed to the returned map'sMap.get(Object)
method is not present in the map, then the factory will be used to create a new object and that object will become the value associated with that key. For instance:Factory factory = new Factory() { public Object create() { return new Date(); } } SortedMap lazy = MapUtils.lazySortedMap(new TreeMap(), factory); Object obj = lazy.get("test");After the above code is executed,obj
will contain a newDate
instance. Furthermore, thatDate
instance is the value for thetest
key.
- Parameters:
map
- the map to make lazy, must not be nullfactory
- the factory for creating new objects, must not be null
- Returns:
- a lazy map backed by the given map
protected static void logInfo(Exception e)
Logs the given exception toSystem.out
.
- Parameters:
e
- the exception to log
public static Map predicatedMap(Map map, Predicate keyPred, Predicate valuePred)
Returns a predicated map backed by the given map. Only keys and values that pass the given predicates can be added to the map. It is important not to use the original map after invoking this method, as it is a backdoor for adding unvalidated objects.
- Parameters:
map
- the map to predicate, must not be nullkeyPred
- the predicate for keys, must not be nullvaluePred
- the predicate for values, must not be null
- Returns:
- a predicated map backed by the given map
public static SortedMap predicatedSortedMap(SortedMap map, Predicate keyPred, Predicate valuePred)
Returns a predicated sorted map backed by the given map. Only keys and values that pass the given predicates can be added to the map. It is important not to use the original map after invoking this method, as it is a backdoor for adding unvalidated objects.
- Parameters:
map
- the map to predicate, must not be nullkeyPred
- the predicate for keys, must not be nullvaluePred
- the predicate for values, must not be null
- Returns:
- a predicated map backed by the given map
public static final void safeAddToMap(Map map, Object key, Object value) throws NullPointerException
Nice method for adding data to a map in such a way as to not get NPE's. The point being that if the value is null, map.put() will throw an exception. That blows in the case of this class cause you may want to essentially treat put("Not Null", null ) == put("Not Null", "") We will still throw a NPE if the key is null cause that should never happen.
public static Properties toProperties(Map map)
Gets a new Properties object initialised with the values from a Map. A null input will return an empty properties object.
- Parameters:
map
- the map to convert to a Properties object
- Returns:
- the properties object
public static void verbosePrint(PrintStream out, Object key, Map map)
Prints the given map with nice line breaks.
- Parameters:
out
- the stream to print tokey
- the key that maps to the map in some other mapmap
- the map to print