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

cn.kduck.core.utils.BeanDefUtils 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.dao.definition.BeanFieldDef;
import cn.kduck.core.dao.sqlbuilder.AliasField;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字段定义工具类
 * @author  LiuHG
 */
public final class BeanDefUtils {

    private BeanDefUtils(){}

    public static AliasField getAliasField(List fieldList, String attrNames){
        for (AliasField field : fieldList) {
            BeanFieldDef fieldDef = field.getFieldDef();
            if(fieldDef.getAttrName().equals(attrNames)){
                return field;
            }
        }
        return null;
    }

    public static List includeAliasField(List fieldList, String... attrNames){
        List resultList = new ArrayList<>();
        for (int i = 0; i < fieldList.size(); i++) {
            AliasField aliasField = fieldList.get(i);
            BeanFieldDef beanFieldDef = aliasField.getFieldDef();
            for (String name : attrNames) {
                if(beanFieldDef.getAttrName().equals(name) && !resultList.contains(beanFieldDef)){
                    resultList.add(aliasField);
                }
            }
        }
        return resultList;
    }

    public static List includeField(List fieldList, String... attrNames){
//        return fieldList.stream().filter(f -> {
//            for (String name : attrNames) {
//                if(f.getAttrName().equals(name)){
//                    return true;
//                }
//            }
//            return false;
//        }).collect(Collectors.toList());

        List resultList = new ArrayList<>();
        for (int i = 0; i < fieldList.size(); i++) {
            BeanFieldDef beanFieldDef = fieldList.get(i);
            for (String name : attrNames) {
                if(beanFieldDef.getAttrName().equals(name) && !resultList.contains(beanFieldDef)){
                    resultList.add(beanFieldDef);
                }
            }
        }
        return resultList;
    }

    /**
     * 将提供的字段集合进行拼装,按照字段名判重,返回一个包含所有字段的集合,相同字段名的字段定义只会包含一个。
     * @param allFieldList
     * @return 返回一个包含所有字段的集合
     */
    public static List join(List... allFieldList){
        List resultFieldList = new ArrayList();
        for (List fieldList : allFieldList) {
            for (BeanFieldDef field : fieldList) {
                if(!existField(resultFieldList,field)){
                    resultFieldList.add(field);
                }
            }
        }
        return resultFieldList;
    }

    private static boolean existField(List fieldList ,BeanFieldDef field){
        for (BeanFieldDef f : fieldList){
            if(f.getFieldName().equals(field.getFieldName())){
                return true;
            }
        }
        return false;
    }

    public static List excludeAliasField(List aliasFieldList, String... excludeAttrNames){
        return aliasFieldList.stream().filter(f -> {
            String attrName = f.getFieldDef().getAttrName();
            return !StringUtils.contain(excludeAttrNames,attrName);
        }).collect(Collectors.toList());
    }

    public static List excludeField(List fieldList, String... excludeAttrNames){
        return fieldList.stream().filter(f -> {
            String attrName = f.getAttrName();
            return !StringUtils.contain(excludeAttrNames,attrName);
        }).collect(Collectors.toList());
    }

    public static BeanFieldDef getByColName(List fieldList, String columnName){
        for (BeanFieldDef fieldDef:fieldList) {
            if (fieldDef.getFieldName().equalsIgnoreCase(columnName)) {
                return fieldDef;
            }
        }
        return null;
    }

    public static BeanFieldDef getByAttrName(List fieldList, String attrName){
        for (BeanFieldDef fieldDef:fieldList) {
            if (fieldDef.getAttrName().equals(attrName)) {
                return fieldDef;
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy