1 | //-------------------------------------------------------------------------------------------------
|
---|
2 | // Module : $RCSfile: TreeNode.java,v $
|
---|
3 | // Version : $Revision: 0.0 $ $Author: Patrick $ $Date: 06.11.2011 11:00:46 $
|
---|
4 | // Project : TaskTreePerformanceTest
|
---|
5 | // Creation : 2011 by Patrick
|
---|
6 | // Copyright : Patrick Harms, 2011
|
---|
7 | //-------------------------------------------------------------------------------------------------
|
---|
8 |
|
---|
9 | package de.ugoe.cs.quest.tasktrees.treeimpl;
|
---|
10 |
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode;
|
---|
15 |
|
---|
16 |
|
---|
17 | //-------------------------------------------------------------------------------------------------
|
---|
18 | /**
|
---|
19 | * TODO comment
|
---|
20 | *
|
---|
21 | * @version $Revision: $ $Date: $
|
---|
22 | * @author 2011, last modified by $Author: $
|
---|
23 | */
|
---|
24 | //-------------------------------------------------------------------------------------------------
|
---|
25 | public class TaskTreeNodeImpl implements TaskTreeNode
|
---|
26 | {
|
---|
27 | /** */
|
---|
28 | private static int sTemporalId = 0;
|
---|
29 |
|
---|
30 | /** */
|
---|
31 | private String mName;
|
---|
32 |
|
---|
33 | /** */
|
---|
34 | private String mDescription;
|
---|
35 |
|
---|
36 | /** */
|
---|
37 | private int mId;
|
---|
38 |
|
---|
39 | /** children */
|
---|
40 | private List<TaskTreeNode> mChildren;
|
---|
41 |
|
---|
42 | //-----------------------------------------------------------------------------------------------
|
---|
43 | /**
|
---|
44 | */
|
---|
45 | //-----------------------------------------------------------------------------------------------
|
---|
46 | TaskTreeNodeImpl(String name)
|
---|
47 | {
|
---|
48 | mName = name;
|
---|
49 | mId = getNewId();
|
---|
50 | }
|
---|
51 |
|
---|
52 | //-----------------------------------------------------------------------------------------------
|
---|
53 | /**
|
---|
54 | * TODO: comment
|
---|
55 | *
|
---|
56 | * @return
|
---|
57 | */
|
---|
58 | //-----------------------------------------------------------------------------------------------
|
---|
59 | private static synchronized int getNewId()
|
---|
60 | {
|
---|
61 | if (sTemporalId == Integer.MAX_VALUE)
|
---|
62 | {
|
---|
63 | sTemporalId = 0;
|
---|
64 | }
|
---|
65 |
|
---|
66 | return sTemporalId++;
|
---|
67 | }
|
---|
68 |
|
---|
69 | //-----------------------------------------------------------------------------------------------
|
---|
70 | /**
|
---|
71 | * @return Returns the name.
|
---|
72 | */
|
---|
73 | //-----------------------------------------------------------------------------------------------
|
---|
74 | public String getName()
|
---|
75 | {
|
---|
76 | return mName;
|
---|
77 | }
|
---|
78 |
|
---|
79 | //-----------------------------------------------------------------------------------------------
|
---|
80 | /* (non-Javadoc)
|
---|
81 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeNode#getDescription()
|
---|
82 | */
|
---|
83 | //-----------------------------------------------------------------------------------------------
|
---|
84 | @Override
|
---|
85 | public String getDescription()
|
---|
86 | {
|
---|
87 | return mDescription;
|
---|
88 | }
|
---|
89 |
|
---|
90 | //-----------------------------------------------------------------------------------------------
|
---|
91 | /**
|
---|
92 | */
|
---|
93 | //-----------------------------------------------------------------------------------------------
|
---|
94 | public synchronized List<TaskTreeNode> getChildren()
|
---|
95 | {
|
---|
96 | if ((mChildren == null) || (mChildren.size() == 0))
|
---|
97 | {
|
---|
98 | return new ArrayList<TaskTreeNode>();
|
---|
99 | }
|
---|
100 |
|
---|
101 | return mChildren.subList(0, mChildren.size());
|
---|
102 | }
|
---|
103 |
|
---|
104 | //-----------------------------------------------------------------------------------------------
|
---|
105 | /* (non-Javadoc)
|
---|
106 | * @see java.lang.Object#equals(java.lang.Object)
|
---|
107 | */
|
---|
108 | //-----------------------------------------------------------------------------------------------
|
---|
109 | @Override
|
---|
110 | public synchronized boolean equals(Object other)
|
---|
111 | {
|
---|
112 | if (!this.getClass().isInstance(other))
|
---|
113 | {
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | return equals((TaskTreeNodeImpl) other);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | //-----------------------------------------------------------------------------------------------
|
---|
123 | /* (non-Javadoc)
|
---|
124 | * @see de.ugoe.cs.tasktree.treeifc.TaskTreeNode#equals(de.ugoe.cs.tasktree.treeifc.TaskTreeNode)
|
---|
125 | */
|
---|
126 | //-----------------------------------------------------------------------------------------------
|
---|
127 | @Override
|
---|
128 | public boolean equals(TaskTreeNode other)
|
---|
129 | {
|
---|
130 | if (!this.getClass().isInstance(other))
|
---|
131 | {
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | return equals((TaskTreeNodeImpl) other);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | //-----------------------------------------------------------------------------------------------
|
---|
141 | /* (non-Javadoc)
|
---|
142 | * @see java.lang.Object#equals(java.lang.Object)
|
---|
143 | */
|
---|
144 | //-----------------------------------------------------------------------------------------------
|
---|
145 | public synchronized boolean equals(TaskTreeNodeImpl other)
|
---|
146 | {
|
---|
147 | if (!this.getClass().isInstance(other))
|
---|
148 | {
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (other.hashCode() != hashCode())
|
---|
153 | {
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (mId != other.mId)
|
---|
158 | {
|
---|
159 | return false;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (!mName.equals(other.mName))
|
---|
163 | {
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 |
|
---|
167 | synchronized (other)
|
---|
168 | {
|
---|
169 | if ((other.mChildren == null) && (mChildren == null))
|
---|
170 | {
|
---|
171 | return true;
|
---|
172 | }
|
---|
173 | else if (((other.mChildren == null) && (mChildren != null)) ||
|
---|
174 | ((other.mChildren != null) && (mChildren == null)))
|
---|
175 | {
|
---|
176 | return false;
|
---|
177 | }
|
---|
178 | else if (other.mChildren.size() != mChildren.size())
|
---|
179 | {
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 |
|
---|
183 | for (int i = 0; i < mChildren.size(); i++)
|
---|
184 | {
|
---|
185 | if (!mChildren.get(i).equals(other.mChildren.get(i)))
|
---|
186 | {
|
---|
187 | return false;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | return true;
|
---|
193 | }
|
---|
194 |
|
---|
195 | //-----------------------------------------------------------------------------------------------
|
---|
196 | /* (non-Javadoc)
|
---|
197 | * @see java.lang.Object#hashCode()
|
---|
198 | */
|
---|
199 | //-----------------------------------------------------------------------------------------------
|
---|
200 | @Override
|
---|
201 | public synchronized int hashCode()
|
---|
202 | {
|
---|
203 | return getClass().getSimpleName().hashCode();
|
---|
204 | }
|
---|
205 |
|
---|
206 | //-----------------------------------------------------------------------------------------------
|
---|
207 | /* (non-Javadoc)
|
---|
208 | * @see java.lang.Object#toString()
|
---|
209 | */
|
---|
210 | //-----------------------------------------------------------------------------------------------
|
---|
211 | @Override
|
---|
212 | public synchronized String toString()
|
---|
213 | {
|
---|
214 | if (mChildren == null)
|
---|
215 | {
|
---|
216 | return mName + "(" + mId + ")";
|
---|
217 | }
|
---|
218 | else
|
---|
219 | {
|
---|
220 | return mName + "(" + mId + ", " + mChildren.size() + " children)";
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | //-----------------------------------------------------------------------------------------------
|
---|
225 | /**
|
---|
226 | * TODO: comment
|
---|
227 | *
|
---|
228 | * @param i
|
---|
229 | * @return
|
---|
230 | */
|
---|
231 | //-----------------------------------------------------------------------------------------------
|
---|
232 | void setDescription(String description)
|
---|
233 | {
|
---|
234 | mDescription = description;
|
---|
235 | }
|
---|
236 |
|
---|
237 | //-----------------------------------------------------------------------------------------------
|
---|
238 | /**
|
---|
239 | */
|
---|
240 | //-----------------------------------------------------------------------------------------------
|
---|
241 | synchronized void addChild(TaskTreeNode child)
|
---|
242 | {
|
---|
243 | if (mChildren == null)
|
---|
244 | {
|
---|
245 | mChildren = new ArrayList<TaskTreeNode>();
|
---|
246 | }
|
---|
247 |
|
---|
248 | mChildren.add(child);
|
---|
249 | }
|
---|
250 |
|
---|
251 | //-----------------------------------------------------------------------------------------------
|
---|
252 | /**
|
---|
253 | */
|
---|
254 | //-----------------------------------------------------------------------------------------------
|
---|
255 | synchronized void addChild(int index, TaskTreeNode child)
|
---|
256 | {
|
---|
257 | if (mChildren == null)
|
---|
258 | {
|
---|
259 | mChildren = new ArrayList<TaskTreeNode>();
|
---|
260 | }
|
---|
261 |
|
---|
262 | mChildren.add(index, child);
|
---|
263 | }
|
---|
264 |
|
---|
265 | //-----------------------------------------------------------------------------------------------
|
---|
266 | /**
|
---|
267 | * TODO: comment
|
---|
268 | *
|
---|
269 | * @param i
|
---|
270 | * @return
|
---|
271 | */
|
---|
272 | //-----------------------------------------------------------------------------------------------
|
---|
273 | synchronized TaskTreeNode removeChild(int index)
|
---|
274 | {
|
---|
275 | return mChildren.remove(index);
|
---|
276 | }
|
---|
277 |
|
---|
278 | //-----------------------------------------------------------------------------------------------
|
---|
279 | /* (non-Javadoc)
|
---|
280 | * @see java.lang.Object#clone()
|
---|
281 | */
|
---|
282 | //-----------------------------------------------------------------------------------------------
|
---|
283 | @Override
|
---|
284 | public TaskTreeNode clone()
|
---|
285 | {
|
---|
286 | TaskTreeNodeImpl clone = null;
|
---|
287 | try
|
---|
288 | {
|
---|
289 | clone = (TaskTreeNodeImpl) super.clone();
|
---|
290 |
|
---|
291 | clone.mChildren = new ArrayList<TaskTreeNode>();
|
---|
292 |
|
---|
293 | for (TaskTreeNode child : mChildren)
|
---|
294 | {
|
---|
295 | clone.mChildren.add(child.clone());
|
---|
296 | }
|
---|
297 |
|
---|
298 | }
|
---|
299 | catch (CloneNotSupportedException e)
|
---|
300 | {
|
---|
301 | // this should never happen. Therefore simply dump the exception
|
---|
302 | e.printStackTrace();
|
---|
303 | }
|
---|
304 |
|
---|
305 | return clone;
|
---|
306 | }
|
---|
307 |
|
---|
308 | }
|
---|