Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.kaizen4j.common.util.MapUtils Maven / Gradle / Ivy
package org.kaizen4j.common.util;
import java.util.*;
public class MapUtils {
public static boolean isEmpty(Map, ?> map) {
return (null == map) ? true : (map.isEmpty() ? true : false);
}
public static boolean isNotEmpty(Map, ?> map) {
return !isEmpty(map);
}
public static String getString(Map map, String key) {
return getString(map, key, null);
}
public static String getString(Map map, String key, String defaultValue) {
Object value = map.get(key);
if (null == value) {
if (null == defaultValue) {
throw new RuntimeException(String.format("Key[%s] is required in map", key));
}
return defaultValue;
}
return value.toString();
}
public static int getInt(Map map, String key) {
return getInt(map, key, null);
}
public static int getInt(Map map, String key, Integer defaultValue) {
Object value = map.get(key);
if (null == value) {
if (null == defaultValue) {
throw new RuntimeException(String.format("Key[%s] is required in map", key));
}
return defaultValue;
}
try {
return Integer.parseInt(value.toString());
} catch (NumberFormatException e) {
throw new NumberFormatException(String.format("Key[%s] should be an int, but was [%s]", key, value));
}
}
public static long getLong(Map map, String key) {
return getLong(map, key, null);
}
public static long getLong(Map map, String key, Long defaultValue) {
Object value = map.get(key);
if (null == value) {
if (null == defaultValue) {
throw new RuntimeException(String.format("Key[%s] is required in map", key));
}
return defaultValue;
}
try {
return Long.parseLong(value.toString());
} catch (NumberFormatException e) {
throw new NumberFormatException(String.format("Key[%s] should be a long, but was [%s]", key, value));
}
}
public static double getDouble(Map map, String key) {
return getDouble(map, key, null);
}
public static double getDouble(Map map, String key, Double defaultValue) {
Object value = map.get(key);
if (null == value) {
if (null == defaultValue) {
throw new RuntimeException(String.format("Key[%s] is required in map", key));
}
return defaultValue;
}
try {
return Double.parseDouble(value.toString());
} catch (NumberFormatException e) {
throw new NumberFormatException(String.format("Key[%s] should be a double, but was [%s]", key, value));
}
}
public static boolean getBoolean(Map map, String key) {
return getBoolean(map, key, null);
}
public static boolean getBoolean(Map map, String key, Boolean defaultValue) {
Object value = map.get(key);
if (null == value) {
if (null == defaultValue) {
throw new RuntimeException(String.format("Key[%s] is required in map", key));
}
return defaultValue;
}
if (value instanceof Number) {
Number n = (Number) value;
return n.intValue() != 0 ? Boolean.TRUE : Boolean.FALSE;
}
String s = value.toString();
if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) {
return Boolean.valueOf(s);
} else {
throw new ClassCastException(
String.format("Key[%s] should be a boolean or number, but was [%s]", key, value));
}
}
public static List getList(Map map, String key) {
return getList(map, key, null);
}
@SuppressWarnings("unchecked")
public static List getList(Map map, String key, List defaultValue) {
Object value = map.get(key);
if (null == value) {
if (null == defaultValue) {
throw new RuntimeException(String.format("Key[%s] is required in map", key));
}
return defaultValue;
}
try {
return (List) value;
} catch (ClassCastException e) {
throw new ClassCastException(String.format("Key[%s] should be a list, but was [%s]", key, value));
}
}
public static Map getMap(Map map, String key) {
return getMap(map, key, null);
}
@SuppressWarnings("unchecked")
public static Map getMap(Map map, String key, Map defaultValue) {
Object value = map.get(key);
if (null == value) {
if (null == defaultValue) {
throw new RuntimeException(String.format("Key[%s] is required in map", key));
}
return defaultValue;
}
try {
return (Map) value;
} catch (ClassCastException e) {
throw new ClassCastException(String.format("Key[%s] should be a map, but was [%s]", key, value));
}
}
public static T getObject(Map map, String key) {
return getObject(map, key, null);
}
@SuppressWarnings("unchecked")
public static T getObject(Map map, String key, T defaultValue) {
Object value = map.get(key);
if (null == value) {
if (null == defaultValue) {
throw new RuntimeException(String.format("Key[%s] is required in map", key));
}
return defaultValue;
}
try {
return (T) value;
} catch (ClassCastException e) {
throw new ClassCastException(
String.format("Key[%s] should be a same class type, but was [%s]", key, value));
}
}
public static Properties toProperties(Map map) {
Properties properties = new Properties();
if (isNotEmpty(map)) {
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object key = entry.getKey();
Object value = entry.getValue();
properties.put(key, value);
}
}
return properties;
}
public static Map toMap(ResourceBundle resourceBundle) {
Map map = new HashMap<>();
if (null != resourceBundle) {
Enumeration enumeration = resourceBundle.getKeys();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
Object value = resourceBundle.getObject(key);
map.put(key, value);
}
}
return map;
}
}