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

com.github.bingoohuang.utils.lang.Mapp Maven / Gradle / Ivy

package com.github.bingoohuang.utils.lang;

import org.apache.commons.lang3.StringUtils;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.github.bingoohuang.utils.codec.Json.json;
import static com.github.bingoohuang.utils.codec.Json.unJson;

public class Mapp {
    public static  Map of(K k1, V v1) {
        Map map = newHashMap();
        map.put(k1, v1);

        return map;
    }

    public static  Map of(K k1, V v1, K k2, V v2) {
        Map map = of(k1, v1);
        map.put(k2, v2);

        return map;
    }

    public static  Map of(K k1, V v1, K k2, V v2, K k3, V v3) {
        Map map = of(k1, v1, k2, v2);
        map.put(k3, v3);

        return map;
    }

    public static  Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
        Map map = of(k1, v1, k2, v2, k3, v3);
        map.put(k4, v4);

        return map;
    }

    public static  Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
        Map map = of(k1, v1, k2, v2, k3, v3, k4, v4);
        map.put(k5, v5);

        return map;
    }

    public static  Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
        Map map = of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
        map.put(k6, v6);

        return map;
    }

    public static  Map of(T... keyAndValues) {
        Map map = newHashMap();
        for (int i = 0; i < keyAndValues.length; i += 2) {
            T key = keyAndValues[i];
            T value = i + 1 < keyAndValues.length ? keyAndValues[i + 1] : null;
            map.put(key, value);
        }

        return map;
    }

    public static Map map(Object... keyAndValues) {
        Map map = newHashMap();
        for (int i = 0; i < keyAndValues.length; i += 2) {
            Object key = keyAndValues[i];
            Object value = i + 1 < keyAndValues.length ? keyAndValues[i + 1] : null;
            map.put(key, value);
        }

        return map;
    }

    public static  Map newHashMap() {
        return new HashMap();
    }

    public static boolean isEmpty(Map map) {
        return map == null || map.isEmpty();
    }

    public static String getStr(Map m, Object key) {
        return getStr(m, key, null);
    }

    public static String getStr(Map m, Object key, String defaultValue) {
        if (m == null) return defaultValue;
        Object value = m.get(key);
        if (value == null) return defaultValue;
        return value.toString();
    }

    public static Number getNum(Map m, Object key) {
        if (m == null) return null;
        Object value = m.get(key);
        if (value == null) return null;
        if (value instanceof Number) return (Number) value;
        if (!(value instanceof String)) return null;
        try {
            return NumberFormat.getInstance().parse((String) value);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Integer getInt(Map m, Object key) {
        Number value = getNum(m, key);
        if (value == null) return null;
        return value instanceof Integer ? (Integer) value : new Integer(value.intValue());
    }

    public static Long getLong(Map m, Object key) {
        Number value = getNum(m, key);
        if (value == null) return null;
        return value instanceof Long ? (Long) value : new Long(value.longValue());
    }

    public static  Map desc(Object obj) {
        return unJson(json(obj), Map.class);
    }

    public static  T spec(Map map, Class clz) {
        return unJson(json(map), clz);
    }

    public static Map mapFromList(List list, String keyKey, String valueKey) {
        if (list == null) return newHashMap();

        Map result = newHashMap();
        for (Map map : list) {
            if (!map.containsKey(keyKey) || StringUtils.isEmpty(getStr(map, keyKey)) ||
                    !map.containsKey(valueKey) || StringUtils.isEmpty(getStr(map, valueKey))) continue;
            result.put(map.get(keyKey), map.get(valueKey));
        }
        return result;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy