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

Last change on this file since 1216 was 1216, checked in by pharms, 11 years ago
  • improved java doc
File size: 7.5 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 * <p>
27 * this is the default implementation of the interface {@link ITaskInstance}. It
28 * does not do anything fancy except implementing the interface.
29 * </p>
30 *
31 * @author Patrick Harms
32 */
33class TaskInstance implements ITaskInstance {
34   
35    /**
36     * <p>
37     * default serial version UID
38     * </p>
39     */
40    private static final long serialVersionUID = 1L;
41
42    /**
43     * <p>
44     * used as a counter to generate new ids for each newly created task instance. May overflow.
45     * </p>
46     */
47    private static int temporalId = 0;
48
49    /**
50     * <p>
51     * the task instantiated by this task instance
52     * </p>
53     */
54    private ITask task;
55   
56    /**
57     * <p>
58     * the id of the task instance (unique throughout the system as long as {@link #temporalId}
59     * does not overflow.
60     * </p>
61     */
62    private int id;
63
64    /**
65     * <p>
66     * the children of this task instance which are task instances, as well
67     * </p>
68     */
69    private List<ITaskInstance> children;
70
71    /**
72     * <p>
73     * instantiated the task instance with the task that is instantiated by the instance. It also
74     * assigns a unique id to the instance using {@link #getNewId()}.
75     * </p>
76     */
77    TaskInstance(ITask task) {
78        this.task = task;
79        id = getNewId();
80    }
81
82    /**
83     * <p>
84     * creates a new id for a task instance using {@link #temporalId} by incrementing it an
85     * returning its current value. Resets the counter if {@link Integer.MAX_VALUE} is reached.
86     * </p>
87     *
88     * @return a new unique id for a task instance as long as {@link #temporalId} does not overflow
89     */
90    private static synchronized int getNewId() {
91        if (temporalId == Integer.MAX_VALUE) {
92            temporalId = 0;
93        }
94
95        return temporalId++;
96    }
97
98    /* (non-Javadoc)
99     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getTask()
100     */
101    @Override
102    public ITask getTask() {
103        return task;
104    }
105
106    /* (non-Javadoc)
107     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getChildren()
108     */
109    public synchronized List<ITaskInstance> getChildren() {
110        if (children == null) {
111            children = new LinkedList<ITaskInstance>();
112        }
113
114        return Collections.unmodifiableList(children);
115    }
116
117    /* (non-Javadoc)
118     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#get(int)
119     */
120    @Override
121    public ITaskInstance get(int index) {
122        if (children == null) {
123            throw new IndexOutOfBoundsException(Integer.toString(index));
124        }
125        else {
126            return children.get(index);
127        }
128    }
129
130    /* (non-Javadoc)
131     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#size()
132     */
133    @Override
134    public int size() {
135        if (children == null) {
136            return 0;
137        }
138        else {
139            return children.size();
140        }
141    }
142
143    /* (non-Javadoc)
144     * @see java.lang.Iterable#iterator()
145     */
146    @Override
147    public Iterator<ITaskInstance> iterator() {
148        return getChildren().iterator();
149    }
150
151    /*
152     * (non-Javadoc)
153     *
154     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode)
155     */
156    @Override
157    public boolean equals(ITaskInstance taskInstance) {
158        // task instances are only equal if they are identical or if they have the same id
159        // (may happen, if they are cloned)
160        return (this == taskInstance) || (this.hashCode() == taskInstance.hashCode());
161    }
162
163    /* (non-Javadoc)
164     * @see java.lang.Object#hashCode()
165     */
166    @Override
167    public synchronized int hashCode() {
168        return id;
169    }
170
171    /* (non-Javadoc)
172     * @see java.lang.Object#toString()
173     */
174    @Override
175    public synchronized String toString() {
176        StringBuffer result = new StringBuffer();
177        result.append("task ");
178        result.append(task.getId());
179        result.append(" (instance ");
180        result.append(id);
181       
182        if (task.getDescription() != null) {
183            result.append(", ");
184            result.append(task.getDescription());
185        }
186       
187        if (children != null) {
188            result.append(", ");
189            result.append(children.size());
190            result.append(" children");
191        }
192       
193        result.append(')');
194        return result.toString();
195    }
196
197    /* (non-Javadoc)
198     * @see java.lang.Object#clone()
199     */
200    @Override
201    public synchronized ITaskInstance clone() {
202        TaskInstance clone = null;
203        try {
204            clone = (TaskInstance) super.clone();
205
206            if (children != null) {
207                clone.children = new LinkedList<ITaskInstance>();
208
209                for (ITaskInstance child : children) {
210                    clone.children.add(child.clone());
211                }
212            }
213
214        }
215        catch (CloneNotSupportedException e) {
216            // this should never happen. Therefore simply dump the exception
217            e.printStackTrace();
218        }
219
220        return clone;
221    }
222
223    /**
224     * <p>
225     * used to add a child to this task instance
226     * </p>
227     *
228     * @param child the new child of this instance
229     */
230    synchronized void addChild(ITaskInstance child) {
231        if (children == null) {
232            children = new LinkedList<ITaskInstance>();
233        }
234
235        children.add(child);
236    }
237
238    /**
239     * <p>
240     * used to add a child to this task instance at a specific position
241     * </p>
242     *
243     * @param index the position of the new child in the list of children
244     * @param child the new child of this instance
245     */
246    synchronized void addChild(int index, ITaskInstance child) {
247        if (children == null) {
248            children = new LinkedList<ITaskInstance>();
249        }
250
251        children.add(index, child);
252    }
253
254    /**
255     * <p>
256     * removes a child from this task instance at a specific position
257     * </p>
258     *
259     * @param index the position of the child to be removed
260     *
261     * @return the child remove from the children of this instance
262     */
263    synchronized ITaskInstance removeChild(int index) {
264        if (children != null) {
265            return children.remove(index);
266        }
267        else {
268            throw new IllegalArgumentException
269                ("this task instance does not have children that can be removed");
270        }
271    }
272
273    /**
274     * <p>
275     * used to update the task represented through this instance
276     * </p>
277     *
278     * @param task the task to set
279     */
280    void setTask(ITask task) {
281        this.task = task;
282    }
283
284}
Note: See TracBrowser for help on using the repository browser.