1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  /**
21   * <a href="TreeNode.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Shuyang Zhou
24   */
25  public class TreeNode<T extends Comparable<T>> {
26  
27      public TreeNode(T value) {
28          this(value, null);
29      }
30  
31      public TreeNode(T value, TreeNode<T> parentNode) {
32          this(value, parentNode, new ArrayList<TreeNode<T>>());
33      }
34  
35      public TreeNode(
36          T value, TreeNode<T> parentNode, List<TreeNode<T>> childNodes) {
37  
38          _value = value;
39          _parentNode = parentNode;
40          _childNodes = childNodes;
41      }
42  
43      public TreeNode<T> addChildNode(T value) {
44          TreeNode<T> childNode = new TreeNode<T>(value, this);
45  
46          _childNodes.add(childNode);
47  
48          return childNode;
49      }
50  
51      public List<TreeNode<T>> getChildNodes() {
52          return _childNodes;
53      }
54  
55      public List<T> getChildValues() {
56          List<T> values = new ArrayList<T>(_childNodes.size());
57  
58          for (TreeNode<T> childNode : _childNodes) {
59              values.add(childNode.getValue());
60          }
61  
62          return values;
63      }
64  
65      public TreeNode<T> getParentNode() {
66          return _parentNode;
67      }
68  
69      public T getValue() {
70          return _value;
71      }
72  
73      public boolean isRootNode() {
74          if (_parentNode == null) {
75              return true;
76          }
77          else {
78              return false;
79          }
80      }
81  
82      private final List<TreeNode<T>> _childNodes;
83      private final TreeNode<T> _parentNode;
84      private final T _value;
85  
86  }