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