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