Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/util/FlexNode.java


1   /*
2    * FlexNode.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.util;
12  
13  import java.util.Enumeration;
14  import java.util.Vector;
15  
16  /**
17   * General purpose node to provide support for building hierarchical data structures.
18   */
19  public abstract class FlexNode
20     implements java.io.Serializable
21  {
22     /** Holds reference to the parent node. */
23     private FlexNode    parent     = null;
24  
25     /** Holds references to children nodes. */
26     private Vector vChildren = null;
27  
28     protected FlexNode()
29     {
30        vChildren = new Vector();
31     }
32  
33     /**
34      * Sets this node parent node.
35      * 
36      * @param parent the parent node for this node.
37      */
38     protected void setParent( FlexNode parent )
39     {
40        this.parent = parent;
41     }
42  
43     /**
44      * Retrieves a reference to the root node this node belongs to.
45      * 
46      * @return the root node.
47      */
48     public FlexNode getRoot()
49     {
50        FlexNode node = this;
51  
52        while ( node.getParent() != null )
53           node = node.getParent();
54  
55        return node;
56     }
57  
58     /**
59      * Retrieves a reference to this node parent node.
60      * 
61      * @return the parent node.
62      */
63     public FlexNode getParent()
64     {
65        return parent;
66     }
67  
68     /**
69      * Verifies if this node has children nodes.
70      * 
71      * @return true if thid node has children, otherwise returns false.
72      */
73     public boolean hasChildren()
74     {
75        return ( vChildren.size() > 0 );
76     }
77  
78     /**
79      * Retrieves the number of children nodes this node has.
80      * 
81      * @return the number of children nodes.
82      */
83     public int getChildrenCount()
84     {
85        return vChildren.size();
86     }
87  
88     /**
89      * Retrieves the children nodes this node has.
90      * 
91      * @return a collection of nodes.
92      */
93     public Vector getChildren()
94     {
95        // Return a clone so the private member is not exposed to the outside.
96        // Not doing so could potentially damage the original node structure
97        return (Vector) vChildren.clone();
98     }
99  
100    /**
101     * Retrieves the descendency of this node in a "In-Order" recursive sequence.
102     * 
103     * @return a collection of this node children.
104     */
105    public Vector getChildrenFlattened()
106    {
107       Vector alDescendency = new Vector();
108 
109       FlexNode   childNode  = null;
110       Enumeration eChildren = vChildren.elements();
111       while ( eChildren.hasMoreElements() )
112       {
113          childNode = (FlexNode)eChildren.nextElement();
114 
115          // add this child
116          alDescendency.addElement( childNode );
117 
118          if ( childNode.hasChildren() )
119          {
120             // add this child children
121             Vector vChildren = childNode.getChildrenFlattened();
122             for ( int c = 0; c < vChildren.size(); c++ )
123                alDescendency.addElement( (FlexNode)vChildren.elementAt( c ) );
124          }
125       }
126 
127       return alDescendency;
128    }
129 
130    /**
131     * Adds a child node to this node.
132     * 
133     * @param node the node to be added as a child.
134     */
135    public void addChild( FlexNode node )
136    {
137       node.setParent( this );
138 
139       vChildren.addElement( node );
140    }
141 
142    /**
143     * Removes a child node from this node.
144     * 
145     * @param node the node to be removed.
146     */
147    public void removeChild( FlexNode node )
148    {
149       vChildren.removeElement( node );
150 
151       node.setParent( null );
152    }
153 
154    /**
155     * Removes all children nodes from this node.
156     */
157    public void removeAllChildren()
158    {
159       Enumeration eChildren = vChildren.elements();
160       while ( eChildren.hasMoreElements() )
161          ( (FlexNode)eChildren.nextElement() ).setParent( null );
162 
163       vChildren.removeAllElements();
164    }
165 
166    /**
167     * Release memory allocated by this node and all its children.
168     */
169    public void dispose()
170    {
171       Enumeration eChildren = vChildren.elements();
172       while ( eChildren.hasMoreElements() )
173          ( (FlexNode)eChildren.nextElement() ).dispose();
174 
175       vChildren.removeAllElements();
176       vChildren = null;
177 
178       parent = null;
179    }
180 
181 } // end of class