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