|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.collections.MapUtils
public class MapUtils
Provides utility methods and decorators for
Map
and SortedMap
instances.
fixedSizeMap(Map)
fixedSizeSortedMap(SortedMap)
lazyMap(Map,Factory)
lazySortedMap(SortedMap,Factory)
predicatedMap(Map,Predicate,Predicate)
predicatedSortedMap(SortedMap,Predicate,Predicate)
transformedMap(Map, Transformer, Transformer)
transformedSortedMap(SortedMap, Transformer, Transformer)
typedMap(Map, Class, Class)
typedSortedMap(SortedMap, Class, Class)
Field Summary | |
---|---|
static Map |
EMPTY_MAP
An empty unmodifiable map. |
static SortedMap |
EMPTY_SORTED_MAP
An empty unmodifiable sorted map. |
Constructor Summary | |
---|---|
MapUtils()
MapUtils should not normally be instantiated. |
Method Summary | ||
---|---|---|
static void |
debugPrint(PrintStream out,
Object label,
Map map)
Prints the given map with nice line breaks. |
|
static
|
fixedSizeMap(Map<K,V> map)
Returns a fixed-sized map backed by the given map. |
|
static
|
fixedSizeSortedMap(SortedMap<K,V> map)
Returns a fixed-sized sorted map backed by the given sorted map. |
|
static Boolean |
getBoolean(Map map,
Object key)
Gets a Boolean from a Map in a null-safe manner. |
|
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 boolean |
getBooleanValue(Map map,
Object key)
Gets a boolean from a Map in a null-safe manner. |
|
static boolean |
getBooleanValue(Map map,
Object key,
boolean defaultValue)
Gets a boolean from a Map in a null-safe manner, using the default value if the the conversion fails. |
|
static Byte |
getByte(Map map,
Object key)
Gets a Byte from a Map in a null-safe manner. |
|
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 byte |
getByteValue(Map map,
Object key)
Gets a byte from a Map in a null-safe manner. |
|
static byte |
getByteValue(Map map,
Object key,
byte defaultValue)
Gets a byte from a Map in a null-safe manner, using the default value if the the conversion fails. |
|
static Double |
getDouble(Map map,
Object key)
Gets a Double from a Map in a null-safe manner. |
|
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 double |
getDoubleValue(Map map,
Object key)
Gets a double from a Map in a null-safe manner. |
|
static double |
getDoubleValue(Map map,
Object key,
double defaultValue)
Gets a double from a Map in a null-safe manner, using the default value if the the conversion fails. |
|
static Float |
getFloat(Map map,
Object key)
Gets a Float from a Map in a null-safe manner. |
|
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 float |
getFloatValue(Map map,
Object key)
Gets a float from a Map in a null-safe manner. |
|
static float |
getFloatValue(Map map,
Object key,
float defaultValue)
Gets a float from a Map in a null-safe manner, using the default value if the the conversion fails. |
|
static Integer |
getInteger(Map map,
Object key)
Gets a Integer from a Map in a null-safe manner. |
|
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 int |
getIntValue(Map map,
Object key)
Gets an int from a Map in a null-safe manner. |
|
static int |
getIntValue(Map map,
Object key,
int defaultValue)
Gets an int from a Map in a null-safe manner, using the default value if the the conversion fails. |
|
static Long |
getLong(Map map,
Object key)
Gets a Long from a Map in a null-safe manner. |
|
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 long |
getLongValue(Map map,
Object key)
Gets a long from a Map in a null-safe manner. |
|
static long |
getLongValue(Map map,
Object key,
long defaultValue)
Gets a long from a Map in a null-safe manner, using the default value if the the conversion fails. |
|
static
|
getMap(Map<K,Map<A,B>> map,
K key)
Gets a Map from a Map in a null-safe manner. |
|
static
|
getMap(Map<K,Map<A,B>> map,
K key,
Map<A,B> 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)
Gets a Number from a Map in a null-safe manner. |
|
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
|
getObject(Map<K,V> map,
K key,
V defaultValue)
Looks up the given key in the given map, converting null into the given default value. |
|
static
|
getObject(Map<K,V> map,
V key)
Gets from a Map in a null-safe manner. |
|
static Short |
getShort(Map map,
Object key)
Gets a Short from a Map in a null-safe manner. |
|
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 short |
getShortValue(Map map,
Object key)
Gets a short from a Map in a null-safe manner. |
|
static short |
getShortValue(Map map,
Object key,
short defaultValue)
Gets a short from a Map in a null-safe manner, using the default value if the the conversion fails. |
|
static
|
getString(Map<K,V> map,
K key)
Gets a String from a Map in a null-safe manner. |
|
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
|
invertMap(Map<K,V> map)
Inverts the supplied map returning a new HashMap such that the keys of the input are swapped with the values. |
|
static
|
lazyMap(Map<K,V> map,
Factory<V> factory)
Returns a "lazy" map whose values will be created on demand. |
|
static
|
lazySortedMap(SortedMap<K,V> map,
Factory<V> factory)
Returns a "lazy" sorted map whose values will be created on demand. |
|
protected static void |
logInfo(Exception ex)
Logs the given exception to System.out . |
|
static
|
orderedMap(Map<K,V> map)
Returns a map that maintains the order of keys that are added backed by the given map. |
|
static
|
predicatedMap(Map<K,V> map,
Predicate<? super K> keyPred,
Predicate<? super V> valuePred)
Returns a predicated (validating) map backed by the given map. |
|
static
|
predicatedSortedMap(SortedMap<K,V> map,
Predicate<? super K> keyPred,
Predicate<? super V> valuePred)
Returns a predicated (validating) 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
|
synchronizedMap(Map<K,V> map)
Returns a synchronized map backed by the given map. |
|
static
|
synchronizedSortedMap(SortedMap<K,V> map)
Returns a synchronized sorted map backed by the given sorted map. |
|
static Map<String,Object> |
toMap(ResourceBundle resourceBundle)
Creates a new HashMap using data copied from a ResourceBundle. |
|
static Properties |
toProperties(Map map)
Gets a new Properties object initialised with the values from a Map. |
|
static Map |
transformedMap(Map map,
Transformer keyTransformer,
Transformer valueTransformer)
Returns a transformed map backed by the given map. |
|
static SortedMap |
transformedSortedMap(SortedMap map,
Transformer keyTransformer,
Transformer valueTransformer)
Returns a transformed sorted map backed by the given map. |
|
static Map |
typedMap(Map map,
Class keyType,
Class valueType)
Deprecated. this is no longer needed with Java generics. |
|
static SortedMap |
typedSortedMap(SortedMap map,
Class keyType,
Class valueType)
Deprecated. no longer needed with Java generics. |
|
static
|
unmodifiableMap(Map<K,V> map)
Returns an unmodifiable map backed by the given map. |
|
static
|
unmodifiableSortedMap(SortedMap<K,V> map)
Returns an unmodifiable sorted map backed by the given sorted map. |
|
static void |
verbosePrint(PrintStream out,
Object label,
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 |
Field Detail |
---|
public static final Map EMPTY_MAP
public static final SortedMap EMPTY_SORTED_MAP
Constructor Detail |
---|
public MapUtils()
MapUtils
should not normally be instantiated.
Method Detail |
---|
public static <K,V> V getObject(Map<K,V> map, V key)
map
- the map to usekey
- the key to look up
null
if null map inputpublic static <K,V> String getString(Map<K,V> map, K key)
toString
.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static Boolean getBoolean(Map map, Object key)
Boolean
it is returned directly.
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, null
is returned.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static Number getNumber(Map map, Object key)
Number
it is returned directly.
If the value is a String
it is converted using
NumberFormat.parse(String)
on the system default formatter
returning null
if the conversion fails.
Otherwise, null
is returned.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static Byte getByte(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static Short getShort(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static Integer getInteger(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static Long getLong(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static Float getFloat(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static Double getDouble(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static <K,A,B> Map<A,B> getMap(Map<K,Map<A,B>> map, K key)
null
is returned.
map
- the map to usekey
- the key to look up
null
if null map inputpublic static <K,V> V getObject(Map<K,V> map, K key, V 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
public 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 fails
public 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 fails
public 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 fails
public 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 fails
public 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 fails
public 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 fails
public 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 fails
public 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 fails
public 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 fails
public static <K,A,B> Map<A,B> getMap(Map<K,Map<A,B>> map, K key, Map<A,B> 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 fails
public static boolean getBooleanValue(Map map, Object key)
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.
map
- the map to usekey
- the key to look up
false
if null map inputpublic static byte getByteValue(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
0
if null map inputpublic static short getShortValue(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
0
if null map inputpublic static int getIntValue(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
0
if null map inputpublic static long getLongValue(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
0L
if null map inputpublic static float getFloatValue(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
0.0F
if null map inputpublic static double getDoubleValue(Map map, Object key)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look up
0.0
if null map inputpublic static boolean getBooleanValue(Map map, Object key, boolean defaultValue)
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.
map
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the
conversion fails
defaultValue
if null map inputpublic static byte getByteValue(Map map, Object key, byte defaultValue)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the
conversion fails
defaultValue
if null map inputpublic static short getShortValue(Map map, Object key, short defaultValue)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the
conversion fails
defaultValue
if null map inputpublic static int getIntValue(Map map, Object key, int defaultValue)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the
conversion fails
defaultValue
if null map inputpublic static long getLongValue(Map map, Object key, long defaultValue)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the
conversion fails
defaultValue
if null map inputpublic static float getFloatValue(Map map, Object key, float defaultValue)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the
conversion fails
defaultValue
if null map inputpublic static double getDoubleValue(Map map, Object key, double defaultValue)
getNumber(Map,Object)
.
map
- the map to usekey
- the key to look updefaultValue
- return if the value is null or if the
conversion fails
defaultValue
if null map inputpublic static Properties toProperties(Map map)
map
- the map to convert to a Properties object, may not be null
public static Map<String,Object> toMap(ResourceBundle resourceBundle)
resourceBundle
- the resource bundle to convert, may not be null
NullPointerException
- if the bundle is nullpublic static void verbosePrint(PrintStream out, Object label, Map map)
out
- the stream to print to, must not be nulllabel
- The label to be used, may be null
.
If null
, the label is not output.
It typically represents the name of the property in a bean or similar.map
- The map to print, may be null
.
If null
, the text 'null' is output.
NullPointerException
- if the stream is null
public static void debugPrint(PrintStream out, Object label, Map map)
out
- the stream to print to, must not be nulllabel
- The label to be used, may be null
.
If null
, the label is not output.
It typically represents the name of the property in a bean or similar.map
- The map to print, may be null
.
If null
, the text 'null' is output.
NullPointerException
- if the stream is null
protected static void logInfo(Exception ex)
System.out
.
This method exists as Jakarta Collections does not depend on logging.
ex
- the exception to logpublic static <K,V> Map<V,K> invertMap(Map<K,V> map)
map
- the map to invert, may not be null
NullPointerException
- if the map is nullpublic static void safeAddToMap(Map map, Object key, Object value) throws NullPointerException
Note: this is not a type-safe operation in Java 1.5.
map
- the map to add to, may not be nullkey
- the keyvalue
- the value
NullPointerException
- if the map is nullpublic static <K,V> Map<K,V> synchronizedMap(Map<K,V> map)
Map m = MapUtils.synchronizedMap(myMap); Set s = m.keySet(); // outside synchronized block synchronized (m) { // synchronized on MAP! Iterator i = s.iterator(); while (i.hasNext()) { process (i.next()); } }This method uses the implementation in
Collections
.
map
- the map to synchronize, must not be null
IllegalArgumentException
- if the map is nullpublic static <K,V> Map<K,V> unmodifiableMap(Map<K,V> map)
map
- the map to make unmodifiable, must not be null
IllegalArgumentException
- if the map is nullpublic static <K,V> Map<K,V> predicatedMap(Map<K,V> map, Predicate<? super K> keyPred, Predicate<? super V> valuePred)
map
- the map to predicate, must not be nullkeyPred
- the predicate for keys, null means no checkvaluePred
- the predicate for values, null means no check
IllegalArgumentException
- if the Map is nullpublic static Map typedMap(Map map, Class keyType, Class valueType)
map
- the map to limit to a specific type, must not be nullkeyType
- the type of keys which may be added to the map, must not be nullvalueType
- the type of values which may be added to the map, must not be null
IllegalArgumentException
- if the Map or Class is nullpublic static Map transformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer)
map
- the map to transform, must not be nullkeyTransformer
- the transformer for the map keys, null means no transformationvalueTransformer
- the transformer for the map values, null means no transformation
IllegalArgumentException
- if the Map is nullpublic static <K,V> Map<K,V> fixedSizeMap(Map<K,V> map)
Map.put(Object,Object)
method).
map
- the map whose size to fix, must not be null
IllegalArgumentException
- if the Map is nullpublic static <K,V> Map<K,V> lazyMap(Map<K,V> map, Factory<V> factory)
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 lazyMap = MapUtils.lazyMap(new HashMap(), factory); Object obj = lazyMap.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 in the map.
map
- the map to make lazy, must not be nullfactory
- the factory for creating new objects, must not be null
IllegalArgumentException
- if the Map or Factory is nullpublic static <K,V> Map<K,V> orderedMap(Map<K,V> map)
map
- the map to order, must not be null
IllegalArgumentException
- if the Map is nullpublic static <K,V> Map<K,V> synchronizedSortedMap(SortedMap<K,V> map)
Map m = MapUtils.synchronizedSortedMap(myMap); Set s = m.keySet(); // outside synchronized block synchronized (m) { // synchronized on MAP! Iterator i = s.iterator(); while (i.hasNext()) { process (i.next()); } }This method uses the implementation in
Collections
.
map
- the map to synchronize, must not be null
IllegalArgumentException
- if the map is nullpublic static <K,V> Map<K,V> unmodifiableSortedMap(SortedMap<K,V> map)
map
- the sorted map to make unmodifiable, must not be null
IllegalArgumentException
- if the map is nullpublic static <K,V> SortedMap<K,V> predicatedSortedMap(SortedMap<K,V> map, Predicate<? super K> keyPred, Predicate<? super V> valuePred)
map
- the map to predicate, must not be nullkeyPred
- the predicate for keys, null means no checkvaluePred
- the predicate for values, null means no check
IllegalArgumentException
- if the SortedMap is nullpublic static SortedMap typedSortedMap(SortedMap map, Class keyType, Class valueType)
map
- the map to limit to a specific type, must not be nullkeyType
- the type of keys which may be added to the map, must not be nullvalueType
- the type of values which may be added to the map, must not be null
public static SortedMap transformedSortedMap(SortedMap map, Transformer keyTransformer, Transformer valueTransformer)
map
- the map to transform, must not be nullkeyTransformer
- the transformer for the map keys, null means no transformationvalueTransformer
- the transformer for the map values, null means no transformation
IllegalArgumentException
- if the SortedMap is nullpublic static <K,V> SortedMap<K,V> fixedSizeSortedMap(SortedMap<K,V> map)
Map.put(Object,Object)
method).
map
- the map whose size to fix, must not be null
IllegalArgumentException
- if the SortedMap is nullpublic static <K,V> SortedMap<K,V> lazySortedMap(SortedMap<K,V> map, Factory<V> factory)
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
IllegalArgumentException
- if the SortedMap or Factory is null
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |