maputils.java
来自「JAVA 文章管理系统源码」· Java 代码 · 共 1,470 行 · 第 1/4 页
JAVA
1,470 行
/**
* Gets a short from a Map in a null-safe manner,
* using the default value if the the conversion fails.
* <p>
* 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, <code>defaultValue</code> 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.
* <p>
* 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, <code>defaultValue</code> 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.
* <p>
* 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, <code>defaultValue</code> 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.
* <p>
* 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, <code>defaultValue</code> 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.
* <p>
* 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, <code>defaultValue</code> 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
//-------------------------------------------------------------------------
/**
* Gets a new Properties object initialised with the values from a Map.
* A null input will return an empty properties object.
*
* @param map the map to convert to a Properties object, may not be null
* @return the properties object
*/
public static Properties toProperties(final Map map) {
Properties answer = new Properties();
if (map != null) {
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
answer.put(key, value);
}
}
return answer;
}
/**
* Creates a new HashMap using data copied from a ResourceBundle.
*
* @param resourceBundle the resource bundle to convert, may not be null
* @return the hashmap containing the data
* @throws NullPointerException if the bundle is null
*/
public static Map toMap(final ResourceBundle resourceBundle) {
Enumeration enumeration = resourceBundle.getKeys();
Map map = new HashMap();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
Object value = resourceBundle.getObject(key);
map.put(key, value);
}
return map;
}
// Printing methods
//-------------------------------------------------------------------------
/**
* Prints the given map with nice line breaks.
* <p>
* This method prints a nicely formatted String describing the Map.
* Each map entry will be printed with key and value.
* When the value is a Map, recursive behaviour occurs.
* <p>
* This method is NOT thread-safe in any special way. You must manually
* synchronize on either this class or the stream as required.
*
* @param out the stream to print to, must not be null
* @param label The label to be used, may be <code>null</code>.
* If <code>null</code>, the label is not output.
* It typically represents the name of the property in a bean or similar.
* @param map The map to print, may be <code>null</code>.
* If <code>null</code>, the text 'null' is output.
* @throws NullPointerException if the stream is <code>null</code>
*/
public static void verbosePrint(
final PrintStream out,
final Object label,
final Map map) {
verbosePrintInternal(out, label, map, new ArrayStack(), false);
}
/**
* Prints the given map with nice line breaks.
* <p>
* This method prints a nicely formatted String describing the Map.
* Each map entry will be printed with key, value and value classname.
* When the value is a Map, recursive behaviour occurs.
* <p>
* This method is NOT thread-safe in any special way. You must manually
* synchronize on either this class or the stream as required.
*
* @param out the stream to print to, must not be null
* @param label The label to be used, may be <code>null</code>.
* If <code>null</code>, the label is not output.
* It typically represents the name of the property in a bean or similar.
* @param map The map to print, may be <code>null</code>.
* If <code>null</code>, the text 'null' is output.
* @throws NullPointerException if the stream is <code>null</code>
*/
public static void debugPrint(
final PrintStream out,
final Object label,
final Map map) {
verbosePrintInternal(out, label, map, new ArrayStack(), true);
}
// Implementation methods
//-------------------------------------------------------------------------
/**
* Logs the given exception to <code>System.out</code>.
* <p>
* This method exists as Jakarta Collections does not depend on logging.
*
* @param ex the exception to log
*/
protected static void logInfo(final Exception ex) {
System.out.println("INFO: Exception: " + ex);
}
/**
* Implementation providing functionality for {@link #debugPrint} and for
* {@link #verbosePrint}. This prints the given map with nice line breaks.
* If the debug flag is true, it additionally prints the type of the object
* value. If the contents of a map include the map itself, then the text
* <em>(this Map)</em> is printed out. If the contents include a
* parent container of the map, the the text <em>(ancestor[i] Map)</em> is
* printed, where i actually indicates the number of levels which must be
* traversed in the sequential list of ancestors (e.g. father, grandfather,
* great-grandfather, etc).
*
* @param out the stream to print to
* @param label the label to be used, may be <code>null</code>.
* If <code>null</code>, the label is not output.
* It typically represents the name of the property in a bean or similar.
* @param map the map to print, may be <code>null</code>.
* If <code>null</code>, the text 'null' is output
* @param lineage a stack consisting of any maps in which the previous
* argument is contained. This is checked to avoid infinite recursion when
* printing the output
* @param debug flag indicating whether type names should be output.
* @throws NullPointerException if the stream is <code>null</code>
*/
private static void verbosePrintInternal(
final PrintStream out,
final Object label,
final Map map,
final ArrayStack lineage,
final boolean debug) {
printIndent(out, lineage.size());
if (map == null) {
if (label != null) {
out.print(label);
out.print(" = ");
}
out.println("null");
return;
}
if (label != null) {
out.print(label);
out.println(" = ");
}
printIndent(out, lineage.size());
out.println("{");
lineage.push(map);
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
Object childKey = entry.getKey();
Object childValue = entry.getValue();
if (childValue instanceof Map && !lineage.contains(childValue)) {
verbosePrintInternal(
out,
(childKey == null ? "null" : childKey),
(Map) childValue,
lineage,
debug);
} else {
printIndent(out, lineage.size());
out.print(childKey);
out.print(" = ");
final int lineageIndex = lineage.indexOf(childValue);
if (lineageIndex == -1) {
out.print(childValue);
} else if (lineage.size() - 1 == lineageIndex) {
out.print("(this Map)");
} else {
out.print(
"(ancestor["
+ (lineage.size() - 1 - lineageIndex - 1)
+ "] Map)");
}
if (debug && childValue != null) {
out.print(' ');
out.println(childValue.getClass().getName());
} else {
out.println();
}
}
}
lineage.pop();
printIndent(out, lineage.size());
out.println(debug ? "} " + map.getClass().getName() : "}");
}
/**
* Writes indentation to the given stream.
*
* @param out the stream to indent
*/
private static void printIndent(final PrintStream out, final int indent) {
for (int i = 0; i < indent; i++) {
out.print(INDENT_STRING);
}
}
// Misc
//-----------------------------------------------------------------------
/**
* Inverts the supplied map returning a new HashMap such that the keys of
* the input are swapped with the values.
* <p>
* This operation assumes that the inverse mapping is well defined.
* If the input map had multiple entries with the same value mapped to
* different keys, the returned map will map one of those keys to the
* value, but the exact key which will be mapped is undefined.
*
* @see DoubleOrderedMap
* @param map the map to invert, may not be null
* @return a new HashMap containing the inverted data
* @throws NullPointerException if the map is null
*/
public static Map invertMap(Map map) {
Map out = new HashMap(map.size());
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
out.put(entry.getValue(), entry.getKey());
}
return out;
}
/**
* 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.
*
* @param map the map to add to, may not be null
* @param key the key
* @param value the value
* @throws NullPointerException if the map is null
*/
public static void safeAddToMap(Map map, Object key, Object value) throws NullPointerException {
if (value == null) {
map.put ( key, "" );
} else {
map.put ( key, value );
}
}
// Map decorators
//-----------------------------------------------------------------------
/**
* Returns a synchronized map backed by the given map.
* <p>
* You must manually synchronize on the returned buffer's iterator to
* avoid non-deterministic behavior:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?