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.ArrayList; |
---|
18 | |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo;
|
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskMetric; |
---|
22 |
|
---|
23 | /**
|
---|
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 |
---|
30 | */
|
---|
31 | public class TaskInfo implements ITaskInfo {
|
---|
32 |
|
---|
33 | /** |
---|
34 | * <p> |
---|
35 | * the task to which the infos belong |
---|
36 | * </p> |
---|
37 | */
|
---|
38 | private ITask task; |
---|
39 | |
---|
40 | /** |
---|
41 | * <p> |
---|
42 | * all available measures for the task |
---|
43 | * </p> |
---|
44 | */ |
---|
45 | private ArrayList<Measure> measures = new ArrayList<Measure>();
|
---|
46 |
|
---|
47 | /** |
---|
48 | * <p> |
---|
49 | * initialized the task infos with the task to which they belong. |
---|
50 | * </p> |
---|
51 | *
|
---|
52 | * @param task
|
---|
53 | */
|
---|
54 | TaskInfo(ITask task) {
|
---|
55 | this.task = task;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /* (non-Javadoc)
|
---|
59 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getTask()
|
---|
60 | */
|
---|
61 | @Override
|
---|
62 | public ITask getTask() {
|
---|
63 | return task;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* (non-Javadoc) |
---|
67 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasures() |
---|
68 | */ |
---|
69 | @Override |
---|
70 | public IMeasure[] getMeasures() { |
---|
71 | measures.trimToSize(); |
---|
72 | return measures.toArray(new IMeasure[measures.size()]); |
---|
73 | } |
---|
74 | |
---|
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 | |
---|
89 | /* (non-Javadoc)
|
---|
90 | * @see java.lang.Object#toString()
|
---|
91 | */
|
---|
92 | @Override
|
---|
93 | public synchronized String toString() {
|
---|
94 | return "TaskInfo(" + task + ")";
|
---|
95 | }
|
---|
96 | |
---|
97 | /** |
---|
98 | * <p> |
---|
99 | * must be called to indicate that new measures for the provided metric are about to |
---|
100 | * be calculated and added. |
---|
101 | * </p> |
---|
102 | * |
---|
103 | * @param metric the metric for which measures are about to be provided |
---|
104 | */ |
---|
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> |
---|
118 | * sets a specific value for a measure of a specific metric |
---|
119 | * </p> |
---|
120 | * |
---|
121 | * @param metric the metric to which the value belongs |
---|
122 | * @param value the value of the measure |
---|
123 | */ |
---|
124 | void setCount(TaskMetric metric, int value) { |
---|
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 | |
---|
132 | measure.set(value); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * <p> |
---|
137 | * increases a specific value for a measure of a specific metric |
---|
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 | */ |
---|
143 | void increaseCount(TaskMetric metric, int increment) { |
---|
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 | |
---|
151 | measure.increase(increment); |
---|
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> |
---|
188 | * the value of the measure independent |
---|
189 | * </p> |
---|
190 | */ |
---|
191 | private int value = 0; |
---|
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() { |
---|
216 | return value; |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * <p> |
---|
221 | * sets the value of the measure |
---|
222 | * </p> |
---|
223 | */ |
---|
224 | private void set(int newValue) { |
---|
225 | value = newValue; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * <p> |
---|
230 | * increases the value of the measure |
---|
231 | * </p> |
---|
232 | */ |
---|
233 | private void increase(int increment) { |
---|
234 | value += increment; |
---|
235 | } |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | }
|
---|