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

Last change on this file since 1074 was 1074, checked in by pharms, 11 years ago
  • prevented a findbugs warning
  • Property svn:executable set to *
File size: 4.8 KB
Line 
1package de.ugoe.cs.autoquest.tasktrees.treeimpl;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
7
8/**
9 * TODO comment
10 *
11 * @version $Revision: $ $Date: $
12 * @author 2011, last modified by $Author: $
13 */
14public class TaskTreeNode implements ITaskTreeNode {
15    /** */
16    private static int temporalId = 0;
17
18    /** */
19    private String name;
20
21    /** */
22    private String description;
23
24    /** */
25    private int id;
26
27    /** children */
28    private List<ITaskTreeNode> children;
29
30    /**
31     *
32     */
33    public TaskTreeNode(String name) {
34        this.name = name;
35        id = getNewId();
36    }
37
38    /**
39     * TODO: comment
40     *
41     * @return
42     */
43    private static synchronized int getNewId() {
44        if (temporalId == Integer.MAX_VALUE) {
45            temporalId = 0;
46        }
47
48        return temporalId++;
49    }
50
51    /**
52     * @return Returns the name.
53     */
54    public String getName() {
55        return name;
56    }
57
58    /*
59     * (non-Javadoc)
60     *
61     * @see de.ugoe.cs.tasktree.treeifc.TaskTreeNode#getDescription()
62     */
63    @Override
64    public String getDescription() {
65        return description;
66    }
67
68    /**
69     *
70     */
71    public synchronized List<ITaskTreeNode> getChildren() {
72        if ((children == null) || (children.size() == 0)) {
73            return new ArrayList<ITaskTreeNode>();
74        }
75
76        return children.subList(0, children.size());
77    }
78
79    /*
80     * (non-Javadoc)
81     *
82     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode)
83     */
84    @Override
85    public boolean equals(ITaskTreeNode taskTreeNode) {
86        if (!this.getClass().isInstance(taskTreeNode)) {
87            return false;
88        }
89
90        if (taskTreeNode.hashCode() != hashCode()) {
91            return false;
92        }
93
94        TaskTreeNode other = (TaskTreeNode) taskTreeNode;
95
96        if (id != other.id) {
97            return false;
98        }
99
100        if (!name.equals(other.name)) {
101            return false;
102        }
103
104        synchronized (other) {
105            if (children == null) {
106                return (other.children == null);
107            }
108            else if (other.children == null) {
109                return (children == null);
110            }
111            else if (other.children.size() != children.size()) {
112                return false;
113            }
114
115            for (int i = 0; i < children.size(); i++) {
116                if (!children.get(i).equals(other.children.get(i))) {
117                    return false;
118                }
119            }
120        }
121
122        return true;
123    }
124
125    /*
126     * (non-Javadoc)
127     *
128     * @see java.lang.Object#hashCode()
129     */
130    @Override
131    public synchronized int hashCode() {
132        return getClass().getSimpleName().hashCode();
133    }
134
135    /*
136     * (non-Javadoc)
137     *
138     * @see java.lang.Object#toString()
139     */
140    @Override
141    public synchronized String toString() {
142        if (children == null) {
143            return name + "(" + id + ")";
144        }
145        else {
146            return name + "(" + id + ", " + children.size() + " children)";
147        }
148    }
149
150    /**
151     * TODO: comment
152     *
153     * @param i
154     * @return
155     */
156    void setDescription(String description) {
157        this.description = description;
158    }
159
160    /**
161     *
162     */
163    synchronized void addChild(ITaskTreeNode child) {
164        if (children == null) {
165            children = new ArrayList<ITaskTreeNode>();
166        }
167
168        children.add(child);
169    }
170
171    /**
172     *
173     */
174    synchronized void addChild(int index, ITaskTreeNode child) {
175        if (children == null) {
176            children = new ArrayList<ITaskTreeNode>();
177        }
178
179        children.add(index, child);
180    }
181
182    /**
183     * TODO: comment
184     *
185     * @param i
186     * @return
187     */
188    synchronized ITaskTreeNode removeChild(int index) {
189        return children.remove(index);
190    }
191
192    /*
193     * (non-Javadoc)
194     *
195     * @see java.lang.Object#clone()
196     */
197    @Override
198    public synchronized ITaskTreeNode clone() {
199        TaskTreeNode clone = null;
200        try {
201            clone = (TaskTreeNode) super.clone();
202
203            if (children != null) {
204                clone.children = new ArrayList<ITaskTreeNode>();
205
206                for (ITaskTreeNode child : children) {
207                    clone.children.add(child.clone());
208                }
209            }
210
211        }
212        catch (CloneNotSupportedException e) {
213            // this should never happen. Therefore simply dump the exception
214            e.printStackTrace();
215        }
216
217        return clone;
218    }
219
220}
Note: See TracBrowser for help on using the repository browser.