1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| package com.jeantessier.metrics; |
34 |
| |
35 |
| import java.io.*; |
36 |
| import java.util.*; |
37 |
| |
38 |
| import org.apache.log4j.*; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| public class SumMeasurement extends MeasurementBase { |
55 |
| private List<String> terms = new LinkedList<String>(); |
56 |
| |
57 |
| private double value = 0.0; |
58 |
| |
59 |
696
| public SumMeasurement(MeasurementDescriptor descriptor, Metrics context, String initText) {
|
60 |
696
| super(descriptor, context, initText);
|
61 |
| |
62 |
696
| try {
|
63 |
696
| BufferedReader in = new BufferedReader(new StringReader(initText));
|
64 |
692
| String line;
|
65 |
| |
66 |
?
| while ((line = in.readLine()) != null) {
|
67 |
1365
| terms.add(line.trim());
|
68 |
| } |
69 |
| |
70 |
692
| in.close();
|
71 |
| } catch (Exception ex) { |
72 |
4
| Logger.getLogger(getClass()).debug("Cannot initialize with \"" + initText + "\"", ex);
|
73 |
4
| terms.clear();
|
74 |
| } |
75 |
| } |
76 |
| |
77 |
64
| public List<String> getTerms() {
|
78 |
64
| return terms;
|
79 |
| } |
80 |
| |
81 |
12
| public boolean isEmpty() {
|
82 |
12
| compute();
|
83 |
| |
84 |
12
| return super.isEmpty();
|
85 |
| } |
86 |
| |
87 |
2
| public void accept(MeasurementVisitor visitor) {
|
88 |
2
| visitor.visitSumMeasurement(this);
|
89 |
| } |
90 |
| |
91 |
80
| protected double compute() {
|
92 |
80
| if (!isCached()) {
|
93 |
64
| synchronized (this) {
|
94 |
64
| if (!isCached()) {
|
95 |
64
| value = 0.0;
|
96 |
64
| setEmpty(true);
|
97 |
| |
98 |
64
| if (getContext() != null) {
|
99 |
54
| Logger.getLogger(getClass()).debug("Start computing \"" + getShortName() + "\" on \"" + getContext().getName() + "\": value=" + value);
|
100 |
| } else { |
101 |
10
| Logger.getLogger(getClass()).debug("Start computing \"" + getShortName() + "\" on null: value=" + value);
|
102 |
| } |
103 |
| |
104 |
64
| for (String term : getTerms()) {
|
105 |
97
| Logger.getLogger(getClass()).debug("Evaluating term \"" + term + "\"");
|
106 |
| |
107 |
97
| double termValue = Double.NaN;
|
108 |
| |
109 |
97
| try {
|
110 |
97
| termValue = Double.parseDouble(term);
|
111 |
| } catch (NumberFormatException ex) { |
112 |
85
| if (term.startsWith("-")) {
|
113 |
2
| termValue = -1 * evaluateMeasurement(term.substring(1));
|
114 |
| } else { |
115 |
83
| termValue = evaluateMeasurement(term);
|
116 |
| } |
117 |
| } |
118 |
| |
119 |
97
| Logger.getLogger(getClass()).debug("term \"" + term + "\" is " + termValue);
|
120 |
| |
121 |
97
| value += termValue;
|
122 |
| |
123 |
97
| Logger.getLogger(getClass()).debug("value=" + value);
|
124 |
| } |
125 |
| |
126 |
64
| if (getContext() != null) {
|
127 |
54
| Logger.getLogger(getClass()).debug("Stop computing \"" + getShortName() + "\" on \"" + getContext().getName() + "\": value=" + value);
|
128 |
| } else { |
129 |
10
| Logger.getLogger(getClass()).debug("Stop computing \"" + getShortName() + "\" on null: value=" + value);
|
130 |
| } |
131 |
| |
132 |
64
| setCached(true);
|
133 |
| } |
134 |
| } |
135 |
| } |
136 |
| |
137 |
80
| if (getContext() != null) {
|
138 |
70
| Logger.getLogger(getClass()).debug("\"" + getShortName() + "\" on \"" + getContext().getName() + "\": value=" + value);
|
139 |
| } else { |
140 |
10
| Logger.getLogger(getClass()).debug("\"" + getShortName() + "\" on null: value=" + value);
|
141 |
| } |
142 |
| |
143 |
80
| return value;
|
144 |
| } |
145 |
| |
146 |
85
| private double evaluateMeasurement(String name) {
|
147 |
85
| double result = 0;
|
148 |
| |
149 |
85
| if (name.length() != 0) {
|
150 |
82
| int dispose;
|
151 |
| |
152 |
82
| synchronized (perl()) {
|
153 |
82
| if (perl().match("/(.*)\\s+(dispose_\\w+)$/i", name)) {
|
154 |
21
| name = perl().group(1);
|
155 |
| |
156 |
21
| String disposeText = perl().group(2);
|
157 |
| |
158 |
21
| if (disposeText.equalsIgnoreCase("DISPOSE_IGNORE")) {
|
159 |
0
| dispose = StatisticalMeasurement.DISPOSE_IGNORE;
|
160 |
21
| } else if (disposeText.equalsIgnoreCase("DISPOSE_MINIMUM")) {
|
161 |
0
| dispose = StatisticalMeasurement.DISPOSE_MINIMUM;
|
162 |
21
| } else if (disposeText.equalsIgnoreCase("DISPOSE_MEDIAN")) {
|
163 |
0
| dispose = StatisticalMeasurement.DISPOSE_MEDIAN;
|
164 |
21
| } else if (disposeText.equalsIgnoreCase("DISPOSE_AVERAGE")) {
|
165 |
0
| dispose = StatisticalMeasurement.DISPOSE_AVERAGE;
|
166 |
21
| } else if (disposeText.equalsIgnoreCase("DISPOSE_STANDARD_DEVIATION")) {
|
167 |
0
| dispose = StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION;
|
168 |
21
| } else if (disposeText.equalsIgnoreCase("DISPOSE_MAXIMUM")) {
|
169 |
0
| dispose = StatisticalMeasurement.DISPOSE_MAXIMUM;
|
170 |
21
| } else if (disposeText.equalsIgnoreCase("DISPOSE_SUM")) {
|
171 |
21
| dispose = StatisticalMeasurement.DISPOSE_SUM;
|
172 |
0
| } else if (disposeText.equalsIgnoreCase("DISPOSE_NB_DATA_POINTS")) {
|
173 |
0
| dispose = StatisticalMeasurement.DISPOSE_NB_DATA_POINTS;
|
174 |
| } else { |
175 |
0
| dispose = StatisticalMeasurement.DISPOSE_IGNORE;
|
176 |
| } |
177 |
| } else { |
178 |
61
| dispose = StatisticalMeasurement.DISPOSE_IGNORE;
|
179 |
| } |
180 |
| } |
181 |
| |
182 |
82
| Measurement measurement = getContext().getMeasurement(name);
|
183 |
| |
184 |
82
| if (measurement instanceof StatisticalMeasurement) {
|
185 |
21
| StatisticalMeasurement stats = (StatisticalMeasurement) measurement;
|
186 |
| |
187 |
21
| switch (dispose) {
|
188 |
0
| case StatisticalMeasurement.DISPOSE_MINIMUM:
|
189 |
0
| result = stats.getMinimum();
|
190 |
0
| break;
|
191 |
0
| case StatisticalMeasurement.DISPOSE_MEDIAN:
|
192 |
0
| result = stats.getMedian();
|
193 |
0
| break;
|
194 |
0
| case StatisticalMeasurement.DISPOSE_AVERAGE:
|
195 |
0
| result = stats.getAverage();
|
196 |
0
| break;
|
197 |
0
| case StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION:
|
198 |
0
| result = stats.getStandardDeviation();
|
199 |
0
| break;
|
200 |
0
| case StatisticalMeasurement.DISPOSE_MAXIMUM:
|
201 |
0
| result = stats.getMaximum();
|
202 |
0
| break;
|
203 |
21
| case StatisticalMeasurement.DISPOSE_SUM:
|
204 |
21
| result = stats.getSum();
|
205 |
21
| break;
|
206 |
0
| case StatisticalMeasurement.DISPOSE_NB_DATA_POINTS:
|
207 |
0
| result = stats.getNbDataPoints();
|
208 |
0
| break;
|
209 |
0
| case StatisticalMeasurement.DISPOSE_IGNORE:
|
210 |
0
| default:
|
211 |
0
| result = stats.getValue().doubleValue();
|
212 |
0
| break;
|
213 |
| } |
214 |
| } else { |
215 |
61
| result = measurement.getValue().doubleValue();
|
216 |
| } |
217 |
| |
218 |
82
| if (super.isEmpty()) {
|
219 |
57
| setEmpty(measurement.isEmpty());
|
220 |
| } |
221 |
| } |
222 |
| |
223 |
85
| return result;
|
224 |
| } |
225 |
| } |