1 /**
2 * Copyright 2010-2015 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.common.jute.tree;
17
18 import static com.google.common.collect.Lists.newArrayList;
19 import static com.google.common.collect.Lists.reverse;
20 import static com.google.common.collect.Lists.transform;
21 import static org.kuali.common.jute.base.Precondition.checkNotNull;
22
23 import java.util.List;
24
25 public abstract class AbstractNode<T> implements Node<T> {
26
27 /**
28 * Return true if this is the root node (ie it has no parent)
29 */
30 @Override
31 public boolean isRoot() {
32 return !getParent().isPresent();
33 }
34
35 /**
36 * Return true if this is a leaf node (ie it has no children)
37 */
38 @Override
39 public boolean isLeaf() {
40 return getChildren().isEmpty();
41 }
42
43 /**
44 * Returns the number of levels above this node. The distance from the root to this node. If this node is the root, returns 0.
45 */
46 @Override
47 public int getLevel() {
48 int level = 0;
49 Node<T> ancestor = this;
50 while (ancestor.getParent().isPresent()) {
51 ancestor = ancestor.getParent().get();
52 level++;
53 }
54 return level;
55 }
56
57 /**
58 * Returns the path from the root, to get to this node. The last element in the path is this node.
59 */
60 @Override
61 public List<Node<T>> getPath() {
62 Node<T> ancestor = this;
63 List<Node<T>> list = newArrayList();
64 list.add(ancestor);
65 while (ancestor.getParent().isPresent()) {
66 ancestor = ancestor.getParent().get();
67 list.add(ancestor);
68 }
69 return reverse(list);
70 }
71
72 /**
73 * Returns the node elements in the path from the root, to get to this node. The last entry is the element from this node
74 */
75 @Override
76 public List<T> getElementPath() {
77 return transform(getPath(), Nodes.<T> nodeElementFunction());
78 }
79
80 /**
81 * Return true if this node is a child of parent
82 */
83 @Override
84 public boolean isChild(Node<T> parent) {
85 checkNotNull(parent, "parent");
86 return parent.getChildren().contains(this);
87 }
88
89 /**
90 * Return true if this node is a parent of child
91 */
92 @Override
93 public boolean isParent(Node<T> child) {
94 checkNotNull(child, "child");
95 return getChildren().contains(child);
96 }
97
98 /**
99 * Return true if this node descends from parent OR is parent
100 */
101 @Override
102 public boolean isAncestor(Node<T> parent) {
103 checkNotNull(parent, "parent");
104 return getPath().contains(parent);
105 }
106
107 }