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