Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Tree |
|
| 1.1666666666666667;1.167 |
1 | /** | |
2 | * Copyright 2005-2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.core.api.util.tree; | |
17 | ||
18 | import java.io.Serializable; | |
19 | import java.util.ArrayList; | |
20 | import java.util.List; | |
21 | ||
22 | /** | |
23 | * Represents a Tree of Objects of generic type T. The Tree is represented as | |
24 | * a single rootElement which points to a List<Node<T>> of children. There is | |
25 | * no restriction on the number of children that a particular node may have. | |
26 | * This Tree provides a method to serialize the Tree into a List by doing a | |
27 | * pre-order traversal. It has several methods to allow easy updates of Nodes | |
28 | * in the Tree. | |
29 | */ | |
30 | public class Tree<T, K> implements Serializable { | |
31 | private static final long serialVersionUID = -8335139832978783512L; | |
32 | ||
33 | private Node<T, K> rootElement; | |
34 | ||
35 | /** | |
36 | * Default constructor. | |
37 | */ | |
38 | public Tree() { | |
39 | 0 | super(); |
40 | 0 | } |
41 | ||
42 | /** | |
43 | * Return the root Node of the tree. | |
44 | * | |
45 | * @return the root element. | |
46 | */ | |
47 | public Node<T, K> getRootElement() { | |
48 | 0 | return this.rootElement; |
49 | } | |
50 | ||
51 | /** | |
52 | * Set the root Element for the tree. | |
53 | * | |
54 | * @param rootElement the root element to set. | |
55 | */ | |
56 | public void setRootElement(Node<T, K> rootElement) { | |
57 | 0 | this.rootElement = rootElement; |
58 | 0 | } |
59 | ||
60 | /** | |
61 | * Returns the Tree<T> as a List of Node<T, K> objects. The elements of the | |
62 | * List are generated from a pre-order traversal of the tree. | |
63 | * | |
64 | * @return a List<Node<T, K>>. | |
65 | */ | |
66 | public List<Node<T, K>> toList() { | |
67 | 0 | List<Node<T, K>> list = new ArrayList<Node<T, K>>(); |
68 | 0 | walk(rootElement, list); |
69 | 0 | return list; |
70 | } | |
71 | ||
72 | /** | |
73 | * Returns a String representation of the Tree. The elements are generated | |
74 | * from a pre-order traversal of the Tree. | |
75 | * | |
76 | * @return the String representation of the Tree. | |
77 | */ | |
78 | public String toString() { | |
79 | 0 | return toList().toString(); |
80 | } | |
81 | ||
82 | /** | |
83 | * Walks the Tree in pre-order style. This is a recursive method, and is | |
84 | * called from the toList() method with the root element as the first | |
85 | * argument. It appends to the second argument, which is passed by reference | |
86 | * as it recurses down the tree. | |
87 | * | |
88 | * @param element the starting element. | |
89 | * @param list the output of the walk. | |
90 | */ | |
91 | private void walk(Node<T, K> element, List<Node<T, K>> list) { | |
92 | 0 | list.add(element); |
93 | 0 | for (Node<T, K> data : element.getChildren()) { |
94 | 0 | walk(data, list); |
95 | } | |
96 | 0 | } |
97 | } | |
98 |