1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }