[439] | 1 | // Module : $RCSfile: TreeNode.java,v $
|
---|
| 2 | // Version : $Revision: 0.0 $ $Author: Patrick $ $Date: 06.11.2011 11:00:46 $
|
---|
| 3 | // Project : TaskTreePerformanceTest
|
---|
| 4 | // Creation : 2011 by Patrick
|
---|
| 5 | // Copyright : Patrick Harms, 2011
|
---|
| 6 |
|
---|
| 7 | package de.ugoe.cs.quest.tasktrees.treeimpl;
|
---|
| 8 |
|
---|
| 9 | import java.util.ArrayList;
|
---|
| 10 | import java.util.List;
|
---|
| 11 |
|
---|
[557] | 12 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
|
---|
[439] | 13 |
|
---|
| 14 | /**
|
---|
| 15 | * TODO comment
|
---|
[557] | 16 | *
|
---|
[439] | 17 | * @version $Revision: $ $Date: $
|
---|
[557] | 18 | * @author 2011, last modified by $Author: $
|
---|
[439] | 19 | */
|
---|
[557] | 20 | public class TaskTreeNode implements ITaskTreeNode {
|
---|
| 21 | /** */
|
---|
| 22 | private static int temporalId = 0;
|
---|
[439] | 23 |
|
---|
[557] | 24 | /** */
|
---|
| 25 | private String name;
|
---|
[439] | 26 |
|
---|
[557] | 27 | /** */
|
---|
| 28 | private String description;
|
---|
[439] | 29 |
|
---|
[557] | 30 | /** */
|
---|
| 31 | private int id;
|
---|
| 32 |
|
---|
| 33 | /** children */
|
---|
| 34 | private List<ITaskTreeNode> children;
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | *
|
---|
| 38 | */
|
---|
| 39 | public TaskTreeNode(String name) {
|
---|
| 40 | this.name = name;
|
---|
| 41 | id = getNewId();
|
---|
[439] | 42 | }
|
---|
| 43 |
|
---|
[557] | 44 | /**
|
---|
| 45 | * TODO: comment
|
---|
| 46 | *
|
---|
| 47 | * @return
|
---|
| 48 | */
|
---|
| 49 | private static synchronized int getNewId() {
|
---|
| 50 | if (temporalId == Integer.MAX_VALUE) {
|
---|
| 51 | temporalId = 0;
|
---|
| 52 | }
|
---|
[439] | 53 |
|
---|
[557] | 54 | return temporalId++;
|
---|
| 55 | }
|
---|
[439] | 56 |
|
---|
[557] | 57 | /**
|
---|
| 58 | * @return Returns the name.
|
---|
| 59 | */
|
---|
| 60 | public String getName() {
|
---|
| 61 | return name;
|
---|
[439] | 62 | }
|
---|
[557] | 63 |
|
---|
| 64 | /*
|
---|
| 65 | * (non-Javadoc)
|
---|
| 66 | *
|
---|
| 67 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeNode#getDescription()
|
---|
| 68 | */
|
---|
| 69 | @Override
|
---|
| 70 | public String getDescription() {
|
---|
| 71 | return description;
|
---|
[439] | 72 | }
|
---|
[557] | 73 |
|
---|
| 74 | /**
|
---|
| 75 | *
|
---|
| 76 | */
|
---|
| 77 | public synchronized List<ITaskTreeNode> getChildren() {
|
---|
| 78 | if ((children == null) || (children.size() == 0)) {
|
---|
| 79 | return new ArrayList<ITaskTreeNode>();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | return children.subList(0, children.size());
|
---|
[439] | 83 | }
|
---|
[557] | 84 |
|
---|
| 85 | /*
|
---|
| 86 | * (non-Javadoc)
|
---|
| 87 | *
|
---|
| 88 | * @see de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode)
|
---|
| 89 | */
|
---|
| 90 | @Override
|
---|
| 91 | public boolean equals(ITaskTreeNode taskTreeNode) {
|
---|
| 92 | if (!this.getClass().isInstance(taskTreeNode)) {
|
---|
| 93 | return false;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | if (taskTreeNode.hashCode() != hashCode()) {
|
---|
| 97 | return false;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | TaskTreeNode other = (TaskTreeNode) taskTreeNode;
|
---|
| 101 |
|
---|
| 102 | if (id != other.id) {
|
---|
| 103 | return false;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | if (!name.equals(other.name)) {
|
---|
| 107 | return false;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | synchronized (other) {
|
---|
| 111 | if (children == null) {
|
---|
| 112 | return (other.children == null);
|
---|
| 113 | }
|
---|
| 114 | else if (other.children == null) {
|
---|
| 115 | return (children == null);
|
---|
| 116 | }
|
---|
| 117 | else if (other.children.size() != children.size()) {
|
---|
| 118 | return false;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | for (int i = 0; i < children.size(); i++) {
|
---|
| 122 | if (!children.get(i).equals(other.children.get(i))) {
|
---|
| 123 | return false;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | return true;
|
---|
[439] | 129 | }
|
---|
[557] | 130 |
|
---|
| 131 | /*
|
---|
| 132 | * (non-Javadoc)
|
---|
| 133 | *
|
---|
| 134 | * @see java.lang.Object#hashCode()
|
---|
| 135 | */
|
---|
| 136 | @Override
|
---|
| 137 | public synchronized int hashCode() {
|
---|
| 138 | return getClass().getSimpleName().hashCode();
|
---|
[439] | 139 | }
|
---|
[557] | 140 |
|
---|
| 141 | /*
|
---|
| 142 | * (non-Javadoc)
|
---|
| 143 | *
|
---|
| 144 | * @see java.lang.Object#toString()
|
---|
| 145 | */
|
---|
| 146 | @Override
|
---|
| 147 | public synchronized String toString() {
|
---|
| 148 | if (children == null) {
|
---|
| 149 | return name + "(" + id + ")";
|
---|
[439] | 150 | }
|
---|
[557] | 151 | else {
|
---|
| 152 | return name + "(" + id + ", " + children.size() + " children)";
|
---|
| 153 | }
|
---|
[439] | 154 | }
|
---|
| 155 |
|
---|
[557] | 156 | /**
|
---|
| 157 | * TODO: comment
|
---|
| 158 | *
|
---|
| 159 | * @param i
|
---|
| 160 | * @return
|
---|
| 161 | */
|
---|
| 162 | void setDescription(String description) {
|
---|
| 163 | this.description = description;
|
---|
| 164 | }
|
---|
[439] | 165 |
|
---|
[557] | 166 | /**
|
---|
| 167 | *
|
---|
| 168 | */
|
---|
| 169 | synchronized void addChild(ITaskTreeNode child) {
|
---|
| 170 | if (children == null) {
|
---|
| 171 | children = new ArrayList<ITaskTreeNode>();
|
---|
| 172 | }
|
---|
[439] | 173 |
|
---|
[557] | 174 | children.add(child);
|
---|
[439] | 175 | }
|
---|
[557] | 176 |
|
---|
| 177 | /**
|
---|
| 178 | *
|
---|
| 179 | */
|
---|
| 180 | synchronized void addChild(int index, ITaskTreeNode child) {
|
---|
| 181 | if (children == null) {
|
---|
| 182 | children = new ArrayList<ITaskTreeNode>();
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | children.add(index, child);
|
---|
[439] | 186 | }
|
---|
| 187 |
|
---|
[557] | 188 | /**
|
---|
| 189 | * TODO: comment
|
---|
| 190 | *
|
---|
| 191 | * @param i
|
---|
| 192 | * @return
|
---|
| 193 | */
|
---|
| 194 | synchronized ITaskTreeNode removeChild(int index) {
|
---|
| 195 | return children.remove(index);
|
---|
[439] | 196 | }
|
---|
| 197 |
|
---|
[557] | 198 | /*
|
---|
| 199 | * (non-Javadoc)
|
---|
| 200 | *
|
---|
| 201 | * @see java.lang.Object#clone()
|
---|
| 202 | */
|
---|
| 203 | @Override
|
---|
| 204 | public ITaskTreeNode clone() {
|
---|
| 205 | TaskTreeNode clone = null;
|
---|
| 206 | try {
|
---|
| 207 | clone = (TaskTreeNode) super.clone();
|
---|
| 208 |
|
---|
| 209 | if (children != null) {
|
---|
| 210 | clone.children = new ArrayList<ITaskTreeNode>();
|
---|
| 211 |
|
---|
| 212 | for (ITaskTreeNode child : children) {
|
---|
| 213 | clone.children.add(child.clone());
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[470] | 217 | }
|
---|
[557] | 218 | catch (CloneNotSupportedException e) {
|
---|
| 219 | // this should never happen. Therefore simply dump the exception
|
---|
| 220 | e.printStackTrace();
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | return clone;
|
---|
[467] | 224 | }
|
---|
[439] | 225 |
|
---|
| 226 | }
|
---|