001 /**
002 * Copyright (c) 2010, SIB. All rights reserved.
003 *
004 * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005 * https://sourceforge.net/projects/javaprotlib/
006 *
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions are met:
009 * Redistributions of source code must retain the above copyright notice, this
010 * list of conditions and the following disclaimer. Redistributions in binary
011 * form must reproduce the above copyright notice, this list of conditions and
012 * the following disclaimer in the documentation and/or other materials provided
013 * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014 * its contributors may be used to endorse or promote products derived from this
015 * software without specific prior written permission.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020 * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package org.expasy.jpl.commons.collection.tree;
029
030
031 import java.util.List;
032 import java.util.Set;
033
034
035 /**
036 * A read-only tree view.
037 *
038 * @author nikitin
039 *
040 * @param <T> the node type.
041 *
042 * @version 1.0
043 *
044 */
045 public final class TreeViewImpl<T> implements TreeView<T> {
046
047 TreeNode<T> treeNode;
048
049 public TreeViewImpl(TreeNode<T> root) {
050 treeNode = root;
051 }
052
053 /** {@inheritDoc} */
054 public TreeView<T> getChildAt(int index) {
055 return treeNode.getChildAt(index);
056 }
057
058 /** {@inheritDoc} */
059 public List<? extends TreeView<T>> getChildren() {
060 return treeNode.getChildren();
061 }
062
063 /** {@inheritDoc} */
064 public T getData() {
065 return treeNode.getData();
066 }
067
068 /** {@inheritDoc} */
069 public Set<? extends TreeView<T>> getLeaves() {
070 return treeNode.getLeaves();
071 }
072
073 /** {@inheritDoc} */
074 public Set<? extends TreeView<T>> getNodes() {
075 return treeNode.getNodes();
076 }
077
078 /** {@inheritDoc} */
079 public int getNumberOfChildren() {
080 return treeNode.getNumberOfChildren();
081 }
082
083 /** {@inheritDoc} */
084 public TreeView<T> getParent() {
085 return treeNode.getParent();
086 }
087
088 /** {@inheritDoc} */
089 public boolean isLeave() {
090 return treeNode.isLeave();
091 }
092
093 /** {@inheritDoc} */
094 public boolean isRoot() {
095 return treeNode.isRoot();
096 }
097
098 /**
099 * Recursive depth search first.
100 *
101 * @param fromNode the root node.
102 * @param nodes the found nodes.
103 * @param isLeaveOnly search only leaves if true.
104 */
105 @SuppressWarnings("unchecked")
106 public static <T, E extends TreeView<T>> void depthSearchFirst(E fromNode,
107 Set<E> nodes, boolean isLeaveOnly) {
108
109 if (isLeaveOnly) {
110 if (fromNode.isLeave()) {
111 nodes.add(fromNode);
112 }
113 } else {
114 nodes.add(fromNode);
115 }
116
117 for (TreeView<T> child : fromNode.getChildren()) {
118 depthSearchFirst((E) child, nodes, isLeaveOnly);
119 }
120 }
121
122 }