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

cn.kduck.core.utils.ValueMapUtils Maven / Gradle / Ivy

Go to download

The core of the K-Duck development framework encompasses all the featured components of the framework.

There is a newer version: 2.0.0
Show newest version
package cn.kduck.core.utils;

import cn.kduck.core.service.ValueMap;
import cn.kduck.core.service.ValueMapList;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * LiuHG
 */
public final class ValueMapUtils {

    private ValueMapUtils(){}

    public static String getValueAsString(Map valueMap,String name) {
        Object v = valueMap.get(name);
        return getValue(v,String.class);
    }

    public static Integer getValueAsInteger(Map valueMap,String name) {
        Object v = valueMap.get(name);
        return getValue(v,Integer.class);
    }

    public static Long getValueAsLong(Map valueMap,String name) {
        Object v = valueMap.get(name);
        return getValue(v,Long.class);
    }

    public static int getValueAsInt(Map valueMap,String name) {
        Integer v = getValueAsInteger(valueMap,name);
        return v == null ? 0 : v.intValue();
    }

    public static Date getValueAsDate(Map valueMap,String name) {
        Object v = valueMap.get(name);
        return getValue(v,Date.class);
    }

    public static Double getValueAsDouble(Map valueMap,String name) {
        Object v = valueMap.get(name);
        return getValue(v,Double.class);
    }

    public static boolean getValueAsBool(Map valueMap,String name) {
        Boolean v = getValueAsBoolean(valueMap,name);
        return v == null ? false : v.booleanValue();
    }

    public static Float getValueAsFloat(Map valueMap,String name) {
        Object v = valueMap.get(name);
        return getValue(v,Float.class);
    }

    public static Boolean getValueAsBoolean(Map valueMap,String name) {
        Object v = valueMap.get(name);
        return getValue(v,Boolean.class,false);
    }

    public static List getValueAsList(Map valueMap,String name) {
        Object v = valueMap.get(name);
        if(v == null){
            return Collections.emptyList();
        }
        if(v instanceof List){
            return (List)v;
        }

        return Collections.singletonList(v);
    }

    public static  T[] getValueAsArray(Map valueMap,String name,Class clazz){
        Object v = valueMap.get(name);
        if(v != null){
            if(v.getClass().isArray()) {
                return (T[])v;
            }if(v instanceof Collection) {
                Collection collection = (Collection) v;
                T[] vArray = (T[]) Array.newInstance(clazz,collection.size());
                int i = 0;
                for (Object o : collection) {
                    vArray[i++] = ConversionUtils.convert(o,clazz);
                }
                return vArray;
            }else{
               T[] vArray = (T[]) Array.newInstance(clazz,1);
               vArray[0] = ConversionUtils.convert(v,clazz);
               return vArray;
            }
        }

        return (T[]) Array.newInstance(clazz,0);
    }

    public static Map getValueAsMap(Map valueMap,String name) {
        Object v = valueMap.get(name);
        if(v == null){
            return Collections.emptyMap();
        }
        if(v instanceof Map){
            return (Map)v;
        }
        HashMap hashMap = new HashMap();
        hashMap.put(name,v);
        return hashMap;
    }

    public static  R convert(Function bean, T valueMap){
        if(valueMap == null){
            return null;
        }
        return bean.apply(valueMap);
    }

    public static Map> groupData(String groupName, ValueMapList dataList){
        return groupData(groupName,dataList,ValueMap::new);
    }

    public static Map> groupData(String groupName,List dataList){
        return groupData(groupName,dataList,ValueMap::new);
    }

    public static  Map> groupData(String groupName, List dataList, Function bean){
        Map> resultMap = new HashMap<>();
        List groupDataList = null;
        Object groupValue = null;
        for (Map dataMap : dataList) {
            Object value = dataMap.get(groupName);
            if(!value.equals(groupValue)){
                if(groupValue != null){
                    resultMap.put(groupValue,groupDataList);
                }
                groupDataList = new ArrayList();
                groupValue = value;
            }
            groupDataList.add(bean.apply(dataMap));
        }

        if(!dataList.isEmpty()){
            resultMap.put(groupValue,groupDataList);
        }

        return resultMap;
    }

    private static  T getValue(Object v,Class cls){
        return getValue(v,cls,null);
    }

    private static  T getValue(Object v,Class cls,Object defaultValue){
        if(v == null){
            v =  defaultValue;
        }

        if(v == null){
            return null;
        }
        return ConversionUtils.convert(v,cls);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy