|
||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.collections.MapUtils
A helper class for using Map
instances.
It contains various typesafe methods as well as other useful features like deep copying.
It also provides the following decorators:
fixedSizeMap(Map)
fixedSizeSortedMap(SortedMap)
lazyMap(Map,Factory)
lazySortedMap(SortedMap,Factory)
predicatedMap(Map,Predicate,Predicate)
predicatedSortedMap(SortedMap,Predicate,Predicate)
Constructor Summary | |
MapUtils()
Please don't instantiate a MapUtils . |
Method Summary | |
static void |
debugPrint(PrintStream out,
Object key,
Map map)
Prints the given map with nice line breaks. |
protected static void |
debugPrintIndent(PrintStream out)
Writes indentation to the given stream. |
static Map |
fixedSizeMap(Map map)
Returns a fixed-sized map backed by the given map. |
static SortedMap |
fixedSizeSortedMap(SortedMap map)
Returns a fixed-sized sorted map backed by the given sorted map. |
static Boolean |
getBoolean(Map map,
Object key)
Looks up the given key in the given map, converting the result into a Boolean . |
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. |
static Byte |
getByte(Map map,
Object key)
Looks up the given key in the given map, converting the result into a Byte . |
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. |
static Double |
getDouble(Map map,
Object key)
Looks up the given key in the given map, converting the result into a Double . |
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. |
static Float |
getFloat(Map map,
Object key)
Looks up the given key in the given map, converting the result into a Float . |
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. |
static Integer |
getInteger(Map map,
Object key)
Looks up the given key in the given map, converting the result into an Integer . |
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. |
static Long |
getLong(Map map,
Object key)
Looks up the given key in the given map, converting the result into a Long . |
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. |
static Map |
getMap(Map map,
Object key)
Looks up the given key in the given map, returning another map. |
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. |
static Number |
getNumber(Map map,
Object key)
Looks up the given key in the given map, converting the result into a Number . |
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. |
static Object |
getObject(Map map,
Object key)
Synonym for Map.get(Object) . |
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. |
static Short |
getShort(Map map,
Object key)
Looks up the given key in the given map, converting the result into a Short . |
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. |
static String |
getString(Map map,
Object key)
Looks up the given key in the given map, converting the result into a string. |
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. |
static Map |
lazyMap(Map map,
Factory factory)
Returns a "lazy" map whose values will be created on demand. |
static SortedMap |
lazySortedMap(SortedMap map,
Factory factory)
Returns a "lazy" sorted map whose values will be created on demand. |
protected static void |
logInfo(Exception e)
Logs the given exception to System.out . |
static Map |
predicatedMap(Map map,
Predicate keyPred,
Predicate valuePred)
Returns a predicated map backed by the given map. |
static SortedMap |
predicatedSortedMap(SortedMap map,
Predicate keyPred,
Predicate valuePred)
Returns a predicated sorted map backed by the given map. |
static void |
safeAddToMap(Map map,
Object key,
Object value)
Nice method for adding data to a map in such a way as to not get NPE's. |
static Properties |
toProperties(Map map)
Gets a new Properties object initialised with the values from a Map. |
static void |
verbosePrint(PrintStream out,
Object key,
Map map)
Prints the given map with nice line breaks. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Constructor Detail |
public MapUtils()
MapUtils
.Method Detail |
public static Object getObject(Map map, Object key)
Map.get(Object)
.map
- the map whose value to look upkey
- the key whose value to look up in that mapmap.get(key)
public static String getString(Map map, Object key)
map
- the map whose value to look upkey
- the key whose value to look up in that maptoString()
result of the value for that keypublic static Boolean getBoolean(Map map, Object key)
Boolean
. If the map is null, this method returns null.
If the value mapped by the given key is a
Boolean
, then it is returned as-is. Otherwise, if the value
is a string, then if that string ignoring case equals "true", then
a true Boolean
is returned. Any other string value will
result in a false Boolean
being returned. OR, if the value
is a Number
, and that Number
is 0, then a false
Boolean
is returned. Any other Number
value results
in a true Boolean
being returned.
Any value that is not a Boolean
, String
or
Number
results in null being returned.
map
- the map whose value to look upkey
- the key whose value to look up in that mapBoolean
or nullpublic static Number getNumber(Map map, Object key)
Number
. If the map is null, this method returns null.
Otherwise, if the key maps to a Number
, then that number
is returned as-is. Otherwise, if the key maps to a String
,
that string is parsed into a number using the system default
NumberFormat
.
If the value is not a Number
or a String
, or if
the value is a String
that cannot be parsed into a
Number
, then null is returned.
map
- the map whose value to look upkey
- the key whose value to look up in that mapNumber
or nullpublic static Byte getByte(Map map, Object key)
Byte
. First, getNumber(Map,Object)
is invoked.
If the result is null, then null is returned. Otherwise, the
byte value of the resulting Number
is returned.map
- the map whose value to look upkey
- the key whose value to look up in that mapByte
or nullpublic static Short getShort(Map map, Object key)
Short
. First, getNumber(Map,Object)
is invoked.
If the result is null, then null is returned. Otherwise, the
short value of the resulting Number
is returned.map
- the map whose value to look upkey
- the key whose value to look up in that mapShort
or nullpublic static Integer getInteger(Map map, Object key)
Integer
. First, getNumber(Map,Object)
is invoked.
If the result is null, then null is returned. Otherwise, the
integer value of the resulting Number
is returned.map
- the map whose value to look upkey
- the key whose value to look up in that mapInteger
or nullpublic static Long getLong(Map map, Object key)
Long
. First, getNumber(Map,Object)
is invoked.
If the result is null, then null is returned. Otherwise, the
long value of the resulting Number
is returned.map
- the map whose value to look upkey
- the key whose value to look up in that mapLong
or nullpublic static Float getFloat(Map map, Object key)
Float
. First, getNumber(Map,Object)
is invoked.
If the result is null, then null is returned. Otherwise, the
float value of the resulting Number
is returned.map
- the map whose value to look upkey
- the key whose value to look up in that mapFloat
or nullpublic static Double getDouble(Map map, Object key)
Double
. First, getNumber(Map,Object)
is invoked.
If the result is null, then null is returned. Otherwise, the
double value of the resulting Number
is returned.map
- the map whose value to look upkey
- the key whose value to look up in that mapDouble
or nullpublic static Map getMap(Map map, Object key)
map
- the map whose value to look upkey
- the key whose value to look up in that mapMap
or nullpublic static Object getObject(Map map, Object key, Object defaultValue)
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 nullpublic static String getString(Map map, Object key, String defaultValue)
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 failspublic static Boolean getBoolean(Map map, Object key, Boolean defaultValue)
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 failspublic static Number getNumber(Map map, Object key, Number defaultValue)
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 failspublic static Byte getByte(Map map, Object key, Byte defaultValue)
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 failspublic static Short getShort(Map map, Object key, Short defaultValue)
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 failspublic static Integer getInteger(Map map, Object key, Integer defaultValue)
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 failspublic static Long getLong(Map map, Object key, Long defaultValue)
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 failspublic static Float getFloat(Map map, Object key, Float defaultValue)
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 failspublic static Double getDouble(Map map, Object key, Double defaultValue)
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 failspublic static Map getMap(Map map, Object key, Map defaultValue)
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 failspublic static Properties toProperties(Map map)
map
- the map to convert to a Properties objectpublic static void verbosePrint(PrintStream out, Object key, Map map)
out
- the stream to print tokey
- the key that maps to the map in some other mapmap
- the map to printpublic static void debugPrint(PrintStream out, Object key, Map map)
out
- the stream to print tokey
- the key that maps to the map in some other mapmap
- the map to printprotected static void debugPrintIndent(PrintStream out)
out
- the stream to indentprotected static void logInfo(Exception e)
System.out
.e
- the exception to logpublic static final void safeAddToMap(Map map, Object key, Object value) throws NullPointerException
public static Map predicatedMap(Map map, Predicate keyPred, Predicate valuePred)
map
- the map to predicate, must not be nullkeyPred
- the predicate for keys, must not be nullvaluePred
- the predicate for values, must not be nullpublic static Map fixedSizeMap(Map map)
Map.put(Object,Object)
method).map
- the map whose size to fix, must not be nullpublic static Map lazyMap(Map map, Factory factory)
When the key passed to the returned map's Map.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 new Date
instance. Furthermore, that Date
instance is the value for the test
key.map
- the map to make lazy, must not be nullfactory
- the factory for creating new objects, must not be nullpublic static SortedMap predicatedSortedMap(SortedMap map, Predicate keyPred, Predicate valuePred)
map
- the map to predicate, must not be nullkeyPred
- the predicate for keys, must not be nullvaluePred
- the predicate for values, must not be nullpublic static SortedMap fixedSizeSortedMap(SortedMap map)
Map.put(Object,Object)
method).map
- the map whose size to fix, must not be nullpublic static SortedMap lazySortedMap(SortedMap map, Factory factory)
When the key passed to the returned map's Map.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 new Date
instance. Furthermore, that Date
instance is the value for the test
key.map
- the map to make lazy, must not be nullfactory
- the factory for creating new objects, must not be null
|
||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |