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.ArrayList;
032 import java.util.HashSet;
033 import java.util.List;
034 import java.util.Set;
035
036
037 /**
038 * Naive tree node.
039 *
040 * @author nikitin.
041 *
042 * @param <T> data type.
043 *
044 * @version 1.0
045 *
046 */
047 public final class TreeNodeImpl<T> implements TreeNode<T> {
048
049 private static final long serialVersionUID = 1724526834635959445L;
050 private T data;
051 private TreeNode<T> parent;
052 private List<TreeNode<T>> children;
053
054 private TreeNodeImpl(T data) {
055 children = new ArrayList<TreeNode<T>>();
056 this.data = data;
057 }
058
059 private TreeNodeImpl(T data, TreeNode<T> parent) {
060 this(data);
061 setParent(parent);
062 }
063
064 public static <T> TreeNodeImpl<T> newRoot(T data) {
065 return new TreeNodeImpl<T>(data);
066 }
067
068 public static <T> TreeNodeImpl<T> newNode(T data, TreeNode<T> parent) {
069 return new TreeNodeImpl<T>(data, parent);
070 }
071
072 public final TreeNode<T> getParent() {
073 return parent;
074 }
075
076 public void setParent(TreeNode<T> parent) {
077 parent.addChild(this);
078 }
079
080 public final List<TreeNode<T>> getChildren() {
081 return children;
082 }
083
084 public final Set<TreeNode<T>> getLeaves() {
085
086 Set<TreeNode<T>> leaves = new HashSet<TreeNode<T>>();
087 TreeViewImpl.depthSearchFirst(this, leaves, true);
088
089 return leaves;
090 }
091
092 public final Set<TreeNode<T>> getNodes() {
093
094 Set<TreeNode<T>> nodes = new HashSet<TreeNode<T>>();
095 TreeViewImpl.depthSearchFirst(this, nodes, false);
096
097 return nodes;
098 }
099
100 public final int getNumberOfChildren() {
101 return getChildren().size();
102 }
103
104 public final boolean isRoot() {
105 return parent == null;
106 }
107
108 public final boolean isLeave() {
109 return getNumberOfChildren() == 0;
110 }
111
112 public void addChild(TreeNode<T> child) {
113 children.add(child);
114 ((TreeNodeImpl<T>) child).parent = this;
115 }
116
117 public final TreeNode<T> getChildAt(int index)
118 throws IndexOutOfBoundsException {
119 return children.get(index);
120 }
121
122 public final T getData() {
123 return data;
124 }
125
126 public void setData(T data) {
127 this.data = data;
128 }
129
130 @SuppressWarnings("unchecked")
131 public boolean equals(Object o) {
132
133 if (o instanceof TreeNodeImpl) {
134 TreeNodeImpl<T> node = (TreeNodeImpl<T>) o;
135 return node.getData().equals(getData());
136 }
137 return false;
138 }
139
140 public int hashCode() {
141 return getData().hashCode();
142 }
143
144 public String toString() {
145 if (data != null) {
146 return data.toString();
147 }
148 return null;
149 }
150 }