All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kaizen4j.common.util.MapUtils Maven / Gradle / Ivy

There is a newer version: 1.3.8.RELEASE
Show newest version
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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy