[1113] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.tasktrees.treeimpl;
|
---|
[439] | 16 |
|
---|
| 17 | import java.util.ArrayList;
|
---|
| 18 | import java.util.List;
|
---|
| 19 |
|
---|
[922] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
|
---|
[439] | 21 |
|
---|
| 22 | /**
|
---|
| 23 | * TODO comment
|
---|
[557] | 24 | *
|
---|
[439] | 25 | * @version $Revision: $ $Date: $
|
---|
[557] | 26 | * @author 2011, last modified by $Author: $
|
---|
[439] | 27 | */
|
---|
[557] | 28 | public class TaskTreeNode implements ITaskTreeNode {
|
---|
| 29 | /** */
|
---|
| 30 | private static int temporalId = 0;
|
---|
[439] | 31 |
|
---|
[557] | 32 | /** */
|
---|
| 33 | private String name;
|
---|
[439] | 34 |
|
---|
[557] | 35 | /** */
|
---|
| 36 | private String description;
|
---|
[439] | 37 |
|
---|
[557] | 38 | /** */
|
---|
| 39 | private int id;
|
---|
| 40 |
|
---|
| 41 | /** children */
|
---|
| 42 | private List<ITaskTreeNode> children;
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | *
|
---|
| 46 | */
|
---|
| 47 | public TaskTreeNode(String name) {
|
---|
| 48 | this.name = name;
|
---|
| 49 | id = getNewId();
|
---|
[439] | 50 | }
|
---|
| 51 |
|
---|
[557] | 52 | /**
|
---|
| 53 | * TODO: comment
|
---|
| 54 | *
|
---|
| 55 | * @return
|
---|
| 56 | */
|
---|
| 57 | private static synchronized int getNewId() {
|
---|
| 58 | if (temporalId == Integer.MAX_VALUE) {
|
---|
| 59 | temporalId = 0;
|
---|
| 60 | }
|
---|
[439] | 61 |
|
---|
[557] | 62 | return temporalId++;
|
---|
| 63 | }
|
---|
[439] | 64 |
|
---|
[557] | 65 | /**
|
---|
| 66 | * @return Returns the name.
|
---|
| 67 | */
|
---|
| 68 | public String getName() {
|
---|
| 69 | return name;
|
---|
[439] | 70 | }
|
---|
[557] | 71 |
|
---|
| 72 | /*
|
---|
| 73 | * (non-Javadoc)
|
---|
| 74 | *
|
---|
| 75 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeNode#getDescription()
|
---|
| 76 | */
|
---|
| 77 | @Override
|
---|
| 78 | public String getDescription() {
|
---|
| 79 | return description;
|
---|
[439] | 80 | }
|
---|
[557] | 81 |
|
---|
| 82 | /**
|
---|
| 83 | *
|
---|
| 84 | */
|
---|
| 85 | public synchronized List<ITaskTreeNode> getChildren() {
|
---|
| 86 | if ((children == null) || (children.size() == 0)) {
|
---|
| 87 | return new ArrayList<ITaskTreeNode>();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return children.subList(0, children.size());
|
---|
[439] | 91 | }
|
---|
[557] | 92 |
|
---|
| 93 | /*
|
---|
| 94 | * (non-Javadoc)
|
---|
| 95 | *
|
---|
[922] | 96 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode)
|
---|
[557] | 97 | */
|
---|
| 98 | @Override
|
---|
| 99 | public boolean equals(ITaskTreeNode taskTreeNode) {
|
---|
| 100 | if (!this.getClass().isInstance(taskTreeNode)) {
|
---|
| 101 | return false;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | if (taskTreeNode.hashCode() != hashCode()) {
|
---|
| 105 | return false;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | TaskTreeNode other = (TaskTreeNode) taskTreeNode;
|
---|
| 109 |
|
---|
| 110 | if (id != other.id) {
|
---|
| 111 | return false;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if (!name.equals(other.name)) {
|
---|
| 115 | return false;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | synchronized (other) {
|
---|
| 119 | if (children == null) {
|
---|
| 120 | return (other.children == null);
|
---|
| 121 | }
|
---|
| 122 | else if (other.children == null) {
|
---|
| 123 | return (children == null);
|
---|
| 124 | }
|
---|
| 125 | else if (other.children.size() != children.size()) {
|
---|
| 126 | return false;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | for (int i = 0; i < children.size(); i++) {
|
---|
| 130 | if (!children.get(i).equals(other.children.get(i))) {
|
---|
| 131 | return false;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | return true;
|
---|
[439] | 137 | }
|
---|
[557] | 138 |
|
---|
| 139 | /*
|
---|
| 140 | * (non-Javadoc)
|
---|
| 141 | *
|
---|
| 142 | * @see java.lang.Object#hashCode()
|
---|
| 143 | */
|
---|
| 144 | @Override
|
---|
| 145 | public synchronized int hashCode() {
|
---|
| 146 | return getClass().getSimpleName().hashCode();
|
---|
[439] | 147 | }
|
---|
[557] | 148 |
|
---|
| 149 | /*
|
---|
| 150 | * (non-Javadoc)
|
---|
| 151 | *
|
---|
| 152 | * @see java.lang.Object#toString()
|
---|
| 153 | */
|
---|
| 154 | @Override
|
---|
| 155 | public synchronized String toString() {
|
---|
| 156 | if (children == null) {
|
---|
| 157 | return name + "(" + id + ")";
|
---|
[439] | 158 | }
|
---|
[557] | 159 | else {
|
---|
| 160 | return name + "(" + id + ", " + children.size() + " children)";
|
---|
| 161 | }
|
---|
[439] | 162 | }
|
---|
| 163 |
|
---|
[557] | 164 | /**
|
---|
| 165 | * TODO: comment
|
---|
| 166 | *
|
---|
| 167 | * @param i
|
---|
| 168 | * @return
|
---|
| 169 | */
|
---|
| 170 | void setDescription(String description) {
|
---|
| 171 | this.description = description;
|
---|
| 172 | }
|
---|
[439] | 173 |
|
---|
[557] | 174 | /**
|
---|
| 175 | *
|
---|
| 176 | */
|
---|
| 177 | synchronized void addChild(ITaskTreeNode child) {
|
---|
| 178 | if (children == null) {
|
---|
| 179 | children = new ArrayList<ITaskTreeNode>();
|
---|
| 180 | }
|
---|
[439] | 181 |
|
---|
[557] | 182 | children.add(child);
|
---|
[439] | 183 | }
|
---|
[557] | 184 |
|
---|
| 185 | /**
|
---|
| 186 | *
|
---|
| 187 | */
|
---|
| 188 | synchronized void addChild(int index, ITaskTreeNode child) {
|
---|
| 189 | if (children == null) {
|
---|
| 190 | children = new ArrayList<ITaskTreeNode>();
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | children.add(index, child);
|
---|
[439] | 194 | }
|
---|
| 195 |
|
---|
[557] | 196 | /**
|
---|
| 197 | * TODO: comment
|
---|
| 198 | *
|
---|
| 199 | * @param i
|
---|
| 200 | * @return
|
---|
| 201 | */
|
---|
| 202 | synchronized ITaskTreeNode removeChild(int index) {
|
---|
| 203 | return children.remove(index);
|
---|
[439] | 204 | }
|
---|
| 205 |
|
---|
[557] | 206 | /*
|
---|
| 207 | * (non-Javadoc)
|
---|
| 208 | *
|
---|
| 209 | * @see java.lang.Object#clone()
|
---|
| 210 | */
|
---|
| 211 | @Override
|
---|
[1074] | 212 | public synchronized ITaskTreeNode clone() {
|
---|
[557] | 213 | TaskTreeNode clone = null;
|
---|
| 214 | try {
|
---|
| 215 | clone = (TaskTreeNode) super.clone();
|
---|
| 216 |
|
---|
| 217 | if (children != null) {
|
---|
| 218 | clone.children = new ArrayList<ITaskTreeNode>();
|
---|
| 219 |
|
---|
| 220 | for (ITaskTreeNode child : children) {
|
---|
| 221 | clone.children.add(child.clone());
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[470] | 225 | }
|
---|
[557] | 226 | catch (CloneNotSupportedException e) {
|
---|
| 227 | // this should never happen. Therefore simply dump the exception
|
---|
| 228 | e.printStackTrace();
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | return clone;
|
---|
[467] | 232 | }
|
---|
[439] | 233 |
|
---|
| 234 | }
|
---|