TreeNode.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.data.experiment.scope;
00002 
00003 import java.util.ArrayList;
00004 import java.util.Iterator;
00005 import java.util.List;
00006 
00007 /*******************************************************************************
00008  * Copyright (c) 2005, 2007 IBM Corporation and others.
00009  * All rights reserved. This program and the accompanying materials
00010  * are made available under the terms of the Eclipse Public License v1.0
00011  * which accompanies this distribution, and is available at
00012  * http://www.eclipse.org/legal/epl-v10.html
00013  *
00014  * Contributors:
00015  *     IBM Corporation - initial API and implementation
00016  *******************************************************************************/
00017 
00018 
00035 public class TreeNode {
00036 
00043     private List<TreeNode> children;
00044 
00049     private TreeNode parent;
00050 
00054     protected Object value;
00055 
00062     public TreeNode(final Object value) {
00063         this.value = value;
00064     }
00065     
00066     public boolean equals(final Object object) {
00067         if (object instanceof TreeNode) {
00068             return TreeNode.equals(this.value, ((TreeNode) object).value);
00069         }
00070 
00071         return false;
00072     }
00073 
00081     public Object[] getChildren() {
00082         if (children != null && children.size() > 0) {
00083             return children.toArray();
00084         }
00085         return null;
00086     }
00087 
00094     public TreeNode getParent() {
00095         return parent;
00096     }
00097 
00103     public Object getValue() {
00104         return value;
00105     }
00106 
00114     public boolean hasChildren() {
00115         return children != null && children.size() > 0;
00116     }
00117     
00118     public int hashCode() {
00119         return TreeNode.hashCode(value);
00120     }
00121 
00122     /***
00123      * Add a new child
00124      * 
00125      * @param child : child to be added
00126      */
00127     public void add(TreeNode child)
00128     {
00129         if (children == null) {
00130             children = new ArrayList<TreeNode>();
00131         }
00132         children.add(child);
00133     }
00134     
00141     public void remove(TreeNode child)
00142     {
00143         children.remove(child);
00144     }
00145     
00146     
00151     public void remove(int index)
00152     {
00153         children.remove(index);
00154     }
00155     
00162     public void setParent(final TreeNode parent) {
00163         this.parent = parent;
00164     }
00165     
00166     /***
00167      * Retrieve a child node of a specific index
00168      * 
00169      * @param index
00170      * 
00171      * @return TreeNode: a node
00172      */
00173     public TreeNode getChildAt(int index) 
00174     {
00175         if (children != null) {
00176             if (index < children.size())
00177                 return children.get(index);
00178         }
00179         return null;
00180     }
00181     
00182     /*****
00183      * Return the number of children
00184      * 
00185      * @return
00186      */
00187     public int getChildCount() 
00188     {
00189         if (children == null)
00190             return 0;
00191         return children.size();
00192     }
00193     
00202     public Iterator<TreeNode> getIterator()
00203     {
00204         if (children != null)
00205             return children.iterator();
00206         else
00207             return null;
00208     }
00209     
00219     public static final int hashCode(final Object object) {
00220         return object != null ? object.hashCode() : 0;
00221     }
00222 
00234     public static final boolean equals(final Object left, final Object right) {
00235         return left == null ? right == null : ((right != null) && left
00236                 .equals(right));
00237     }
00238 
00239     /*****
00240      * free resources
00241      */
00242     public void dispose()
00243     {
00244         if (children != null)
00245         {
00246             children.clear();
00247             children = null;
00248         }
00249         parent   = null;
00250         value    = null;
00251     }
00252 }
00253 

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1