Coverage Report - org.kuali.rice.core.api.util.tree.Node
 
Classes in this File Line Coverage Branch Coverage Complexity
Node
0%
0/49
0%
0/12
1.562
 
 1  
 package org.kuali.rice.core.api.util.tree;
 2  
 
 3  
 import java.io.Serializable;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 
 7  
 /**
 8  
  * Represents a node of the Tree<T, K> class. The Node<T, K> is also a container, and
 9  
  * can be thought of as instrumentation to determine the location of the type T
 10  
  * in the Tree<T ,K>.
 11  
  */
 12  
 public class Node<T, K> implements Serializable {
 13  
     private static final long serialVersionUID = -2650587832812603561L;
 14  
 
 15  
     private T data;
 16  
     private List<Node<T, K>> children;
 17  
 
 18  
     private K nodeLabel;
 19  
     private String nodeType;
 20  
 
 21  
     /**
 22  
      * Default constructor.
 23  
      */
 24  
     public Node() {
 25  0
         super();
 26  
 
 27  0
         children = new ArrayList<Node<T, K>>();
 28  0
     }
 29  
 
 30  
     /**
 31  
      * Convenience constructor to create a Node<T, K> with an instance of T.
 32  
      *
 33  
      * @param data an instance of T.
 34  
      */
 35  
     public Node(T data) {
 36  0
         this();
 37  0
         setData(data);
 38  0
     }
 39  
     
 40  
     /**
 41  
      * Convenience constructor to create a Node<T, K> with an instance of T and K.
 42  
      *
 43  
      * @param data an instance of T.
 44  
      */
 45  
     public Node(T data, K label) {
 46  0
         this();
 47  0
         setData(data);
 48  0
         setNodeLabel(label);
 49  0
     }
 50  
 
 51  
     /**
 52  
      * Return the children of Node<T, K>. The Tree<T> is represented by a single
 53  
      * root Node<T, K> whose children are represented by a List<Node<T, K>>. Each of
 54  
      * these Node<T, K> elements in the List can have children. The getChildren()
 55  
      * method will return the children of a Node<T, K>.
 56  
      *
 57  
      * @return the children of Node<T, K>
 58  
      */
 59  
     public List<Node<T, K>> getChildren() {
 60  0
         if (this.children == null) {
 61  0
             return new ArrayList<Node<T, K>>();
 62  
         }
 63  0
         return this.children;
 64  
     }
 65  
 
 66  
     /**
 67  
      * Sets the children of a Node<T, K> object. See docs for getChildren() for
 68  
      * more information.
 69  
      *
 70  
      * @param children the List<Node<T, K>> to set.
 71  
      */
 72  
     public void setChildren(List<Node<T, K>> children) {
 73  0
         this.children = children;
 74  0
     }
 75  
 
 76  
     /**
 77  
      * Returns the number of immediate children of this Node<T, K>.
 78  
      *
 79  
      * @return the number of immediate children.
 80  
      */
 81  
     public int getNumberOfChildren() {
 82  0
         if (children == null) {
 83  0
             return 0;
 84  
         }
 85  0
         return children.size();
 86  
     }
 87  
 
 88  
     /**
 89  
      * Adds a child to the list of children for this Node<T, K>. The addition of
 90  
      * the first child will create a new List<Node<T, K>>.
 91  
      *
 92  
      * @param child a Node<T, K> object to set.
 93  
      */
 94  
     public void addChild(Node<T, K> child) {
 95  0
         if (children == null) {
 96  0
             children = new ArrayList<Node<T, K>>();
 97  
         }
 98  0
         children.add(child);
 99  0
     }
 100  
 
 101  
     /**
 102  
      * Inserts a Node<T, K> at the specified position in the child list. Will
 103  
      * throw an ArrayIndexOutOfBoundsException if the index does not exist.
 104  
      *
 105  
      * @param index the position to insert at.
 106  
      * @param child the Node<T, K> object to insert.
 107  
      * @throws IndexOutOfBoundsException if thrown.
 108  
      */
 109  
     public void insertChildAt(int index, Node<T, K> child) throws IndexOutOfBoundsException {
 110  0
         if (index == getNumberOfChildren()) {
 111  
             // this is really an append
 112  0
             addChild(child);
 113  0
             return;
 114  
         } else {
 115  0
             children.get(index); //just to throw the exception, and stop here
 116  0
             children.add(index, child);
 117  
         }
 118  0
     }
 119  
 
 120  
     /**
 121  
      * Remove the Node<T, K> element at index index of the List<Node<T, K>>.
 122  
      *
 123  
      * @param index the index of the element to delete.
 124  
      * @throws IndexOutOfBoundsException if thrown.
 125  
      */
 126  
     public void removeChildAt(int index) throws IndexOutOfBoundsException {
 127  0
         children.remove(index);
 128  0
     }
 129  
 
 130  
     /**
 131  
      * Data object contained in the node (a leaf)
 132  
      *
 133  
      * @return Object
 134  
      */
 135  
     public T getData() {
 136  0
         return this.data;
 137  
     }
 138  
 
 139  
     /**
 140  
      * Setter for the nodes data
 141  
      *
 142  
      * @param data
 143  
      */
 144  
     public void setData(T data) {
 145  0
         this.data = data;
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Object containing the data for labeling the node (can be simple String)
 150  
      *
 151  
      * @return K
 152  
      */
 153  
     public K getNodeLabel() {
 154  0
         return nodeLabel;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Setter for the nodes label data
 159  
      *
 160  
      * @param nodeLabel
 161  
      */
 162  
     public void setNodeLabel(K nodeLabel) {
 163  0
         this.nodeLabel = nodeLabel;
 164  0
     }
 165  
 
 166  
     /**
 167  
      * Indicates what type of node is being represented, used to give
 168  
      * a functional label for the node that can also be used for presentation
 169  
      * purposes
 170  
      *
 171  
      * @return String node type
 172  
      */
 173  
     public String getNodeType() {
 174  0
         return nodeType;
 175  
     }
 176  
 
 177  
     /**
 178  
      * Setter for the node type String
 179  
      *
 180  
      * @param nodeType
 181  
      */
 182  
     public void setNodeType(String nodeType) {
 183  0
         this.nodeType = nodeType;
 184  0
     }
 185  
 
 186  
     public String toString() {
 187  0
         StringBuilder sb = new StringBuilder();
 188  0
         sb.append("{").append(getData().toString()).append(",[");
 189  0
         int i = 0;
 190  0
         for (Node<T, K> e : getChildren()) {
 191  0
             if (i > 0) {
 192  0
                 sb.append(",");
 193  
             }
 194  0
             sb.append(e.getData().toString());
 195  0
             i++;
 196  
         }
 197  0
         sb.append("]").append("}");
 198  0
         return sb.toString();
 199  
     }
 200  
 }
 201