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

Last change on this file since 557 was 557, checked in by pharms, 12 years ago
  • adapted task tree creation stuff to more general event handling
  • Property svn:executable set to *
File size: 5.0 KB
Line 
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
7package de.ugoe.cs.quest.tasktrees.treeimpl;
8
9import java.util.ArrayList;
10import java.util.List;
11
12import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
13
14/**
15 * TODO comment
16 *
17 * @version $Revision: $ $Date: $
18 * @author 2011, last modified by $Author: $
19 */
20public class TaskTreeNode implements ITaskTreeNode {
21    /** */
22    private static int temporalId = 0;
23
24    /** */
25    private String name;
26
27    /** */
28    private String description;
29
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();
42    }
43
44    /**
45     * TODO: comment
46     *
47     * @return
48     */
49    private static synchronized int getNewId() {
50        if (temporalId == Integer.MAX_VALUE) {
51            temporalId = 0;
52        }
53
54        return temporalId++;
55    }
56
57    /**
58     * @return Returns the name.
59     */
60    public String getName() {
61        return name;
62    }
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;
72    }
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());
83    }
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;
129    }
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();
139    }
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 + ")";
150        }
151        else {
152            return name + "(" + id + ", " + children.size() + " children)";
153        }
154    }
155
156    /**
157     * TODO: comment
158     *
159     * @param i
160     * @return
161     */
162    void setDescription(String description) {
163        this.description = description;
164    }
165
166    /**
167     *
168     */
169    synchronized void addChild(ITaskTreeNode child) {
170        if (children == null) {
171            children = new ArrayList<ITaskTreeNode>();
172        }
173
174        children.add(child);
175    }
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);
186    }
187
188    /**
189     * TODO: comment
190     *
191     * @param i
192     * @return
193     */
194    synchronized ITaskTreeNode removeChild(int index) {
195        return children.remove(index);
196    }
197
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
217        }
218        catch (CloneNotSupportedException e) {
219            // this should never happen. Therefore simply dump the exception
220            e.printStackTrace();
221        }
222
223        return clone;
224    }
225
226}
Note: See TracBrowser for help on using the repository browser.