zoukankan      html  css  js  c++  java
  • 构建一颗普通树

    场景:

    权限管理资源分级别:

    一级资源(部门管理,项目管理)

    二级资源(列表,详情,编辑,删除,增加)

    三级资源(过滤条件,展示范围,深度)

    也可能有更多级别的资源,现在需要构建一个普通树来存放。

    node类

    package com.haizhi.gap.sys.auth.model.vo;
    
    
    import com.haizhi.gap.sys.auth.model.po.SysResourcePo;
    import io.swagger.annotations.ApiModelProperty;
    
    import java.util.LinkedList;
    
    /**
     * Created by tanghaiyang on 2019/1/8.
     */
    public class SysResourceNodeVo {
    
        @ApiModelProperty(value = "资源ID", example = "0")
        public Long id;
    
        @ApiModelProperty(value = "用户父ID", example = "8")
        public int parentId;
    
        @ApiModelProperty(value = "资源名", example = "列表")
        public String name;
    
        @ApiModelProperty(value = "资源url", example = "http://xxxx:xx/api/xx")
        public String url;
    
        @ApiModelProperty(value = "资源级别", example = "一级资源")
        public String remark;
    
        public LinkedList<SysResourceNodeVo> childList;
    
        public SysResourceNodeVo(Long id, int parentId, String name, String remark) {
            this.id = id;
            this.parentId = parentId;
            this.name = name;
            this.remark = remark;
        }
    
        public SysResourceNodeVo(SysResourcePo po) {
            this.id = po.getId();
            this.parentId = po.getParentId();
            this.name = po.getName();
            this.remark = po.getRemark();
        }
    
        public boolean add(SysResourceNodeVo sysResourceNodeVo){
            if (childList == null) {
                childList = new LinkedList<>();
            }
            return childList.add(sysResourceNodeVo);
        }
    
    }

    Tree类

    package com.haizhi.gap.sys.auth.model.vo;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    
    import java.util.Objects;
    
    /**
     * Created by tanghaiyang on 2019/1/8.
     */
    public class SysResourceTreeVo {
    
        private SysResourceNodeVo root;
    
        public SysResourceTreeVo() {
            root = null;
        }
    
        public void insertNode(SysResourceNodeVo childSysResourceNodeVo) {
            if (root == null) {
                root = new SysResourceNodeVo(0L, 0, "root", "根节点");
            }
            SysResourceNodeVo currentSysResourceNodeVo = root;
            addNode(currentSysResourceNodeVo, childSysResourceNodeVo);
        }
    
        private boolean addNode(SysResourceNodeVo currentSysResourceNodeVo, SysResourceNodeVo sysResourceNodeVo) {
            if( currentSysResourceNodeVo.id == sysResourceNodeVo.parentId ){
                currentSysResourceNodeVo.add(sysResourceNodeVo);
                System.out.println("add sysResourceNodeVo: " + sysResourceNodeVo.id);
                return true;
            }
    
            if (Objects.nonNull(currentSysResourceNodeVo.childList)){
                for(int i = 0; i< currentSysResourceNodeVo.childList.size(); i++){
                    System.out.println(currentSysResourceNodeVo.childList);
                    SysResourceNodeVo child  = currentSysResourceNodeVo.childList.get(i);
                    addNode(child, sysResourceNodeVo);
                }
            }
    
            return false;
        }
    
    
        public String toString(){
            return JSON.toJSONString(root, SerializerFeature.PrettyFormat);
        }
    }

    测试用例:

    package com.haizhi.gap.sys.auth.model.vo;
    
    /**
     * Created by tanghaiyang on 2019/1/8.
     */
    public class ResourceTreeTest {
    
        public static void main(String[] args) {
    
            SysResourceNodeVo sysResourceNodeVo1 = new SysResourceNodeVo(11L, 0, "dddd", "一级资源");
            SysResourceNodeVo sysResourceNodeVo2 = new SysResourceNodeVo(12L, 11, "dddd", "一级资源");
            SysResourceNodeVo sysResourceNodeVo3 = new SysResourceNodeVo(13L, 12, "dddd", "二级资源");
            SysResourceNodeVo sysResourceNodeVo4 = new SysResourceNodeVo(14L, 12, "dddd", "二级资源");
            SysResourceNodeVo sysResourceNodeVo5 = new SysResourceNodeVo(15L, 14, "dddd", "三级资源");
    
            SysResourceTreeVo resourceTree = new SysResourceTreeVo();
    
            resourceTree.insertNode(sysResourceNodeVo1);
            resourceTree.insertNode(sysResourceNodeVo2);
            resourceTree.insertNode(sysResourceNodeVo3);
            resourceTree.insertNode(sysResourceNodeVo4);
            resourceTree.insertNode(sysResourceNodeVo5);
    
            System.out.println(resourceTree.toString());
        }
    
    
    }
  • 相关阅读:
    第一章:Android系统的编译和移植实例读书笔记
    第二章:Android系统与嵌入式开发读书笔记
    第三章:Android移植平台工具介绍读书笔记
    第十章 嵌入式Linux的调试技术
    第九章 硬件抽象层:HAL
    第八章 让开发板发出声音:蜂鸣器驱动
    第七章 LED将为我闪烁:控制发光二极管
    第六章 第一个Linux驱动程序:统计单词个数
    第五章 搭建S3C6410开发板的测试环境
    第四章 源代码的下载和编译
  • 原文地址:https://www.cnblogs.com/geektcp/p/10240825.html
Copyright © 2011-2022 走看看