source: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/SequenceInstance.java @ 1588

Last change on this file since 1588 was 1414, checked in by pharms, 10 years ago
File size: 4.8 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.IIterationInstance;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
26
27/**
28 * <p>
29 * Default implementation of {@link IIterationInstance}.
30 * </p>
31 *
32 * @author Patrick Harms
33 */
34class SequenceInstance extends TaskInstance implements ISequenceInstance {
35
36    /**
37     * <p>
38     * default serial version UID
39     * </p>
40     */
41    private static final long serialVersionUID = 1L;
42
43    /**
44     * <p>
45     * the children of this task instance which are task instances, as well
46     * </p>
47     */
48    private List<ITaskInstance> children;
49
50    /**
51     * <p>
52     * initializes this instance with the respective task model
53     * </p>
54     *
55     * @param task  the task of which this is an instance
56     */
57    SequenceInstance(ISequence task) {
58        super(task);
59    }
60
61    /* (non-Javadoc)
62     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getChildren()
63     */
64    public synchronized List<ITaskInstance> getChildren() {
65        if (children == null) {
66            children = new LinkedList<ITaskInstance>();
67        }
68
69        return Collections.unmodifiableList(children);
70    }
71
72    /* (non-Javadoc)
73     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#get(int)
74     */
75    @Override
76    public ITaskInstance get(int index) {
77        if (children == null) {
78            throw new IndexOutOfBoundsException(Integer.toString(index));
79        }
80        else {
81            return children.get(index);
82        }
83    }
84
85    /* (non-Javadoc)
86     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#size()
87     */
88    @Override
89    public int size() {
90        if (children == null) {
91            return 0;
92        }
93        else {
94            return children.size();
95        }
96    }
97
98    /* (non-Javadoc)
99     * @see java.lang.Iterable#iterator()
100     */
101    @Override
102    public Iterator<ITaskInstance> iterator() {
103        return getChildren().iterator();
104    }
105
106    /* (non-Javadoc)
107     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance#getSequence()
108     */
109    @Override
110    public ISequence getSequence() {
111        return (ISequence) super.getTask();
112    }
113
114    /* (non-Javadoc)
115     * @see de.ugoe.cs.autoquest.tasktrees.treeimpl.ISequenceInstance#clone()
116     */
117    @Override
118    public synchronized ISequenceInstance clone() {
119        SequenceInstance clone = (SequenceInstance) super.clone();
120
121        if (children != null) {
122            clone.children = new LinkedList<ITaskInstance>();
123
124            for (ITaskInstance child : children) {
125                clone.children.add(child.clone());
126            }
127        }
128
129        return clone;
130    }
131
132    /**
133     * <p>
134     * used to add a child to this task instance
135     * </p>
136     *
137     * @param child the new child of this instance
138     */
139    synchronized void addChild(ITaskInstance child) {
140        if (children == null) {
141            children = new LinkedList<ITaskInstance>();
142        }
143
144        children.add(child);
145    }
146
147    /**
148     * <p>
149     * used to add a child to this task instance at a specific position
150     * </p>
151     *
152     * @param index the position of the new child in the list of children
153     * @param child the new child of this instance
154     */
155    synchronized void addChild(int index, ITaskInstance child) {
156        if (children == null) {
157            children = new LinkedList<ITaskInstance>();
158        }
159
160        children.add(index, child);
161    }
162
163    /**
164     * <p>
165     * removes a child from this task instance at a specific position
166     * </p>
167     *
168     * @param index the position of the child to be removed
169     *
170     * @return the child removed from the children of this instance
171     */
172    synchronized ITaskInstance removeChild(int index) {
173        if (children != null) {
174            return children.remove(index);
175        }
176        else {
177            throw new IllegalArgumentException
178                ("this task instance does not have children that can be removed");
179        }
180    }
181
182}
Note: See TracBrowser for help on using the repository browser.