1
|
|
package net.sf.flock.tree;
|
2
|
|
|
3
|
|
import java.util.Collection;
|
4
|
|
import java.util.Iterator;
|
5
|
|
import java.util.Set;
|
6
|
|
import java.util.StringTokenizer;
|
7
|
|
import java.util.TreeSet;
|
8
|
|
|
9
|
|
import org.apache.log4j.LogManager;
|
10
|
|
import org.apache.log4j.Logger;
|
11
|
|
|
12
|
|
class BaseTreeNode implements ITreeNode, Comparable {
|
13
|
|
|
14
|
|
private final static Logger LOGGER = LogManager.getLogger(BaseTreeNode.class);
|
15
|
|
|
16
|
|
private final String id;
|
17
|
|
private ITreeNode parent;
|
18
|
|
private Set children = new TreeSet();
|
19
|
|
|
20
|
1
|
public BaseTreeNode(String id) {
|
21
|
1
|
this.id = id;
|
22
|
|
}
|
23
|
|
|
24
|
0
|
public ITreeNode getParent() {
|
25
|
0
|
return this.parent;
|
26
|
|
}
|
27
|
|
|
28
|
0
|
public void setParent(ITreeNode parent) {
|
29
|
0
|
if (this.parent!=null) {
|
30
|
0
|
throw new IllegalStateException("Parent node already set");
|
31
|
|
}
|
32
|
0
|
this.parent = parent;
|
33
|
|
}
|
34
|
|
|
35
|
|
/**
|
36
|
|
* @see net.sf.flock.tree.ITreeNode#getId()
|
37
|
|
*/
|
38
|
1
|
public String getId() {
|
39
|
1
|
return this.id;
|
40
|
|
}
|
41
|
|
|
42
|
|
/**
|
43
|
|
* @see net.sf.flock.tree.ITreeNode#getFullPath()
|
44
|
|
*/
|
45
|
0
|
public String getFullPath() {
|
46
|
0
|
return (this.parent!=null ? this.parent.getFullPath()+"/" : "") + this.getId();
|
47
|
|
}
|
48
|
|
|
49
|
|
|
50
|
|
/**
|
51
|
|
* @see net.sf.flock.tree.ITreeNode#getChildren()
|
52
|
|
*/
|
53
|
0
|
public Collection getChildren() {
|
54
|
0
|
return this.children;
|
55
|
|
}
|
56
|
|
|
57
|
1
|
public BaseTreeNode getChild(String id) {
|
58
|
1
|
for (Iterator i=this.children.iterator(); i.hasNext(); ) {
|
59
|
0
|
BaseTreeNode node = (BaseTreeNode)i.next();
|
60
|
0
|
if (id.equals(node.getId())) {
|
61
|
0
|
return node;
|
62
|
|
}
|
63
|
|
}
|
64
|
1
|
return null;
|
65
|
|
}
|
66
|
|
|
67
|
0
|
public void addChild(BaseTreeNode child) {
|
68
|
0
|
child.setParent(this);
|
69
|
0
|
this.children.add(child);
|
70
|
|
}
|
71
|
|
|
72
|
|
|
73
|
0
|
public boolean equals(Object o) {
|
74
|
0
|
if (o instanceof ITreeNode) {
|
75
|
0
|
return this.id.equals( ((ITreeNode)o).getId() );
|
76
|
|
}
|
77
|
0
|
return false;
|
78
|
|
}
|
79
|
|
|
80
|
0
|
public int compareTo(Object o) {
|
81
|
0
|
return this.id.compareTo( ((ITreeNode)o).getId() );
|
82
|
|
}
|
83
|
|
|
84
|
|
|
85
|
1
|
public BaseTreeNode getChild(String path, boolean create) {
|
86
|
1
|
LOGGER.debug("getCategoryByPath " + path + " create:" + create);
|
87
|
|
|
88
|
1
|
if (!path.startsWith("/")) {
|
89
|
0
|
throw new IllegalArgumentException("Illegal path specified:" + path);
|
90
|
|
}
|
91
|
|
|
92
|
1
|
path = path.substring(1);
|
93
|
1
|
BaseTreeNode currNode = this;
|
94
|
1
|
for (StringTokenizer pathTokens = new StringTokenizer(path, "/"); pathTokens.hasMoreTokens(); ) {
|
95
|
|
|
96
|
1
|
String id = pathTokens.nextToken();
|
97
|
|
|
98
|
1
|
LOGGER.debug("-" + currNode.getId() + "->" + id);
|
99
|
|
|
100
|
1
|
BaseTreeNode childNode = currNode.getChild(id);
|
101
|
|
|
102
|
0
|
LOGGER.debug("- childNode : " + childNode);
|
103
|
|
|
104
|
0
|
if (childNode == null) {
|
105
|
0
|
if (create) {
|
106
|
0
|
LOGGER.debug("- create child: " + id);
|
107
|
|
// create
|
108
|
0
|
childNode = new BaseTreeNode(id);
|
109
|
0
|
currNode.addChild(childNode);
|
110
|
|
} else {
|
111
|
0
|
LOGGER.debug("- child not found[" + id + "] `return null`");
|
112
|
0
|
return null;
|
113
|
|
}
|
114
|
|
}
|
115
|
0
|
currNode = childNode;
|
116
|
|
}
|
117
|
0
|
LOGGER.debug("- return node " + currNode);
|
118
|
|
|
119
|
0
|
return currNode;
|
120
|
|
}
|
121
|
|
|
122
|
|
}
|
123
|
|
|