001    /*--------------------------------------------------------------------------+
002    $Id: SimpleTreeNode.java 26268 2010-02-18 10:44:30Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.tree;
019    
020    import java.util.HashMap;
021    
022    import edu.tum.cs.commons.collections.CollectionUtils;
023    import edu.tum.cs.commons.collections.UnmodifiableCollection;
024    import edu.tum.cs.commons.string.StringUtils;
025    
026    /**
027     * A simple node class that can be used with {@link TreeUtils}. See
028     * {@link TreeUtilsTest} for an application of this class that uses strings as
029     * keys.
030     * 
031     * @param <K>
032     *            key used to identify children, e.g. String.
033     * 
034     * @author deissenb
035     * @author $Author: juergens $
036     * @version $Rev: 26268 $
037     * @levd.rating GREEN Hash: B1592DD3B3D1D3EEFF7DDDE5A25EDE03
038     */
039    public class SimpleTreeNode<K> {
040    
041            /** Maps from key to child. */
042            private final HashMap<K, SimpleTreeNode<K>> children = new HashMap<K, SimpleTreeNode<K>>();
043    
044            /** Key of this node. */
045            private final K key;
046    
047            /** Create new node with specified key. */
048            public SimpleTreeNode(K key) {
049                    this.key = key;
050            }
051    
052            /**
053             * Get child with specified key. This returns <code>null</code> if child
054             * with provided key does not exist.
055             */
056            public SimpleTreeNode<K> getChild(K key) {
057                    return children.get(key);
058            }
059    
060            /** Add child. This overwrites existing child with same key. */
061            public void addChild(SimpleTreeNode<K> child) {
062                    children.put(child.getKey(), child);
063            }
064    
065            /** Get key of this node. */
066            public K getKey() {
067                    return key;
068            }
069    
070            /** Get children of this node. */
071            public UnmodifiableCollection<SimpleTreeNode<K>> getChildren() {
072                    return CollectionUtils.asUnmodifiable(children.values());
073            }
074    
075            /**
076             * This returns a nicely indented representation of the whole tree below
077             * this node.
078             */
079            @Override
080            public String toString() {
081                    StringBuilder result = new StringBuilder();
082                    result.append(key + StringUtils.CR);
083                    for (SimpleTreeNode<K> child : children.values()) {
084                            result.append(StringUtils.prefixLines(child.toString(),
085                                            StringUtils.TWO_SPACES, true));
086                            result.append(StringUtils.CR);
087                    }
088                    return result.toString();
089            }
090    }