java 目录结构_基础入门:基于Java实现的多层目录结构

nodeStore;

8

9 /**

10 * 将平面的node list构建成树

11 *

12 * @throws TreeException

13 */

14 public void build() throws TreeException {

15 Listnodes = nodeStore.findByType(type);

16 // 如果nodes为空,则不做任何处理

17 if (nodes == null || nodes.isEmpty()) {

18 return;

19 }

20 // 设置root和node持有器

21 for (T node : nodes) {

22 if (node.isRootNode()) {

23 this.root = node;

24 }

25 nodeHolder.put(node.getId(), node);

26 }

27

28 // 如果root为空,则build失败了

29 if (root == null) {

30 throw new TreeException("no root node found.");

31 }

32

33 // 建立节点之前关系

34 for (T node : nodes) {

35 if (node.isRootNode()) {

36 continue;

37 }

38 Node parent = getNodeById(node.getParentId());

39 if (parent == null) {

40 throw new TreeException("no parent node found.current node id is:" + node.getId());

41 }

42 parent.addChild(node);

43 }

44

45 // 排序

46 root.sortAllChidren();

47 }

48

49 /**

50 * 得到root节点

51 *

52 * @return

53 */

54 public Node getRoot() {

55 return root;

56 }

57

58 /**

59 * 根据id得到对应节点

60 *

61 * @param id

62 * @return

63 */

64 public Node getNodeById(Integer id) {

65 return nodeHolder.get(id);

66 }

67

68 public void setType(String type) {

69 this.type = type;

70 }

71

72 public void setNodeStore(NodeStore nodeStore) {

73 this.nodeStore = nodeStore;

74 }

75

76 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值