001    /**
002     * Copyright (c) 2000-2013 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     * @author Dennis Ju
023     * @author Brian Wing Shun Chan
024     */
025    public class TreeNode<T extends Comparable<T>> {
026    
027            public TreeNode(T value) {
028                    this(value, null);
029            }
030    
031            public TreeNode(T value, TreeNode<T> parentNode) {
032                    this(value, parentNode, new ArrayList<TreeNode<T>>());
033            }
034    
035            public TreeNode(
036                    T value, TreeNode<T> parentNode, List<TreeNode<T>> childNodes) {
037    
038                    _value = value;
039                    _parentNode = parentNode;
040                    _childNodes = childNodes;
041            }
042    
043            public TreeNode<T> addChildNode(T value) {
044                    TreeNode<T> childNode = new TreeNode<T>(value, this);
045    
046                    _childNodes.add(childNode);
047    
048                    return childNode;
049            }
050    
051            public List<TreeNode<T>> getChildNodes() {
052                    return _childNodes;
053            }
054    
055            public List<T> getChildValues() {
056                    List<T> values = new ArrayList<T>(_childNodes.size());
057    
058                    for (TreeNode<T> childNode : _childNodes) {
059                            values.add(childNode.getValue());
060                    }
061    
062                    return values;
063            }
064    
065            public TreeNode<T> getParentNode() {
066                    return _parentNode;
067            }
068    
069            public T getValue() {
070                    return _value;
071            }
072    
073            public boolean isRootNode() {
074                    if (_parentNode == null) {
075                            return true;
076                    }
077                    else {
078                            return false;
079                    }
080            }
081    
082            private final List<TreeNode<T>> _childNodes;
083            private final TreeNode<T> _parentNode;
084            private final T _value;
085    
086    }