[1146] | 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 |
|
---|
[1423] | 17 | import java.util.ArrayList; |
---|
| 18 | |
---|
[1146] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
| 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo;
|
---|
[1423] | 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskMetric; |
---|
[1146] | 22 |
|
---|
| 23 | /**
|
---|
[1216] | 24 | * <p> |
---|
| 25 | * this is the default implementation of the interface {@link ITaskInfo}. It |
---|
| 26 | * does not do anything fancy except implementing the interface. |
---|
| 27 | * </p> |
---|
| 28 | * |
---|
| 29 | * @author Patrick Harms |
---|
[1146] | 30 | */
|
---|
| 31 | public class TaskInfo implements ITaskInfo {
|
---|
| 32 |
|
---|
[1216] | 33 | /** |
---|
| 34 | * <p> |
---|
| 35 | * the task to which the infos belong |
---|
| 36 | * </p> |
---|
| 37 | */
|
---|
[1423] | 38 | private ITask task; |
---|
| 39 | |
---|
[1216] | 40 | /** |
---|
| 41 | * <p> |
---|
[1423] | 42 | * all available measures for the task |
---|
[1287] | 43 | * </p> |
---|
| 44 | */ |
---|
[1423] | 45 | private ArrayList<Measure> measures = new ArrayList<Measure>();
|
---|
| 46 |
|
---|
[1287] | 47 | /** |
---|
| 48 | * <p> |
---|
[1216] | 49 | * initialized the task infos with the task to which they belong. |
---|
| 50 | * </p> |
---|
| 51 | *
|
---|
[1146] | 52 | * @param task
|
---|
| 53 | */
|
---|
| 54 | TaskInfo(ITask task) {
|
---|
| 55 | this.task = task;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[1216] | 58 | /* (non-Javadoc)
|
---|
| 59 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getTask()
|
---|
[1146] | 60 | */
|
---|
| 61 | @Override
|
---|
| 62 | public ITask getTask() {
|
---|
| 63 | return task;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[1287] | 66 | /* (non-Javadoc) |
---|
[1423] | 67 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasures() |
---|
[1287] | 68 | */ |
---|
| 69 | @Override |
---|
[1423] | 70 | public IMeasure[] getMeasures() { |
---|
| 71 | measures.trimToSize(); |
---|
| 72 | return measures.toArray(new IMeasure[measures.size()]); |
---|
[1287] | 73 | } |
---|
| 74 | |
---|
[1423] | 75 | /* (non-Javadoc) |
---|
| 76 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasureValue(java.lang.String) |
---|
| 77 | */ |
---|
| 78 | @Override |
---|
| 79 | public int getMeasureValue(TaskMetric metric) { |
---|
| 80 | Measure measure = getMeasure(metric); |
---|
| 81 | |
---|
| 82 | if (measure == null) { |
---|
| 83 | throw new IllegalArgumentException("unknown metric " + metric); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | return measure.getValue(); |
---|
| 87 | } |
---|
| 88 | |
---|
[1216] | 89 | /* (non-Javadoc)
|
---|
[1146] | 90 | * @see java.lang.Object#toString()
|
---|
| 91 | */
|
---|
| 92 | @Override
|
---|
| 93 | public synchronized String toString() {
|
---|
[1287] | 94 | return "TaskInfo(" + task + ")";
|
---|
[1146] | 95 | }
|
---|
[1287] | 96 | |
---|
| 97 | /** |
---|
| 98 | * <p> |
---|
[1891] | 99 | * must be called to indicate that new measures for the provided metric are about to |
---|
[1423] | 100 | * be calculated and added. |
---|
[1287] | 101 | * </p> |
---|
[1423] | 102 | * |
---|
| 103 | * @param metric the metric for which measures are about to be provided |
---|
[1287] | 104 | */ |
---|
[1423] | 105 | void addMeasure(TaskMetric metric) { |
---|
| 106 | Measure measure = getMeasure(metric); |
---|
| 107 | |
---|
| 108 | if (measure != null) { |
---|
| 109 | throw new IllegalArgumentException("measure for metric " + metric + " already exists."); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | measure = new Measure(metric); |
---|
| 113 | measures.add(measure); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * <p> |
---|
[1891] | 118 | * sets a specific value for a measure of a specific metric |
---|
[1423] | 119 | * </p> |
---|
| 120 | * |
---|
| 121 | * @param metric the metric to which the value belongs |
---|
| 122 | * @param value the value of the measure |
---|
| 123 | */ |
---|
[1891] | 124 | void setCount(TaskMetric metric, int value) { |
---|
[1423] | 125 | Measure measure = getMeasure(metric); |
---|
| 126 | |
---|
| 127 | if (measure == null) { |
---|
| 128 | throw new IllegalArgumentException("unknown metric. Please create a measure " + |
---|
| 129 | "for the metric before using it."); |
---|
| 130 | } |
---|
| 131 | |
---|
[1891] | 132 | measure.set(value); |
---|
[1423] | 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * <p> |
---|
[1891] | 137 | * increases a specific value for a measure of a specific metric |
---|
[1423] | 138 | * </p> |
---|
| 139 | * |
---|
| 140 | * @param metric the metric to which the value belongs |
---|
| 141 | * @param increment the increment to be added to the value of the measure |
---|
| 142 | */ |
---|
[1891] | 143 | void increaseCount(TaskMetric metric, int increment) { |
---|
[1423] | 144 | Measure measure = getMeasure(metric); |
---|
| 145 | |
---|
| 146 | if (measure == null) { |
---|
| 147 | throw new IllegalArgumentException("unknown metric. Please create a measure " + |
---|
| 148 | "for the metric before using it."); |
---|
| 149 | } |
---|
| 150 | |
---|
[1891] | 151 | measure.increase(increment); |
---|
[1423] | 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * <p> |
---|
| 156 | * convenience method to internally determine the measure for a specific metric |
---|
| 157 | * </p> |
---|
| 158 | */ |
---|
| 159 | private Measure getMeasure(TaskMetric metric) { |
---|
| 160 | for (Measure candidate : measures) { |
---|
| 161 | if (candidate.getMetric().equals(metric)) { |
---|
| 162 | return candidate; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | return null; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * <p> |
---|
| 171 | * implementation for the measure interface of the task info interface. Does nothing fancy |
---|
| 172 | * except implementing the interface |
---|
| 173 | * </p> |
---|
| 174 | * |
---|
| 175 | * @author Patrick Harms |
---|
| 176 | */ |
---|
| 177 | private static class Measure implements IMeasure { |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * <p> |
---|
| 181 | * the metric to which the measure belongs |
---|
| 182 | * </p> |
---|
| 183 | */ |
---|
| 184 | private TaskMetric metric; |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * <p> |
---|
[1891] | 188 | * the value of the measure independent |
---|
[1423] | 189 | * </p> |
---|
| 190 | */ |
---|
[1891] | 191 | private int value = 0; |
---|
[1423] | 192 | |
---|
| 193 | /** |
---|
| 194 | * <p> |
---|
| 195 | * initializes the measure with a specific metric |
---|
| 196 | * </p> |
---|
| 197 | */ |
---|
| 198 | private Measure(TaskMetric metric) { |
---|
| 199 | super(); |
---|
| 200 | this.metric = metric; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /* (non-Javadoc) |
---|
| 204 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getMetric() |
---|
| 205 | */ |
---|
| 206 | @Override |
---|
| 207 | public TaskMetric getMetric() { |
---|
| 208 | return metric; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /* (non-Javadoc) |
---|
| 212 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getValue() |
---|
| 213 | */ |
---|
| 214 | @Override |
---|
| 215 | public int getValue() { |
---|
[1891] | 216 | return value; |
---|
[1423] | 217 | } |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * <p> |
---|
[1891] | 221 | * sets the value of the measure |
---|
[1423] | 222 | * </p> |
---|
| 223 | */ |
---|
[1891] | 224 | private void set(int newValue) { |
---|
| 225 | value = newValue; |
---|
[1423] | 226 | } |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * <p> |
---|
[1891] | 230 | * increases the value of the measure |
---|
[1423] | 231 | * </p> |
---|
| 232 | */ |
---|
[1891] | 233 | private void increase(int increment) { |
---|
| 234 | value += increment; |
---|
[1423] | 235 | } |
---|
| 236 | |
---|
| 237 | } |
---|
| 238 | |
---|
[1146] | 239 | }
|
---|