📄 maputils.java
字号:
return longObject.longValue();
}
/**
* Gets a float from a Map in a null-safe manner.
* <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
* @return the value in the Map as a float, <code>0.0F</code> if null map input
*/
public static float getFloatValue(final Map map, final Object key) {
Float floatObject = getFloat(map, key);
if (floatObject == null) {
return 0f;
}
return floatObject.floatValue();
}
/**
* Gets a double from a Map in a null-safe manner.
* <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
* @return the value in the Map as a double, <code>0.0</code> if null map input
*/
public static double getDoubleValue(final Map map, final Object key) {
Double doubleObject = getDouble(map, key);
if (doubleObject == null) {
return 0d;
}
return doubleObject.doubleValue();
}
// Type safe primitive getters with default values
//-------------------------------------------------------------------------
/**
* Gets a boolean from a Map in a null-safe manner,
* using the default value if the the conversion fails.
* <p>
* If the value is a <code>Boolean</code> its value is returned.
* If the value is a <code>String</code> and it equals 'true' ignoring case
* then <code>true</code> is returned, otherwise <code>false</code>.
* If the value is a <code>Number</code> an integer zero value returns
* <code>false</code> and non-zero returns <code>true</code>.
* Otherwise, <code>defaultValue</code> is returned.
*
* @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 Boolean, <code>defaultValue</code> if null map input
*/
public static boolean getBooleanValue(final Map map, final Object key, boolean defaultValue) {
Boolean booleanObject = getBoolean(map, key);
if (booleanObject == null) {
return defaultValue;
}
return booleanObject.booleanValue();
}
/**
* Gets a byte from a Map in a null-safe manner,
* using the default value if the the conversion fails.
* <p>
* The byte 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 byte, <code>defaultValue</code> if null map input
*/
public static byte getByteValue(final Map map, final Object key, byte defaultValue) {
Byte byteObject = getByte(map, key);
if (byteObject == null) {
return defaultValue;
}
return byteObject.byteValue();
}
/**
* 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(" = ");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -