source: branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskInfo.java @ 1733

Last change on this file since 1733 was 1733, checked in by rkrimmel, 10 years ago

Used Eclipse code cleanup

File size: 7.4 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.treeimpl;
16
17import java.util.ArrayList;
18import java.util.HashMap;
19
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskMetric;
23
24/**
25 * <p>
26 * this is the default implementation of the interface {@link ITaskInfo}. It
27 * does not do anything fancy except implementing the interface.
28 * </p>
29 *
30 * @author Patrick Harms
31 */
32public class TaskInfo implements ITaskInfo {
33
34        /**
35         * <p>
36         * implementation for the measure interface of the task info interface. Does
37         * nothing fancy except implementing the interface
38         * </p>
39         *
40         * @author Patrick Harms
41         */
42        private static class Measure implements IMeasure {
43
44                /**
45                 * <p>
46                 * the metric to which the measure belongs
47                 * </p>
48                 */
49                private final TaskMetric metric;
50
51                /**
52                 * <p>
53                 * the observed values for the difference contexts of the task
54                 * </p>
55                 */
56                private HashMap<ITask, Integer> values;
57
58                /**
59                 * <p>
60                 * the context free value of the measure independent of the task context
61                 * </p>
62                 */
63                private int contextFreeValue = 0;
64
65                /**
66                 * <p>
67                 * initializes the measure with a specific metric
68                 * </p>
69                 */
70                private Measure(TaskMetric metric) {
71                        super();
72                        this.metric = metric;
73                }
74
75                /*
76                 * (non-Javadoc)
77                 *
78                 * @see
79                 * de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getMetric()
80                 */
81                @Override
82                public TaskMetric getMetric() {
83                        return metric;
84                }
85
86                /*
87                 * (non-Javadoc)
88                 *
89                 * @see
90                 * de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getValue()
91                 */
92                @Override
93                public int getValue() {
94                        return contextFreeValue;
95                }
96
97                /*
98                 * (non-Javadoc)
99                 *
100                 * @see
101                 * de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getValue
102                 * (de.ugoe.cs.autoquest.tasktrees.treeifc.ITask)
103                 */
104                @Override
105                public int getValue(ITask context) {
106                        if ((context != null) && (values != null)) {
107                                final Integer currentValue = values.get(context);
108
109                                if (currentValue != null) {
110                                        return currentValue;
111                                }
112                        }
113
114                        return Integer.MIN_VALUE;
115                }
116
117                /**
118                 * <p>
119                 * increases the value of the measure context free as well as specific
120                 * to the provided context according to the provided increment
121                 * </p>
122                 */
123                private void increase(ITask context, int increment) {
124                        contextFreeValue += increment;
125
126                        if (context != null) {
127                                if (values == null) {
128                                        values = new HashMap<ITask, Integer>();
129                                }
130
131                                Integer currentValue = values.get(context);
132
133                                if (currentValue == null) {
134                                        currentValue = 0;
135                                }
136
137                                values.put(context, currentValue + increment);
138                        }
139                }
140
141                /**
142                 * <p>
143                 * sets the value of the measure context free as well as specific to the
144                 * provided context
145                 * </p>
146                 */
147                private void set(ITask context, int value) {
148                        contextFreeValue = value;
149
150                        if (context != null) {
151                                if (values == null) {
152                                        values = new HashMap<ITask, Integer>();
153                                }
154
155                                values.put(context, value);
156                        }
157                }
158
159        }
160
161        /**
162         * <p>
163         * the task to which the infos belong
164         * </p>
165         */
166        private final ITask task;
167
168        /**
169         * <p>
170         * all available measures for the task
171         * </p>
172         */
173        private final ArrayList<Measure> measures = new ArrayList<Measure>();
174
175        /**
176         * <p>
177         * initialized the task infos with the task to which they belong.
178         * </p>
179         *
180         * @param task
181         */
182        TaskInfo(ITask task) {
183                this.task = task;
184        }
185
186        /**
187         * <p>
188         * must be called to indicate that a new new measures for the provided
189         * metric are about to be calculated and added.
190         * </p>
191         *
192         * @param metric
193         *            the metric for which measures are about to be provided
194         */
195        void addMeasure(TaskMetric metric) {
196                Measure measure = getMeasure(metric);
197
198                if (measure != null) {
199                        throw new IllegalArgumentException("measure for metric " + metric
200                                        + " already exists.");
201                }
202
203                measure = new Measure(metric);
204                measures.add(measure);
205        }
206
207        /**
208         * <p>
209         * convenience method to internally determine the measure for a specific
210         * metric
211         * </p>
212         */
213        private Measure getMeasure(TaskMetric metric) {
214                for (final Measure candidate : measures) {
215                        if (candidate.getMetric().equals(metric)) {
216                                return candidate;
217                        }
218                }
219
220                return null;
221        }
222
223        /*
224         * (non-Javadoc)
225         *
226         * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasures()
227         */
228        @Override
229        public IMeasure[] getMeasures() {
230                measures.trimToSize();
231                return measures.toArray(new IMeasure[measures.size()]);
232        }
233
234        /*
235         * (non-Javadoc)
236         *
237         * @see
238         * de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasureValue(java
239         * .lang.String)
240         */
241        @Override
242        public int getMeasureValue(TaskMetric metric) {
243                final Measure measure = getMeasure(metric);
244
245                if (measure == null) {
246                        throw new IllegalArgumentException("unknown metric " + metric);
247                }
248
249                return measure.getValue();
250        }
251
252        /*
253         * (non-Javadoc)
254         *
255         * @see
256         * de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasureValue(java
257         * .lang.String, de.ugoe.cs.autoquest.tasktrees.treeifc.ITask)
258         */
259        @Override
260        public int getMeasureValue(TaskMetric metric, ITask context) {
261                final Measure measure = getMeasure(metric);
262
263                if (measure == null) {
264                        throw new IllegalArgumentException("unknown metric " + metric);
265                }
266
267                return measure.getValue(context);
268        }
269
270        /*
271         * (non-Javadoc)
272         *
273         * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getTask()
274         */
275        @Override
276        public ITask getTask() {
277                return task;
278        }
279
280        /**
281         * <p>
282         * increases a specific value for a measure of a specific metric in the
283         * provided context of the task
284         * </p>
285         *
286         * @param metric
287         *            the metric to which the value belongs
288         * @param context
289         *            the context of the task in which the measure was recorded
290         * @param increment
291         *            the increment to be added to the value of the measure
292         */
293        void increaseCount(TaskMetric metric, ITask context, int increment) {
294                final Measure measure = getMeasure(metric);
295
296                if (measure == null) {
297                        throw new IllegalArgumentException(
298                                        "unknown metric. Please create a measure "
299                                                        + "for the metric before using it.");
300                }
301
302                measure.increase(context, increment);
303        }
304
305        /**
306         * <p>
307         * sets a specific value for a measure of a specific metric in the provided
308         * context of the task
309         * </p>
310         *
311         * @param metric
312         *            the metric to which the value belongs
313         * @param context
314         *            the context of the task in which the measure was recorded
315         * @param value
316         *            the value of the measure
317         */
318        void setCount(TaskMetric metric, ITask context, int value) {
319                final Measure measure = getMeasure(metric);
320
321                if (measure == null) {
322                        throw new IllegalArgumentException(
323                                        "unknown metric. Please create a measure "
324                                                        + "for the metric before using it.");
325                }
326
327                measure.set(context, value);
328        }
329
330        /*
331         * (non-Javadoc)
332         *
333         * @see java.lang.Object#toString()
334         */
335        @Override
336        public synchronized String toString() {
337                return "TaskInfo(" + task + ")";
338        }
339
340}
Note: See TracBrowser for help on using the repository browser.