Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3657274
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Java

2021-06-25 17:18:42

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.function.BiConsumer;

import java.util.function.Function;

import java.util.function.Supplier;

import java.util.stream.Collectors;

/**

 * @program: myUtil

 * @description: 工具类 - 树状结构生成

 * @author: syf

 * @create: 2021-06-15 10:38

 */

public class TreeModel {

    /**

     * 上级字段名称

     */

    private Function superColumn;

    /**

     * 当前比对字段

     */

    private Function column;

    /**

     * 列表接收字段名

     */

    private BiConsumer> children;

    /**

     * 数据列表

     */

    private List list;

    /**

     * 数据解除关联性使用的对象

     */

    private Supplier decoupling;

    /**

     * 私有化工具类

     */

    private TreeModel() {

    }

    /**

     * 初始化载入树状工具类

     *

     * @param list        需要整合的数据列表

     * @param superColumn 上级字段(对象::字段get方法)

     * @param column      本级字段(对象::字段get方法)

     * @param children    下级数据存储字段(对象::字段set方法)

     * @return 树状工具类

     */

    public static TreeModel load(List list, Function superColumn,

                                        Function column, BiConsumer> children) {

        TreeModel treeModel = new TreeModel<>();

        treeModel.setSuperColumn(superColumn)

                .setColumn(column)

                .setChildren(children)

                .setList(list);

        return treeModel;

    }

    /**

     * 获取树状数据(不解除原集合中对象与树状集合对象的关联)

     *

     * @param initValue 根数据

     * @return 树状数据数据列表

     */

    public List getTree(Object initValue) {

        List tree = treeAll(initValue);

        if (tree != null && tree.size() > 0) {

            return tree.stream().filter(ls -> initValue.equals(superColumn.apply(ls))).collect(Collectors.toList());

        }

        return tree;

    }

    /**

     * 获取树状数据(解除原集合中对象与树状集合对象的关联)

     *

     * @param initValue  根数据

     * @param decoupling 解除关联对象(对象::new

     * @return 树状数据数据列表

     */

    public List getTree(Object initValue, Supplier decoupling) {

        this.setDecoupling(decoupling);

        return tree(initValue);

    }

    /**

     * 获取树状数据

     *

     * @param initValue 根数据

     * @return 树状数据

     */

    private List treeAll(Object initValue) {

        if (list == null || list.size() < 1) {

            return list;

        }

        List collect = list.stream().filter(f -> initValue.equals(superColumn.apply(f))).collect(Collectors.toList());

        if (collect.size() < 1) {

            return null;

        }

        collect.forEach(c -> children.accept(c, treeAll(column.apply(c))));

        return collect;

    }

    private List tree(Object o) {

        List childrenList = new ArrayList<>();

        if(o == null){

            return childrenList;

        }

        for (T entity : list) {

            if (o.equals(superColumn.apply(entity))) {

                T now = decoupling.get();

                copy(entity, now);

                childrenList.add(now);

            }

        }

        for (T cs : childrenList) {

            children.accept(cs, tree(column.apply(cs)));

        }

        return childrenList;

    }

    /**

     * source中的属性赋值给target

     * @param source    数据来源类

     * @param target    数据接收类

     */

    private void copy(T source, T target) {

        if (source == null) {

            throw new NullPointerException("dataSource");

        }

        if (target == null) {

            target = decoupling.get();

        }

        Field[] sourceFields = source.getClass().getDeclaredFields();

        Field[] targetFields = target.getClass().getDeclaredFields();

        Map sourceMap = new HashMap<>(sourceFields.length);

        try {

            for (Field field : sourceFields) {

                if (Modifier.isFinal(field.getModifiers())) {

                    continue;

                }

                field.setAccessible(true);

                sourceMap.put(field.getName(), field.get(source));

            }

            for (Field field : targetFields) {

                Object o = sourceMap.get(field.getName());

                if (o == null) {

                    continue;

                }

                field.setAccessible(true);

                field.set(target, o);

            }

        } catch (IllegalAccessException e) {

            e.printStackTrace();

        }

    }

    private TreeModel setSuperColumn(Function superColumn) {

        if (superColumn == null) {

            throw new NullPointerException("superColumn");

        }

        this.superColumn = superColumn;

        return this;

    }

    private TreeModel setColumn(Function column) {

        if (column == null) {

            throw new NullPointerException("column");

        }

        this.column = column;

        return this;

    }

    private TreeModel setChildren(BiConsumer> children) {

        if (children == null) {

            throw new NullPointerException("children");

        }

        this.children = children;

        return this;

    }

    private void setList(List list) {

        if (list == null || list.size() < 1) {

            throw new NullPointerException("list");

        }

        this.list = list;

    }

    private void setDecoupling(Supplier decoupling) {

        if (decoupling == null) {

            throw new NullPointerException("decoupling");

        }

        this.decoupling = decoupling;

    }

}

阅读(3901) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~