Coverage Report - org.kuali.rice.core.api.util.tree.Tree
 
Classes in this File Line Coverage Branch Coverage Complexity
Tree
0%
0/13
0%
0/2
1.167
 
 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 Tree of Objects of generic type T. The Tree is represented as
 9  
  * a single rootElement which points to a List<Node<T>> of children. There is
 10  
  * no restriction on the number of children that a particular node may have.
 11  
  * This Tree provides a method to serialize the Tree into a List by doing a
 12  
  * pre-order traversal. It has several methods to allow easy updates of Nodes
 13  
  * in the Tree.
 14  
  */
 15  
 public class Tree<T, K> implements Serializable {
 16  
     private static final long serialVersionUID = -8335139832978783512L;
 17  
 
 18  
     private Node<T, K> rootElement;
 19  
 
 20  
     /**
 21  
      * Default constructor.
 22  
      */
 23  
     public Tree() {
 24  0
         super();
 25  0
     }
 26  
 
 27  
     /**
 28  
      * Return the root Node of the tree.
 29  
      *
 30  
      * @return the root element.
 31  
      */
 32  
     public Node<T, K> getRootElement() {
 33  0
         return this.rootElement;
 34  
     }
 35  
 
 36  
     /**
 37  
      * Set the root Element for the tree.
 38  
      *
 39  
      * @param rootElement the root element to set.
 40  
      */
 41  
     public void setRootElement(Node<T, K> rootElement) {
 42  0
         this.rootElement = rootElement;
 43  0
     }
 44  
 
 45  
     /**
 46  
      * Returns the Tree<T> as a List of Node<T, K> objects. The elements of the
 47  
      * List are generated from a pre-order traversal of the tree.
 48  
      *
 49  
      * @return a List<Node<T, K>>.
 50  
      */
 51  
     public List<Node<T, K>> toList() {
 52  0
         List<Node<T, K>> list = new ArrayList<Node<T, K>>();
 53  0
         walk(rootElement, list);
 54  0
         return list;
 55  
     }
 56  
 
 57  
     /**
 58  
      * Returns a String representation of the Tree. The elements are generated
 59  
      * from a pre-order traversal of the Tree.
 60  
      *
 61  
      * @return the String representation of the Tree.
 62  
      */
 63  
     public String toString() {
 64  0
         return toList().toString();
 65  
     }
 66  
 
 67  
     /**
 68  
      * Walks the Tree in pre-order style. This is a recursive method, and is
 69  
      * called from the toList() method with the root element as the first
 70  
      * argument. It appends to the second argument, which is passed by reference
 71  
      * as it recurses down the tree.
 72  
      *
 73  
      * @param element the starting element.
 74  
      * @param list    the output of the walk.
 75  
      */
 76  
     private void walk(Node<T, K> element, List<Node<T, K>> list) {
 77  0
         list.add(element);
 78  0
         for (Node<T, K> data : element.getChildren()) {
 79  0
             walk(data, list);
 80  
         }
 81  0
     }
 82  
 }
 83