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

io.github.davidchild.bitter.bitterlist.BMap Maven / Gradle / Ivy

package io.github.davidchild.bitter.bitterlist;

import com.alibaba.fastjson2.JSONObject;
import io.github.davidchild.bitter.tools.CoreStringUtils;
import io.github.davidchild.bitter.tools.JsonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BMap extends ArrayList> {

    private Class myType;

    static String snakeToCamel(String str) {
        StringBuilder builder = new StringBuilder(str);
        for (int i = 0; i < builder.length(); i++) {
            if (builder.charAt(i) == '_') {

                builder.deleteCharAt(i);
                builder.replace(i, i + 1, String.valueOf(Character.toUpperCase(builder.charAt(i))));
            }
        }
        return builder.toString();
    }

    public Class getType() {
        return myType;
    }

    public void setType(Class type) {
        this.myType = type;
    }

    public  R tryCase(R v) {
        if (this.size() <= 0)
            return v;
        Object myValue = v;
        Class clazz = v.getClass();
        Map map = this.get(0);
        Object myv = map.values().stream().findFirst().orElse(null);
        if (CoreStringUtils.isNotNull(myv)) {
            if (java.lang.Double.class.equals(clazz)) {
                myValue = java.lang.Double.parseDouble(myv.toString());
            } else if (java.lang.Integer.class.equals(clazz)) {
                myValue = java.lang.Integer.parseInt(myv.toString());
            } else if (java.lang.String.class.equals(clazz)) {
                myValue = myv.toString();
            } else if (java.lang.Float.class.equals(clazz)) {
                myValue = java.lang.Float.parseFloat(myv.toString());
            } else if (java.lang.Short.class.equals(clazz)) {
                myValue = java.lang.Short.parseShort(myv.toString());
            } else if (java.lang.Boolean.class.equals(clazz)) {
                myValue = java.lang.Boolean.getBoolean(myv.toString());
            }
        }
        return (R) myValue;
    }

    // if List size<=0 return null
    public Object getFirstRowSomeData(String key) {
        if (this.size() <= 0)
            return null;
        Map map = this.get(0);
        return map.get(key);
    }

    public Object getFirstRowData(String key, Object object) {
        if (this.size() <= 0)
            return null;
        Map map = this.get(0);
        return map.getOrDefault(key, object);
    }

    public  BList toBList(Class clazz, boolean mapUnderscoreToCamelCase) {
        if (this.size() <= 0) {
            return new BList<>(clazz);
        }
        List> finalTempList;
        if (mapUnderscoreToCamelCase) {
            List> finalTempList1 = new ArrayList<>();
            this.forEach(item -> {
                Map temp = new HashMap<>();
                item.forEach((key, value) -> temp.put(snakeToCamel(key), value));
                finalTempList1.add(temp);
            });
            finalTempList = finalTempList1;
        } else {
            finalTempList = this;
        }

        BList list = new BList<>(clazz);
        finalTempList.forEach(item -> {
            String str = JsonUtil.object2String(item);
            OT ob = JSONObject.parseObject(str, clazz);
            list.add(ob);
        });
        return list;
    }

    public  BList toBList(Class clazz) {
        if (this.size() <= 0) {
            return new BList<>(clazz);
        }
        BList list = new BList<>(clazz);
        this.forEach(item -> {
            String str = JsonUtil.object2String(item);
            OT ob = JsonUtil.string2Object(str, clazz);
            list.add(ob);
        });
        return list;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy