📄 maputils.java
字号:
* <p>
* If the value returned from the specified map is not a Map then
* <code>null</code> is returned.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Map, <code>null</code> if null map input
*/
public static Map getMap(final Map map, final Object key) {
if (map != null) {
Object answer = map.get(key);
if (answer != null && answer instanceof Map) {
return (Map) answer;
}
}
return null;
}
// Type safe getters with default values
//-------------------------------------------------------------------------
/**
* Looks up the given key in the given map, converting null into the
* given default value.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null
* @return the value in the map, or defaultValue if the original value
* is null or the map is null
*/
public static Object getObject( Map map, Object key, Object defaultValue ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
return answer;
}
}
return 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.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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 String getString( Map map, Object key, String defaultValue ) {
String answer = getString( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* a boolean, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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 Boolean getBoolean( Map map, Object key, Boolean defaultValue ) {
Boolean answer = getBoolean( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* a number, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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 Number getNumber( Map map, Object key, Number defaultValue ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* a byte, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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 Byte getByte( Map map, Object key, Byte defaultValue ) {
Byte answer = getByte( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* a short, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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 Short getShort( Map map, Object key, Short defaultValue ) {
Short answer = getShort( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* an integer, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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, Integer defaultValue ) {
Integer answer = getInteger( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* a long, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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, Long defaultValue ) {
Long answer = getLong( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* a float, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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, Float defaultValue ) {
Float answer = getFloat( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* a double, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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, Double defaultValue ) {
Double answer = getDouble( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* a map, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return 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 Map getMap( Map map, Object key, Map defaultValue ) {
Map answer = getMap( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
// Type safe primitive getters
//-------------------------------------------------------------------------
/**
* Gets a boolean from a Map in a null-safe manner.
* <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>false</code> is returned.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Boolean, <code>false</code> if null map input
*/
public static boolean getBooleanValue(final Map map, final Object key) {
Boolean booleanObject = getBoolean(map, key);
if (booleanObject == null) {
return false;
}
return booleanObject.booleanValue();
}
/**
* Gets a byte from a Map in a null-safe manner.
* <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
* @return the value in the Map as a byte, <code>0</code> if null map input
*/
public static byte getByteValue(final Map map, final Object key) {
Byte byteObject = getByte(map, key);
if (byteObject == null) {
return 0;
}
return byteObject.byteValue();
}
/**
* Gets a short from a Map in a null-safe manner.
* <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
* @return the value in the Map as a short, <code>0</code> if null map input
*/
public static short getShortValue(final Map map, final Object key) {
Short shortObject = getShort(map, key);
if (shortObject == null) {
return 0;
}
return shortObject.shortValue();
}
/**
* Gets an int from a Map in a null-safe manner.
* <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
* @return the value in the Map as an int, <code>0</code> if null map input
*/
public static int getIntValue(final Map map, final Object key) {
Integer integerObject = getInteger(map, key);
if (integerObject == null) {
return 0;
}
return integerObject.intValue();
}
/**
* Gets a long from a Map in a null-safe manner.
* <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
* @return the value in the Map as a long, <code>0L</code> if null map input
*/
public static long getLongValue(final Map map, final Object key) {
Long longObject = getLong(map, key);
if (longObject == null) {
return 0L;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -