構建一顆普通樹

場景:java

權限管理資源分級別:node

一級資源(部門管理,項目管理)json

二級資源(列表,詳情,編輯,刪除,增長)api

三級資源(過濾條件,展現範圍,深度)測試

也可能有更多級別的資源,如今須要構建一個普通樹來存放。this

 

node類url

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類spa

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); } }

 

測試用例:code

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()); } }
相關文章
相關標籤/搜索