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     */
023    public class ListTree<T extends Comparable<T>> {
024    
025            public ListTree() {
026                    this(null);
027            }
028    
029            public ListTree(T value) {
030                    _rootNode = new TreeNode<T>(value);
031            }
032    
033            public List<TreeNode<T>> getChildNodes(TreeNode<T> node) {
034                    List<TreeNode<T>> nodes = new ArrayList<TreeNode<T>>();
035    
036                    getChildNodes(node, nodes);
037    
038                    return nodes;
039            }
040    
041            public TreeNode<T> getRootNode() {
042                    return _rootNode;
043            }
044    
045            protected void getChildNodes(TreeNode<T> node, List<TreeNode<T>> nodes) {
046                    List<TreeNode<T>> childNodes = node.getChildNodes();
047    
048                    nodes.addAll(childNodes);
049    
050                    for (TreeNode<T> childNode : childNodes) {
051                            getChildNodes(childNode, nodes);
052                    }
053            }
054    
055            private final TreeNode<T> _rootNode;
056    
057    }