场景:
权限管理资源分级别:
一级资源(部门管理,项目管理)
二级资源(列表,详情,编辑,删除,增加)
三级资源(过滤条件,展示范围,深度)
也可能有更多级别的资源,现在需要构建一个普通树来存放。
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()); } }