Source code: openfuture/util/misc/HashTree.java
1 package openfuture.util.misc;
2 /*
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.<p>
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.<p>
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br>
16 * http://www.gnu.org/copyleft/lesser.html
17 */
18
19 import java.io.PrintStream;
20 import java.util.Collection;
21 import java.util.Enumeration;
22 import java.util.Hashtable;
23 import java.util.Iterator;
24
25 // Configuration Management Information:
26 // -------------------------------------
27 // $Id: HashTree.java,v 1.1.1.1 2001/07/08 18:29:32 wreissen Exp $
28 //
29 // Version History:
30 // ----------------
31 // $Log: HashTree.java,v $
32 // Revision 1.1.1.1 2001/07/08 18:29:32 wreissen
33 // initial version registered ad SourceForge
34 //
35 //
36 // ***********************************************************************************
37 /**
38 * Tree using a {@link java.util.Hashtable} in order to store the
39 * child nodes.<p>
40 *
41 *
42 * Created: Sun May 13 17:13:10 2001
43 *
44 * @author <a href="mailto:wolfgang@openfuture.de">Wolfgang Reissenberger</a>
45 * @version $Revision: 1.1.1.1 $
46 */
47
48 public class HashTree {
49
50 /**
51 * String separating a list of elements on a path.
52 *
53 */
54 public static final String PATH_SEPARATOR = "/";
55 /**
56 * Name of the node.
57 *
58 */
59 String name;
60 /**
61 * Hashtable holding all children. The
62 * {@link openfuture.util.misc.HashTree#getName() name}
63 * of the node is the key in the Hashtable.
64 *
65 */
66 Hashtable children;
67
68 /**
69 * Parent node
70 *
71 */
72 HashTree parent;
73
74 /**
75 * Creates a new <code>HashTree</code> instance.
76 *
77 * @param name Node name.
78 */
79 public HashTree(String name) {
80 setChildren(new Hashtable());
81 setName(name);
82 setParent(null);
83 }
84
85
86 /**
87 * Creates a new <code>HashTree</code> instance.
88 *
89 * @param path array of node names.
90 */
91 public HashTree(String[] path) {
92 this(path[0]);
93
94 HashTree current = this;
95
96 for (int i = 1; i < path.length; i++) {
97 HashTree child = new HashTree(path[i]);
98 current.addChild(child);
99 current = child;
100 }
101 }
102
103
104 /**
105 * Get the value of name.
106 * @return value of name.
107 */
108 public String getName() {
109 return name;
110 }
111
112 /**
113 * Set the value of name.
114 * @param v Value to assign to name.
115 */
116 public void setName(String v) {
117 this.name = v;
118 }
119
120
121 /**
122 * Get the value of children.
123 * @return value of children.
124 */
125 protected Hashtable getChildren() {
126 return children;
127 }
128
129 /**
130 * Set the value of children.
131 * @param v Value to assign to children.
132 */
133 protected void setChildren(Hashtable v) {
134 this.children = v;
135 }
136
137
138 /**
139 * Get the value of parent.
140 * @return value of parent.
141 */
142 public HashTree getParent() {
143 return parent;
144 }
145
146 /**
147 * Set the value of parent.
148 * @param v Value to assign to parent.
149 */
150 public void setParent(HashTree v) {
151 this.parent = v;
152 }
153
154
155 /**
156 * Retrieve the child with the given name.
157 *
158 * @param name name of the child
159 * @return child node with the given name, <code>null</code>
160 * if no child exists.
161 */
162 public HashTree getChild(String name) {
163 return((HashTree) getChildren().get(name));
164 }
165
166 /**
167 * Check, if a child with the given name exists.
168 *
169 * @param name name of the child
170 * @return <code>true</code>, if this node has a child
171 * with the given name
172 */
173 public boolean hasChild(String name) {
174 return(getChildren().get(name) != null);
175 }
176
177
178 /**
179 * Returns the set of {@link openfuture.util.misc.HashTree children}.
180 *
181 * @return the set of child nodes
182 */
183 public Collection children() {
184 return (getChildren().values());
185 }
186
187
188 /**
189 * Check, if this node has children.
190 *
191 * @return true, if this node does not have any child nodes.
192 */
193 public boolean isLeaf() {
194 return(getChildren().isEmpty());
195 }
196
197
198 /**
199 * Add a {@link openfuture.util.misc.HashTree HashTree} below as
200 * child. If there exists already a child node with the same name,
201 * their subtrees are merged.
202 *
203 * @param child a <code>HashTree</code> value
204 */
205 public void addChild(HashTree child) {
206 Hashtable children = getChildren();
207 if (children.containsKey(child.getName())) {
208 HashTree existingChild = (HashTree) children.get(child.getName());
209 Hashtable grandchildren = child.getChildren();
210
211 Enumeration elements = grandchildren.elements();
212 while(elements.hasMoreElements()) {
213 existingChild.addChild((HashTree) elements.nextElement());
214 }
215 } else {
216 children.put(child.getName(), child);
217 child.setParent(this);
218 }
219 }
220
221
222 /**
223 * Dump the tree to the given print stream.
224 *
225 * @param stream the tree should be dumped to.
226 */
227 public void dumpTree(PrintStream stream) {
228 dumpTree(stream, "", this);
229 }
230
231 /**
232 * Dump the subtree to the given print stream.
233 *
234 * @param stream stream the tree should be dumped to.
235 * @param prefix path prefix to the current node
236 * @param node current node
237 */
238 private void dumpTree(PrintStream stream, String prefix, HashTree node) {
239
240 prefix += PATH_SEPARATOR + node.getName();
241
242 if (node.isLeaf()) stream.println(prefix);
243 else {
244 Iterator it = node.children().iterator();
245
246 while(it.hasNext())
247 dumpTree(stream, prefix, (HashTree) it.next());
248 }
249 }
250 } // HashTree