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