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 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.treeimpl; |
---|
16 | |
---|
17 | import java.util.Collections; |
---|
18 | import java.util.Iterator; |
---|
19 | import java.util.LinkedList; |
---|
20 | import java.util.List; |
---|
21 | |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
25 | import 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 | */ |
---|
34 | class 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 |
---|
56 | * the task of which this is an instance |
---|
57 | */ |
---|
58 | SequenceInstance(ISequence task) { |
---|
59 | super(task); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * <p> |
---|
64 | * used to add a child to this task instance at a specific position |
---|
65 | * </p> |
---|
66 | * |
---|
67 | * @param index |
---|
68 | * the position of the new child in the list of children |
---|
69 | * @param child |
---|
70 | * the new child of this instance |
---|
71 | */ |
---|
72 | synchronized void addChild(int index, ITaskInstance child) { |
---|
73 | if (children == null) { |
---|
74 | children = new LinkedList<ITaskInstance>(); |
---|
75 | } |
---|
76 | |
---|
77 | children.add(index, child); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * <p> |
---|
82 | * used to add a child to this task instance |
---|
83 | * </p> |
---|
84 | * |
---|
85 | * @param child |
---|
86 | * the new child of this instance |
---|
87 | */ |
---|
88 | synchronized void addChild(ITaskInstance child) { |
---|
89 | if (children == null) { |
---|
90 | children = new LinkedList<ITaskInstance>(); |
---|
91 | } |
---|
92 | |
---|
93 | children.add(child); |
---|
94 | } |
---|
95 | |
---|
96 | /* |
---|
97 | * (non-Javadoc) |
---|
98 | * |
---|
99 | * @see de.ugoe.cs.autoquest.tasktrees.treeimpl.ISequenceInstance#clone() |
---|
100 | */ |
---|
101 | @Override |
---|
102 | public synchronized ISequenceInstance clone() { |
---|
103 | final SequenceInstance clone = (SequenceInstance) super.clone(); |
---|
104 | |
---|
105 | if (children != null) { |
---|
106 | clone.children = new LinkedList<ITaskInstance>(); |
---|
107 | |
---|
108 | for (final ITaskInstance child : children) { |
---|
109 | clone.children.add(child.clone()); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | return clone; |
---|
114 | } |
---|
115 | |
---|
116 | /* |
---|
117 | * (non-Javadoc) |
---|
118 | * |
---|
119 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#get(int) |
---|
120 | */ |
---|
121 | @Override |
---|
122 | public ITaskInstance get(int index) { |
---|
123 | if (children == null) { |
---|
124 | throw new IndexOutOfBoundsException(Integer.toString(index)); |
---|
125 | } else { |
---|
126 | return children.get(index); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | /* |
---|
131 | * (non-Javadoc) |
---|
132 | * |
---|
133 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getChildren() |
---|
134 | */ |
---|
135 | public synchronized List<ITaskInstance> getChildren() { |
---|
136 | if (children == null) { |
---|
137 | children = new LinkedList<ITaskInstance>(); |
---|
138 | } |
---|
139 | |
---|
140 | return Collections.unmodifiableList(children); |
---|
141 | } |
---|
142 | |
---|
143 | /* |
---|
144 | * (non-Javadoc) |
---|
145 | * |
---|
146 | * @see |
---|
147 | * de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance#getSequence() |
---|
148 | */ |
---|
149 | @Override |
---|
150 | public ISequence getSequence() { |
---|
151 | return (ISequence) super.getTask(); |
---|
152 | } |
---|
153 | |
---|
154 | /* |
---|
155 | * (non-Javadoc) |
---|
156 | * |
---|
157 | * @see java.lang.Iterable#iterator() |
---|
158 | */ |
---|
159 | @Override |
---|
160 | public Iterator<ITaskInstance> iterator() { |
---|
161 | return getChildren().iterator(); |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * <p> |
---|
166 | * removes a child from this task instance at a specific position |
---|
167 | * </p> |
---|
168 | * |
---|
169 | * @param index |
---|
170 | * the position of the child to be removed |
---|
171 | * |
---|
172 | * @return the child removed from the children of this instance |
---|
173 | */ |
---|
174 | synchronized ITaskInstance removeChild(int index) { |
---|
175 | if (children != null) { |
---|
176 | return children.remove(index); |
---|
177 | } else { |
---|
178 | throw new IllegalArgumentException( |
---|
179 | "this task instance does not have children that can be removed"); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | /* |
---|
184 | * (non-Javadoc) |
---|
185 | * |
---|
186 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList#size() |
---|
187 | */ |
---|
188 | @Override |
---|
189 | public int size() { |
---|
190 | if (children == null) { |
---|
191 | return 0; |
---|
192 | } else { |
---|
193 | return children.size(); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | } |
---|