-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTreeMap.java
More file actions
412 lines (325 loc) · 9.36 KB
/
TreeMap.java
File metadata and controls
412 lines (325 loc) · 9.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package org.hy.common;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
/**
* 树结构的集合。
*
* 与 TreeNode 类搭配一起使用。
*
* <O> 为附属对象 TreeMap.TreeNode.info 的类型
*
* @author ZhengWei(HY)
* @version V1.0 2012-10-08
*/
public class TreeMap<O> implements java.io.Serializable
{
private static final long serialVersionUID = 8893165781470163765L;
/**
* 保存树结构信息
* Map.key 为 TreeNode.getOrderByID()
* Map.Value 为 TreeNode 本身
*/
private java.util.TreeMap<String ,TreeNode<O>> treeMap;
/**
* 保存树节点 TreeNode.getNodeID() 值非空的集合信息
*
* Map.key 为 TreeNode.getNodeID()
* Map.Value 为 TreeNode 本身
*/
private Map<String ,TreeNode<O>> nodeIDMap;
public TreeMap()
{
this.treeMap = new java.util.TreeMap<String ,TreeNode<O>>();
this.nodeIDMap = new Hashtable<String ,TreeNode<O>>();
}
/**
* 获取树结构节点的数量
*
* @return
*/
public int size()
{
return this.treeMap.size();
}
/**
* 验证树目录的节点ID是否存在
*
* @param i_NodeID
* @return
*/
public boolean containsNodeID(String i_NodeID)
{
return this.nodeIDMap.containsKey(i_NodeID);
}
/**
* 验证树目录的排序ID是否存在
*
* @param i_OrderByID
* @return
*/
public boolean containsOrderByID(String i_OrderByID)
{
return this.treeMap.containsKey(i_OrderByID);
}
/**
* 构造树结构。即向树结构中添加节点。
*
* 实现同步操作
*
* @param i_NodeInfo
*/
public synchronized void put(TreeNode<O> i_NodeInfo)
{
if ( i_NodeInfo == null )
{
throw new NullPointerException("Tree node is null.");
}
this.treeMap.put(i_NodeInfo.getOrderByID() ,i_NodeInfo);
if ( i_NodeInfo.getNodeID() != null )
{
// ZhengWei(HY) Edit 2016-01-04 删除下的重复不put,改为重复覆盖。
// 当节点的附属对象为空时,先用旧附属对象的实例
if ( null == i_NodeInfo.getInfo() && this.nodeIDMap.containsKey(i_NodeInfo.getNodeID()) )
{
i_NodeInfo.setInfo(this.nodeIDMap.get(i_NodeInfo.getNodeID()).getInfo());
}
this.nodeIDMap.put(i_NodeInfo.getNodeID() ,i_NodeInfo);
}
}
/**
* 按排序OrderByID,删除节点信息
*
* @param i_NodeInfo
* @return
*/
public synchronized TreeNode<O> remove(String i_OrderByID)
{
return this.remove(this.get(i_OrderByID));
}
/**
* 按节点NodeID,删除节点信息
*
* @param i_NodeInfo
* @return
*/
public synchronized TreeNode<O> removeNodeID(String i_NodeID)
{
return this.remove(this.getByNodeID(i_NodeID));
}
/**
* 删除节点信息
*
* @param i_NodeInfo
* @return
*/
public synchronized TreeNode<O> remove(TreeNode<O> i_NodeInfo)
{
if ( i_NodeInfo == null || i_NodeInfo.getOrderByID() == null )
{
return null;
}
else if ( this.treeMap.containsKey(i_NodeInfo.getOrderByID()) )
{
this.treeMap.remove(i_NodeInfo.getOrderByID());
if ( i_NodeInfo.getNodeID() != null )
{
if ( this.nodeIDMap.containsKey(i_NodeInfo.getNodeID()) )
{
try
{
this.nodeIDMap.remove(i_NodeInfo.getNodeID());
}
catch (Exception exce)
{
// 如果删除异常,则重新put进集合
this.treeMap.put(i_NodeInfo.getOrderByID() ,i_NodeInfo);
return null;
}
}
}
}
else
{
return null;
}
return i_NodeInfo;
}
/**
* 按树结构的排序ID获取对应的节点信息
*
* @param i_OrderByID
* @return
*/
public TreeNode<O> get(String i_OrderByID)
{
if ( i_OrderByID == null )
{
return null;
}
else if ( this.treeMap.containsKey(i_OrderByID) )
{
return this.treeMap.get(i_OrderByID);
}
else
{
return null;
}
}
/**
* 按树节点的NodeID获取对应的节点信息
*
* @param i_NodeID
* @return
*/
public TreeNode<O> getByNodeID(String i_NodeID)
{
if ( i_NodeID == null )
{
return null;
}
else if ( this.nodeIDMap.containsKey(i_NodeID) )
{
return this.nodeIDMap.get(i_NodeID);
}
else
{
return null;
}
}
/**
* 获取树目录的 orderByID 的迭代器
*
* @return
*/
public Iterator<String> keySet()
{
return this.treeMap.keySet().iterator();
}
/**
* 获取树目录的 nodeID 的迭代器
*
* @return
*/
public Iterator<String> keySetNodeID()
{
return this.nodeIDMap.keySet().iterator();
}
/**
* 获取树目录节点 TreeNode 的迭代器
*
* @return
*/
public Iterator<TreeNode<O>> values()
{
return this.treeMap.values().iterator();
}
/**
* 获取树目录节点,相关有 nodeID 值的 TreeNode 的迭代器
*
* @return
*/
public Iterator<TreeNode<O>> valuesNodeID()
{
return this.nodeIDMap.values().iterator();
}
/**
* 按树节点NodeID,获取对应节点的子树结构
*
* @param i_SuperNode_NodeID
* @return
*/
public TreeMap<O> getChildTreeByNodeID(String i_SuperNode_NodeID)
{
return this.getChildTree(this.getByNodeID(i_SuperNode_NodeID));
}
/**
* 按树结构的排序ID,获取对应节点的子树结构
*
* @param i_SuperNode_NodeID
* @return
*/
public TreeMap<O> getChildTree(String i_SuperNode_OrderByID)
{
return this.getChildTree(this.get(i_SuperNode_OrderByID));
}
/**
* 按树节点对象,获取对应节点的子树结构
*
* @param i_SuperNode_NodeID
* @return
*/
public TreeMap<O> getChildTree(TreeNode<O> i_SuperNode)
{
if ( i_SuperNode == null )
{
throw new NullPointerException("Super node is null.");
}
TreeMap<O> v_Ret = new TreeMap<O>();
SortedMap<String ,TreeNode<O>> v_TailMap = this.treeMap.tailMap(i_SuperNode.getOrderByID());
Iterator<TreeNode<O>> v_Iterator = v_TailMap.values().iterator();
boolean v_IsGoOn = true;
while ( v_IsGoOn && v_Iterator.hasNext() )
{
TreeNode<O> v_NodeInfo = v_Iterator.next();
if ( i_SuperNode.getLevel() < v_NodeInfo.getLevel() )
{
v_Ret.put(v_NodeInfo);
}
else
{
if ( v_Ret.size() >= 1 )
{
v_IsGoOn = false;
}
}
}
return v_Ret;
}
/**
* 清空树结构中的所有节点信息
*/
public void clear()
{
this.treeMap.clear();
this.nodeIDMap.clear();
}
@Override
public String toString()
{
StringBuilder v_Buffer = new StringBuilder();
Iterator<TreeNode<O>> v_Iterator = this.treeMap.values().iterator();
while ( v_Iterator.hasNext() )
{
TreeNode<O> v_TreeNode = v_Iterator.next();
for (int v_Level=1; v_Level<=v_TreeNode.getLevel(); v_Level++)
{
if ( v_Level == v_TreeNode.getLevel() )
{
v_Buffer.append("|--- ");
}
else
{
v_Buffer.append("| ");
}
}
//v_Buffer.append(v_TreeNode.getOrderByID()).append(" ");
v_Buffer.append("[").append(v_TreeNode.getNodeID()).append("] ");
v_Buffer.append(v_TreeNode.getInfo().toString());
v_Buffer.append(Help.getSysLineSeparator());
}
return v_Buffer.toString();
}
/*
ZhengWei(HY) Del 2016-07-30
不能实现这个方法。首先JDK中的Hashtable、ArrayList中也没有实现此方法。
它会在元素还有用,但集合对象本身没有用时,释放元素对象
一些与finalize相关的方法,由于一些致命的缺陷,已经被废弃了
protected void finalize() throws Throwable
{
this.clear();
super.finalize();
}
*/
}