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