001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    /**
021     * @author Shuyang Zhou
022     */
023    public class TreeNode<T extends Comparable<T>> {
024    
025            public TreeNode(T value) {
026                    this(value, null);
027            }
028    
029            public TreeNode(T value, TreeNode<T> parentNode) {
030                    this(value, parentNode, new ArrayList<TreeNode<T>>());
031            }
032    
033            public TreeNode(
034                    T value, TreeNode<T> parentNode, List<TreeNode<T>> childNodes) {
035    
036                    _value = value;
037                    _parentNode = parentNode;
038                    _childNodes = childNodes;
039            }
040    
041            public TreeNode<T> addChildNode(T value) {
042                    TreeNode<T> childNode = new TreeNode<T>(value, this);
043    
044                    _childNodes.add(childNode);
045    
046                    return childNode;
047            }
048    
049            public List<TreeNode<T>> getChildNodes() {
050                    return _childNodes;
051            }
052    
053            public List<T> getChildValues() {
054                    List<T> values = new ArrayList<T>(_childNodes.size());
055    
056                    for (TreeNode<T> childNode : _childNodes) {
057                            values.add(childNode.getValue());
058                    }
059    
060                    return values;
061            }
062    
063            public TreeNode<T> getParentNode() {
064                    return _parentNode;
065            }
066    
067            public T getValue() {
068                    return _value;
069            }
070    
071            public boolean isRootNode() {
072                    if (_parentNode == null) {
073                            return true;
074                    }
075                    else {
076                            return false;
077                    }
078            }
079    
080            private final List<TreeNode<T>> _childNodes;
081            private final TreeNode<T> _parentNode;
082            private final T _value;
083    
084    }