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 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.treeimpl;
|
---|
16 |
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * TODO comment
|
---|
24 | *
|
---|
25 | * @version $Revision: $ $Date: $
|
---|
26 | * @author 2011, last modified by $Author: $
|
---|
27 | */
|
---|
28 | public class TaskTreeNode implements ITaskTreeNode {
|
---|
29 | /** */
|
---|
30 | private static int temporalId = 0;
|
---|
31 |
|
---|
32 | /** */
|
---|
33 | private String name;
|
---|
34 |
|
---|
35 | /** */
|
---|
36 | private String description;
|
---|
37 |
|
---|
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();
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * TODO: comment
|
---|
54 | *
|
---|
55 | * @return
|
---|
56 | */
|
---|
57 | private static synchronized int getNewId() {
|
---|
58 | if (temporalId == Integer.MAX_VALUE) {
|
---|
59 | temporalId = 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | return temporalId++;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * @return Returns the name.
|
---|
67 | */
|
---|
68 | public String getName() {
|
---|
69 | return name;
|
---|
70 | }
|
---|
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;
|
---|
80 | }
|
---|
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());
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * (non-Javadoc)
|
---|
95 | *
|
---|
96 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode)
|
---|
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;
|
---|
137 | }
|
---|
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();
|
---|
147 | }
|
---|
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 + ")";
|
---|
158 | }
|
---|
159 | else {
|
---|
160 | return name + "(" + id + ", " + children.size() + " children)";
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * TODO: comment
|
---|
166 | *
|
---|
167 | * @param i
|
---|
168 | * @return
|
---|
169 | */
|
---|
170 | void setDescription(String description) {
|
---|
171 | this.description = description;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | *
|
---|
176 | */
|
---|
177 | synchronized void addChild(ITaskTreeNode child) {
|
---|
178 | if (children == null) {
|
---|
179 | children = new ArrayList<ITaskTreeNode>();
|
---|
180 | }
|
---|
181 |
|
---|
182 | children.add(child);
|
---|
183 | }
|
---|
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);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * TODO: comment
|
---|
198 | *
|
---|
199 | * @param i
|
---|
200 | * @return
|
---|
201 | */
|
---|
202 | synchronized ITaskTreeNode removeChild(int index) {
|
---|
203 | return children.remove(index);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /*
|
---|
207 | * (non-Javadoc)
|
---|
208 | *
|
---|
209 | * @see java.lang.Object#clone()
|
---|
210 | */
|
---|
211 | @Override
|
---|
212 | public synchronized ITaskTreeNode clone() {
|
---|
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 |
|
---|
225 | }
|
---|
226 | catch (CloneNotSupportedException e) {
|
---|
227 | // this should never happen. Therefore simply dump the exception
|
---|
228 | e.printStackTrace();
|
---|
229 | }
|
---|
230 |
|
---|
231 | return clone;
|
---|
232 | }
|
---|
233 |
|
---|
234 | }
|
---|