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

com.github.dennisit.vplus.data.criteria.action.TreeAction Maven / Gradle / Ivy

/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
 */
package com.github.dennisit.vplus.data.criteria.action;

import com.github.dennisit.vplus.data.criteria.TreeCriteria;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 树形折叠行为
 * @author Elon.su
 */
public final class TreeAction {

    /**
     * 属性折叠行为
     */
    private TreeAction(){

    }


    /**
     * 递归实现List折叠成Tree
     * @param list 集合
     * @param  泛型约束
     * @return 折叠后的集合对象
     */
    public static  List tree(List list){
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }

        List items = Lists.newArrayList();
        // 抽取根节点
        list.stream().filter((x) -> x.isTreeRoot())
                .forEach((x) -> {
                    items.add(x);
                });

        // 递归组装成tree
        for (T item : items) {
            tree(list, item);
        }
        return items;
    }


    /**
     * Tree形折叠
     * @param list 集合
     * @param t 集合元素
     * @param  泛型约束
     */
    private static  void tree(List list, T t) {
        final String pid = t.getTreeId();
        final List data = Lists.newArrayList();
        list.stream()
                .filter((x) -> StringUtils.isNotBlank(x.getTreeParentId()) && pid.equals(x.getTreeParentId()))
                .forEach((x) -> {
                    data.add(x);
                    // 递归添加
                    tree(list, x);
                });
        if (!data.isEmpty()) {
            t.setSubList(data);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy