KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > util > TreeNode


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13
14 package com.tonbeller.jpivot.util;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 /**
21  * Tree Node for the for a general tree of Objects
22  */

23 public class TreeNode {
24
25   private TreeNode parent = null;
26   private List JavaDoc children = null;
27   private Object JavaDoc reference;
28
29   /**
30    * cTtor
31    * @param obj referenced object
32    */

33   public TreeNode(Object JavaDoc obj) {
34     this.parent = null;
35     this.reference = obj;
36     this.children = new ArrayList JavaDoc();
37   }
38
39   /**
40    * remove node from tree
41    */

42   public void remove() {
43     if (parent != null) {
44       parent.removeChild(this);
45     }
46   }
47
48   /**
49    * remove child node
50    * @param child
51    */

52   private void removeChild(TreeNode child) {
53     if (children.contains(child))
54       children.remove(child);
55
56   }
57
58   /**
59    * add child node
60    * @param child node to be added
61    */

62   public void addChildNode(TreeNode child) {
63     child.parent = this;
64     if (!children.contains(child))
65       children.add(child);
66   }
67
68   /**
69    * deep copy (clone)
70    * @return copy of TreeNode
71    */

72   public TreeNode deepCopy() {
73     TreeNode newNode = new TreeNode(reference);
74     for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
75       TreeNode child = (TreeNode) iter.next();
76       newNode.addChildNode(child.deepCopy());
77     }
78     return newNode;
79   }
80
81   /**
82    * deep copy (clone) and prune
83    * @param depth - number of child levels to be copied
84    * @return copy of TreeNode
85    */

86   public TreeNode deepCopyPrune(int depth) {
87     if (depth < 0)
88       throw new IllegalArgumentException JavaDoc("Depth is negative");
89     TreeNode newNode = new TreeNode(reference);
90     if (depth == 0)
91       return newNode;
92     for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
93       TreeNode child = (TreeNode) iter.next();
94       newNode.addChildNode(child.deepCopyPrune(depth - 1));
95     }
96     return newNode;
97   }
98
99   /**
100    * @return level = distance from root
101    */

102   public int getLevel() {
103     int level = 0;
104     TreeNode p = parent;
105     while (p != null) {
106       ++level;
107       p = p.parent;
108     }
109     return level;
110   }
111
112   /**
113    * walk through subtree of this node
114    * @param callbackHandler function called on iteration
115    */

116   public int walkTree(TreeNodeCallback callbackHandler) {
117     int code = 0;
118     code = callbackHandler.handleTreeNode(this);
119     if (code != TreeNodeCallback.CONTINUE)
120       return code;
121     ChildLoop: for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
122       TreeNode child = (TreeNode) iter.next();
123       code = child.walkTree(callbackHandler);
124       if (code >= TreeNodeCallback.CONTINUE_PARENT)
125         return code;
126     }
127     return code;
128   }
129
130   /**
131    * walk through children subtrees of this node
132    * @param callbackHandler function called on iteration
133    */

134   public int walkChildren(TreeNodeCallback callbackHandler) {
135     int code = 0;
136     ChildLoop: for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
137       TreeNode child = (TreeNode) iter.next();
138       code = callbackHandler.handleTreeNode(child);
139       if (code >= TreeNodeCallback.CONTINUE_PARENT)
140         return code;
141       if (code == TreeNodeCallback.CONTINUE) {
142         code = child.walkChildren(callbackHandler);
143         if (code > TreeNodeCallback.CONTINUE_PARENT)
144           return code;
145       }
146     }
147     return code;
148   }
149
150   /**
151    * @return List of children
152    */

153   public List JavaDoc getChildren() {
154     return children;
155   }
156
157   /**
158    * @return parent node
159    */

160   public TreeNode getParent() {
161     return parent;
162   }
163
164   /**
165    * @return reference object
166    */

167   public Object JavaDoc getReference() {
168     return reference;
169   }
170
171   /**
172    * set reference object
173    * @param object reference
174    */

175   public void setReference(Object JavaDoc object) {
176     reference = object;
177   }
178
179 } // TreeNode
180
Popular Tags