source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/treeimpl/TaskTreeNodeImpl.java @ 470

Last change on this file since 470 was 470, checked in by pharms, 12 years ago

removed NullPointer?

  • Property svn:executable set to *
File size: 7.5 KB
Line 
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
9package de.ugoe.cs.quest.tasktrees.treeimpl;
10
11import java.util.ArrayList;
12import java.util.List;
13
14import 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//-------------------------------------------------------------------------------------------------
25public 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 de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode)
107   */
108  //-----------------------------------------------------------------------------------------------
109  @Override
110  public boolean equals(TaskTreeNode taskTreeNode)
111  {
112    if (!this.getClass().isInstance(taskTreeNode))
113    {
114      return false;
115    }
116   
117    if (taskTreeNode.hashCode() != hashCode())
118    {
119      return false;
120    }
121   
122    TaskTreeNodeImpl other = (TaskTreeNodeImpl) taskTreeNode;
123   
124    if (mId != other.mId)
125    {
126      return false;
127    }
128   
129    if (!mName.equals(other.mName))
130    {
131      return false;
132    }
133   
134    synchronized (other)
135    {
136      if (mChildren == null)
137      {
138        return (other.mChildren == null);
139      }
140      else if (other.mChildren == null)
141      {
142        return (mChildren == null);
143      }
144      else if (other.mChildren.size() != mChildren.size())
145      {
146        return false;
147      }
148     
149      for (int i = 0; i < mChildren.size(); i++)
150      {
151        if (!mChildren.get(i).equals(other.mChildren.get(i)))
152        {
153          return false;
154        }
155      }
156    }
157
158    return true;
159  }
160
161  //-----------------------------------------------------------------------------------------------
162  /* (non-Javadoc)
163   * @see java.lang.Object#hashCode()
164   */
165  //-----------------------------------------------------------------------------------------------
166  @Override
167  public synchronized int hashCode()
168  {
169    return getClass().getSimpleName().hashCode();
170  }
171
172  //-----------------------------------------------------------------------------------------------
173  /* (non-Javadoc)
174   * @see java.lang.Object#toString()
175   */
176  //-----------------------------------------------------------------------------------------------
177  @Override
178  public synchronized String toString()
179  {
180    if (mChildren == null)
181    {
182      return mName + "(" + mId + ")";
183    }
184    else
185    {
186      return mName + "(" + mId + ", " + mChildren.size() + " children)";
187    }
188  }
189 
190  //-----------------------------------------------------------------------------------------------
191  /**
192   * TODO: comment
193   *
194   * @param i
195   * @return
196   */
197  //-----------------------------------------------------------------------------------------------
198  void setDescription(String description)
199  {
200    mDescription = description;
201  }
202
203  //-----------------------------------------------------------------------------------------------
204  /**
205   */
206  //-----------------------------------------------------------------------------------------------
207  synchronized void addChild(TaskTreeNode child)
208  {
209    if (mChildren == null)
210    {
211      mChildren = new ArrayList<TaskTreeNode>();
212    }
213   
214    mChildren.add(child);
215  }
216 
217  //-----------------------------------------------------------------------------------------------
218  /**
219   */
220  //-----------------------------------------------------------------------------------------------
221  synchronized void addChild(int index, TaskTreeNode child)
222  {
223    if (mChildren == null)
224    {
225      mChildren = new ArrayList<TaskTreeNode>();
226    }
227   
228    mChildren.add(index, child);
229  }
230 
231  //-----------------------------------------------------------------------------------------------
232  /**
233   * TODO: comment
234   *
235   * @param i
236   * @return
237   */
238  //-----------------------------------------------------------------------------------------------
239  synchronized TaskTreeNode removeChild(int index)
240  {
241    return mChildren.remove(index);
242  }
243
244  //-----------------------------------------------------------------------------------------------
245  /* (non-Javadoc)
246   * @see java.lang.Object#clone()
247   */
248  //-----------------------------------------------------------------------------------------------
249  @Override
250  public TaskTreeNode clone()
251  {
252    TaskTreeNodeImpl clone = null;
253    try
254    {
255      clone = (TaskTreeNodeImpl) super.clone();
256     
257      if (mChildren != null)
258      {
259        clone.mChildren = new ArrayList<TaskTreeNode>();
260     
261        for (TaskTreeNode child : mChildren)
262        {
263          clone.mChildren.add(child.clone());
264        }
265      }
266     
267    }
268    catch (CloneNotSupportedException e)
269    {
270      // this should never happen. Therefore simply dump the exception
271      e.printStackTrace();
272    }
273   
274    return clone;
275  }
276
277}
Note: See TracBrowser for help on using the repository browser.