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
019/**
020 * Handler for {@link SimpleTreeNode}.
021 * 
022 * @author deissenb
023 */
024public class SimpleTreeNodeHandler<K> implements ITreeNodeHandler<SimpleTreeNode<K>, K> {
025
026        /** The key used for the root. */
027        private final K rootKey;
028
029        /**
030         * Create new handler.
031         * 
032         * @param rootKey
033         *            the key used for the root node.
034         */
035        public SimpleTreeNodeHandler(K rootKey) {
036                this.rootKey = rootKey;
037        }
038
039        /** {@inheritDoc} */
040        @Override
041        public SimpleTreeNode<K> createRoot() {
042                return new SimpleTreeNode<K>(rootKey);
043        }
044
045        /** {@inheritDoc} */
046        @Override
047        public SimpleTreeNode<K> getOrCreateChild(SimpleTreeNode<K> node, K key) {
048                SimpleTreeNode<K> child = node.getChild(key);
049                if (child != null) {
050                        return child;
051                }
052
053                child = new SimpleTreeNode<K>(key);
054                node.addChild(child);
055                return child;
056        }
057
058}