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

top.doudou.common.tool.utils.TreeUtils Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package top.doudou.common.tool.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import top.doudou.base.exception.CustomException;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program:
 * @description 将list转换为tree
 * @author: 傻男人<[email protected]>
 * @create: 2019-04-24 11:25
 **/
public class TreeUtils {

    /**
     * 把列表转换为树结构
     *
     * @param originalList      原始list数据
     * @param initParentVal     初始的父的值
     * @param keyName           作为唯一标示的字段名称(就是父与子那个字段关联)
     * @param parentFieldName   父字段
     * @param childrenFieldName 子字段
     * @return 组装后的集合
     */
    public static  List getTree(List originalList, String initParentVal, String keyName, String parentFieldName, String childrenFieldName) {
        if (CollectionUtils.isEmpty(originalList)) {
            return null;
        }
        // 获取根节点,即找出父节点为空的对象
        List topList = new ArrayList<>();
        try {
            for (int i = 0; i < originalList.size(); i++) {
                T t = originalList.get(i);
                String parentId = BeanUtils.getProperty(t, parentFieldName);
                if ((StringUtils.isBlank(initParentVal) && StringUtils.isBlank(parentId)) || parentId.equals(initParentVal)) {
                    topList.add(t);
                }
            }

            // 将根节点从原始list移除,减少下次处理数据
            originalList.removeAll(topList);

            // 递归封装树
            fillTree(topList, originalList, keyName, parentFieldName, childrenFieldName);
            if (CollectionUtils.isNotEmpty(originalList)) {
                topList.addAll(originalList);
            }
        } catch (Exception e) {
            throw new CustomException("封装树失败");
        }
        return topList;
    }

    /**
     * 封装树
     *
     * @param parentList        要封装为树的父对象集合
     * @param originalList      原始list数据
     * @param keyName           作为唯一标示的字段名称
     * @param parentFieldName   模型中作为parent字段名称
     * @param childrenFieldName 模型中作为children的字段名称
     */
    public static  void fillTree(List parentList, List originalList, String keyName, String parentFieldName, String childrenFieldName) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        for (int i = 0; i < parentList.size(); i++) {
            List children = fillChildren(parentList.get(i), originalList, keyName, parentFieldName, childrenFieldName);
            if (children.isEmpty()) {
                continue;
            }
            originalList.removeAll(children);
            fillTree(children, originalList, keyName, parentFieldName, childrenFieldName);
        }
    }

    /**
     * 封装子对象
     *
     * @param parent            父对象
     * @param originalList      待处理对象集合
     * @param keyName           作为唯一标示的字段名称
     * @param parentFieldName   模型中作为parent字段名称
     * @param childrenFieldName 模型中作为children的字段名称
     */
    public static  List fillChildren(T parent, List originalList, String keyName, String parentFieldName, String childrenFieldName) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        List childList = new ArrayList<>();
        String parentId = BeanUtils.getProperty(parent, keyName);
        for (int i = 0; i < originalList.size(); i++) {
            T t = originalList.get(i);
            String childParentId = BeanUtils.getProperty(t, parentFieldName);
            if (parentId.equals(childParentId)) {
                childList.add(t);
            }
        }
        if (!childList.isEmpty()) {
            FieldUtils.writeDeclaredField(parent, childrenFieldName, childList, true);
        }
        return childList;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy