Tree.java
4.78 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.jcr.vault.util;
import com.day.jcr.vault.util.Text;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class Tree<E> {
private final char separator;
private Node<E> root = new Node(null, "");
public Tree() {
this('/');
}
public Tree(char separator) {
this.separator = separator;
}
public void clear() {
this.root.elem = null;
this.root.children.clear();
}
public E put(String path, E elem) {
Node<E> n = this.get(path, true);
Object previous = n.elem;
n.elem = elem;
return (E)previous;
}
public E get(String path) {
Node<E> n = this.get(path, false);
return (E)(n == null ? null : n.elem);
}
public Node<E> getNode(String path) {
return this.get(path, false);
}
public E remove(String path) {
Node<E> n = this.get(path, false);
if (n == null) {
return null;
}
Object previous = n.elem;
n.elem = null;
n.prune();
return (E)previous;
}
private Node<E> get(String path, boolean create) {
String[] segs = Text.explode(path, this.separator);
Node n = this.root;
for (String name : segs) {
Node c = n.get(name, create);
if (c == null) {
return null;
}
n = c;
}
return n;
}
public void removeChildren(String path) {
Node<E> n = this.get(path, false);
if (n != null) {
n.removeChildren();
}
}
public Map<String, E> map() {
LinkedHashMap map = new LinkedHashMap();
this.fill(map, this.root, "");
return map;
}
public String getRootPath() {
Node n = this.root;
StringBuffer path = new StringBuffer();
while (n.elem == null && n.children.size() == 1) {
n = (Node)n.children.values().iterator().next();
path.append(this.separator).append(n.name);
}
if (path.length() == 0) {
path.append(this.separator);
}
return path.toString();
}
public Node<E> getRootNode() {
Node n = this.root;
while (n.elem == null && n.children.size() == 1) {
n = (Node)n.children.values().iterator().next();
}
return n;
}
private void fill(Map<String, E> map, Node<E> node, String parentPath) {
String path = parentPath.length() != 1 ? parentPath + this.separator + node.name : parentPath + node.name;
if (node.elem != null) {
map.put(path, (Object)node.elem);
}
for (Node child : node.children.values()) {
this.fill(map, child, path);
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public static class Node<E> {
private String name;
private E elem;
private final Node parent;
private final Map<String, Node<E>> children = new LinkedHashMap<String, Node<E>>();
private Node(Node parent, String name) {
this.parent = parent;
this.name = name;
}
private Node<E> get(String name, boolean create) {
Node<E> child = this.children.get(name);
if (child == null && create) {
child = new Node<E>(this, name);
this.children.put(name, child);
}
return child;
}
private Node<E> remove(String name) {
return this.children.remove(name);
}
private void prune() {
if (this.children.isEmpty() && this.elem == null && this.parent != null) {
this.parent.remove(this.name);
this.parent.prune();
}
}
private void removeChildren() {
this.children.clear();
this.prune();
}
public String getName() {
return this.name;
}
public E getElem() {
return this.elem;
}
public Node getParent() {
return this.parent;
}
public Map<String, Node<E>> getChildren() {
return this.children;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Node");
sb.append("{name='").append(this.name).append('\'');
sb.append(", elem=").append(this.elem);
sb.append(", children=").append(this.children.keySet());
sb.append('}');
return sb.toString();
}
}
}