文件编辑器 3.做出复合节点

复合节点没有自己的size,offset而是取其中所有子节点的∑size为size,第一个子元素的offset作为自己的offset,剩下的交给迭代,也就findNode还有点代码,其它的就没什么了


  0: package editfile;
  1:
  2: import java.util.Iterator;
  3: import java.util.List;
  4:
  5: public class ComboNode extends BaseNode {
  6:
  7:     List<INode> subList = null;
  8:
  9:     public ComboNode() {

10:         super();
11:     }
12:
13:     public ComboNode(String name, List<INode> subList) {
14:         super(name, 0, 0);
15:         this.subList = subList;
16:     }
17:
18:     @Override
19:     public int getSize() {
20:         if (this.subList != null && this.subList.size() > 0) {
21:             int size = 0;
22:             for (INode s : this.subList) {
23:                 size += s.getSize();
24:             }
25:             return size;
26:         }
27:         return 0;
28:     }
29:
30:     /*
31:      * (non-Javadoc)
32:      *
33:      * @see editfile.BaseNode#getOffset()
34:      */
35:     @Override
36:     public long getOffset() {
37:         if (this.subList != null && this.subList.size() > 0) {
38:             return this.subList.get(0).getOffset();
39:         }
40:         return 0;
41:     }
42:
43:     /*
44:      * (non-Javadoc)
45:      *
46:      * @see editfile.BaseNode#findNode(java.lang.String)
47:      */
48:     @Override
49:     public INode findNode(String name) {
50:         if (name == null) {
51:             return null;
52:         }
53:         int spliterIndex = name.indexOf(RawFile.getPathSpliter());
54:         String findName = name;
55:         if (spliterIndex > 0) {
56:             findName = name.substring(0, spliterIndex);
57:         }
58:         INode realSubNode = null;
59:         for (INode subNode : this.subList) {
60:             if (findName.equals(subNode.getName())) {
61:                 if (spliterIndex < 0) {
62:                     return subNode;
63:                 } else {
64:                     realSubNode = subNode.findNode(name.substring(spliterIndex + 1));
65:                     if (realSubNode != null) {
66:                         return realSubNode;
67:                     }
68:                 }
69:             }
70:         }
71:         return null;
72:     }
73:
74:     /*
75:      * (non-Javadoc)
76:      *
77:      * @see editfile.BaseNode#getNodeType()
78:      */
79:     @Override
80:     public NodeType getNodeType() {
81:         return NodeType.combo;
82:     }
83:
84:     /*
85:      * (non-Javadoc)
86:      *
87:      * @see editfile.BaseNode#toString()
88:      */
89:     @Override
90:     public String toString() {
91:         StringBuffer sb = new StringBuffer();
92:         sb.append(super.toString());
93:         if (this.subList != null) {
94:             for (INode node : this.subList) {
95:                 sb.append("/n/t").append(node.toString());
96:             }
97:         }
98:         return sb.toString();
99:     }
100:
101:     @Override
102:     public Iterator<INode> iterator() {
103:         return new Iterator<INode>() {
104:             int index = 0;
105:
106:             @Override
107:             public boolean hasNext() {
108:                 return this.index < ComboNode.this.subList.size();
109:             }
110:
111:             @Override
112:             public INode next() {
113:                 return ComboNode.this.subList.get(this.index++);
114:             }
115:
116:             @Override
117:             public void remove() {
118:                 ComboNode.this.subList.remove(this.index);
119:             }
120:         };
121:     }
122:
123: }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值