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

Last change on this file since 1132 was 1126, checked in by pharms, 11 years ago
  • extended task tree model with support for optionalities
  • improved performance of task tree handling
  • improved visualization of task trees
  • Property svn:executable set to *
File size: 5.7 KB
RevLine 
[1113]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
[922]15package de.ugoe.cs.autoquest.tasktrees.treeimpl;
[439]16
[1126]17import java.util.Collections;
18import java.util.LinkedList;
[439]19import java.util.List;
20
[922]21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
[439]22
23/**
24 * TODO comment
[557]25 *
[439]26 * @version $Revision: $ $Date: $
[557]27 * @author 2011, last modified by $Author: $
[439]28 */
[557]29public class TaskTreeNode implements ITaskTreeNode {
30    /** */
31    private static int temporalId = 0;
[439]32
[557]33    /** */
34    private String name;
[439]35
[557]36    /** */
37    private String description;
[439]38
[557]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();
[439]51    }
52
[557]53    /**
54     * TODO: comment
55     *
56     * @return
57     */
58    private static synchronized int getNewId() {
59        if (temporalId == Integer.MAX_VALUE) {
60            temporalId = 0;
61        }
[439]62
[557]63        return temporalId++;
64    }
[439]65
[557]66    /**
67     * @return Returns the name.
68     */
69    public String getName() {
70        return name;
[439]71    }
[557]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;
[439]81    }
[557]82
83    /**
84     *
85     */
86    public synchronized List<ITaskTreeNode> getChildren() {
87        if ((children == null) || (children.size() == 0)) {
[1126]88            return new LinkedList<ITaskTreeNode>();
[557]89        }
90
[1126]91        return Collections.unmodifiableList(children);
[439]92    }
[557]93
94    /*
95     * (non-Javadoc)
96     *
[922]97     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode)
[557]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;
[439]138    }
[557]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();
[439]148    }
[557]149
150    /*
151     * (non-Javadoc)
152     *
153     * @see java.lang.Object#toString()
154     */
155    @Override
[1126]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();
[439]175    }
176
[557]177    /**
178     * TODO: comment
179     *
180     * @param i
181     * @return
182     */
183    void setDescription(String description) {
184        this.description = description;
185    }
[439]186
[557]187    /**
188     *
189     */
190    synchronized void addChild(ITaskTreeNode child) {
191        if (children == null) {
[1126]192            children = new LinkedList<ITaskTreeNode>();
[557]193        }
[439]194
[557]195        children.add(child);
[439]196    }
[557]197
198    /**
199     *
200     */
201    synchronized void addChild(int index, ITaskTreeNode child) {
202        if (children == null) {
[1126]203            children = new LinkedList<ITaskTreeNode>();
[557]204        }
205
206        children.add(index, child);
[439]207    }
208
[557]209    /**
210     * TODO: comment
211     *
212     * @param i
213     * @return
214     */
215    synchronized ITaskTreeNode removeChild(int index) {
216        return children.remove(index);
[439]217    }
218
[557]219    /*
220     * (non-Javadoc)
221     *
222     * @see java.lang.Object#clone()
223     */
224    @Override
[1074]225    public synchronized ITaskTreeNode clone() {
[557]226        TaskTreeNode clone = null;
227        try {
228            clone = (TaskTreeNode) super.clone();
229
230            if (children != null) {
[1126]231                clone.children = new LinkedList<ITaskTreeNode>();
[557]232
233                for (ITaskTreeNode child : children) {
234                    clone.children.add(child.clone());
235                }
236            }
237
[470]238        }
[557]239        catch (CloneNotSupportedException e) {
240            // this should never happen. Therefore simply dump the exception
241            e.printStackTrace();
242        }
243
244        return clone;
[467]245    }
[439]246
247}
Note: See TracBrowser for help on using the repository browser.