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

Last change on this file since 1146 was 1146, checked in by pharms, 11 years ago
  • complete refactoring of task tree model with a separation of task models and task instances
  • appropriate adaptation of task tree generation process
  • appropriate adaptation of commands and task tree visualization
File size: 5.9 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.treeimpl;
16
17import java.util.Collections;
18import java.util.Iterator;
19import java.util.LinkedList;
20import java.util.List;
21
22import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
24
25/**
26 * TODO comment
27 *
28 * @version $Revision: $ $Date: $
29 * @author 2011, last modified by $Author: $
30 */
31class TaskInstance implements ITaskInstance {
32   
33    /**  */
34    private static final long serialVersionUID = 1L;
35
36    /** */
37    private static int temporalId = 0;
38
39    /** */
40    private ITask task;
41   
42    /** */
43    private int id;
44
45    /** children */
46    private List<ITaskInstance> children;
47
48    /**
49     *
50     */
51    TaskInstance(ITask task) {
52        this.task = task;
53        id = getNewId();
54    }
55
56    /**
57     * TODO: comment
58     *
59     * @return
60     */
61    private static synchronized int getNewId() {
62        if (temporalId == Integer.MAX_VALUE) {
63            temporalId = 0;
64        }
65
66        return temporalId++;
67    }
68
69    /* (non-Javadoc)
70     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getTask()
71     */
72    @Override
73    public ITask getTask() {
74        return task;
75    }
76
77    /* (non-Javadoc)
78     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getChildren()
79     */
80    public synchronized List<ITaskInstance> getChildren() {
81        if (children == null) {
82            children = new LinkedList<ITaskInstance>();
83        }
84
85        return Collections.unmodifiableList(children);
86    }
87
88    /* (non-Javadoc)
89     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#get(int)
90     */
91    @Override
92    public ITaskInstance get(int index) {
93        if (children == null) {
94            throw new IndexOutOfBoundsException(Integer.toString(index));
95        }
96        else {
97            return children.get(index);
98        }
99    }
100
101    /* (non-Javadoc)
102     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#size()
103     */
104    @Override
105    public int size() {
106        if (children == null) {
107            return 0;
108        }
109        else {
110            return children.size();
111        }
112    }
113
114    /* (non-Javadoc)
115     * @see java.lang.Iterable#iterator()
116     */
117    @Override
118    public Iterator<ITaskInstance> iterator() {
119        return getChildren().iterator();
120    }
121
122    /*
123     * (non-Javadoc)
124     *
125     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode)
126     */
127    @Override
128    public boolean equals(ITaskInstance taskInstance) {
129        // task instances are only equal if they are identical or if they have the same id
130        // (may happen, if they are cloned)
131        return (this == taskInstance) || (this.hashCode() == taskInstance.hashCode());
132    }
133
134    /*
135     * (non-Javadoc)
136     *
137     * @see java.lang.Object#hashCode()
138     */
139    @Override
140    public synchronized int hashCode() {
141        return id;
142    }
143
144    /*
145     * (non-Javadoc)
146     *
147     * @see java.lang.Object#toString()
148     */
149    @Override
150    public synchronized String toString() {
151        StringBuffer result = new StringBuffer();
152        result.append("task ");
153        result.append(task.getId());
154        result.append(" (instance ");
155        result.append(id);
156       
157        if (task.getDescription() != null) {
158            result.append(", ");
159            result.append(task.getDescription());
160        }
161       
162        if (children != null) {
163            result.append(", ");
164            result.append(children.size());
165            result.append(" children");
166        }
167       
168        result.append(')');
169        return result.toString();
170    }
171
172    /*
173     * (non-Javadoc)
174     *
175     * @see java.lang.Object#clone()
176     */
177    @Override
178    public synchronized ITaskInstance clone() {
179        TaskInstance clone = null;
180        try {
181            clone = (TaskInstance) super.clone();
182
183            if (children != null) {
184                clone.children = new LinkedList<ITaskInstance>();
185
186                for (ITaskInstance child : children) {
187                    clone.children.add(child.clone());
188                }
189            }
190
191        }
192        catch (CloneNotSupportedException e) {
193            // this should never happen. Therefore simply dump the exception
194            e.printStackTrace();
195        }
196
197        return clone;
198    }
199
200    /**
201     *
202     */
203    synchronized void addChild(ITaskInstance child) {
204        if (children == null) {
205            children = new LinkedList<ITaskInstance>();
206        }
207
208        children.add(child);
209    }
210
211    /**
212     *
213     */
214    synchronized void addChild(int index, ITaskInstance child) {
215        if (children == null) {
216            children = new LinkedList<ITaskInstance>();
217        }
218
219        children.add(index, child);
220    }
221
222    /**
223     * TODO: comment
224     *
225     * @param i
226     * @return
227     */
228    synchronized ITaskInstance removeChild(int index) {
229        if (children != null) {
230            return children.remove(index);
231        }
232        else {
233            throw new IllegalArgumentException
234                ("this task instance does not have children that can be removed");
235        }
236    }
237
238    /**
239     * @param task the task to set
240     */
241    void setTask(ITask task) {
242        this.task = task;
243    }
244
245}
Note: See TracBrowser for help on using the repository browser.