KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > SumMeasurement


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  * <p>Adds up numerical values. Use it with just one term to alias
42  * other measurements.</p>
43  *
44  * <p>This is the syntax for initializing this type of
45  * measurement:</p>
46  *
47  * <pre>
48  * &lt;init&gt;
49  * number | measurement name [DISPOSE_x]
50  * ...
51  * &lt;/init&gt;
52  * </pre>
53  */

54 public class SumMeasurement extends MeasurementBase {
55     private List terms = new LinkedList();
56
57     private double value = 0.0;
58
59     public SumMeasurement(MeasurementDescriptor descriptor, Metrics context, String JavaDoc initText) {
60         super(descriptor, context, initText);
61
62         try {
63             BufferedReader in = new BufferedReader(new StringReader(initText));
64             String JavaDoc line;
65
66             while ((line = in.readLine()) != null) {
67                 terms.add(line.trim());
68             }
69
70             in.close();
71         } catch (Exception JavaDoc ex) {
72             Logger.getLogger(getClass()).debug("Cannot initialize with \"" + initText + "\"", ex);
73             terms.clear();
74         }
75     }
76
77     public List getTerms() {
78         return terms;
79     }
80
81     public boolean isEmpty() {
82         compute();
83
84         return super.isEmpty();
85     }
86     
87     public void accept(MeasurementVisitor visitor) {
88         visitor.visitSumMeasurement(this);
89     }
90
91     protected double compute() {
92         if (!isCached()) {
93             synchronized (this) {
94                 if (!isCached()) {
95                     value = 0.0;
96                     setEmpty(true);
97
98                     if (getContext() != null) {
99                         Logger.getLogger(getClass()).debug("Start computing \"" + getShortName() + "\" on \"" + getContext().getName() + "\": value=" + value);
100                     } else {
101                         Logger.getLogger(getClass()).debug("Start computing \"" + getShortName() + "\" on null: value=" + value);
102                     }
103                     
104                     Iterator i = getTerms().iterator();
105                     while (i.hasNext()) {
106                         String JavaDoc term = (String JavaDoc) i.next();
107                         
108                         Logger.getLogger(getClass()).debug("Evaluating term \"" + term + "\"");
109                         
110                         double termValue = Double.NaN;
111                         
112                         try {
113                             termValue = Double.parseDouble(term);
114                         } catch (NumberFormatException JavaDoc ex) {
115                             if (term.startsWith("-")) {
116                                 termValue = -1 * evaluateMeasurement(term.substring(1));
117                             } else {
118                                 termValue = evaluateMeasurement(term);
119                             }
120                         }
121                         
122                         Logger.getLogger(getClass()).debug("term \"" + term + "\" is " + termValue);
123                         
124                         value += termValue;
125                         
126                         Logger.getLogger(getClass()).debug("value=" + value);
127                     }
128                     
129                     if (getContext() != null) {
130                         Logger.getLogger(getClass()).debug("Stop computing \"" + getShortName() + "\" on \"" + getContext().getName() + "\": value=" + value);
131                     } else {
132                         Logger.getLogger(getClass()).debug("Stop computing \"" + getShortName() + "\" on null: value=" + value);
133                     }
134                     
135                     setCached(true);
136                 }
137             }
138         }
139
140         if (getContext() != null) {
141             Logger.getLogger(getClass()).debug("\"" + getShortName() + "\" on \"" + getContext().getName() + "\": value=" + value);
142         } else {
143             Logger.getLogger(getClass()).debug("\"" + getShortName() + "\" on null: value=" + value);
144         }
145
146         return value;
147     }
148
149     private double evaluateMeasurement(String JavaDoc name) {
150         double result = 0;
151
152         if (name.length() != 0) {
153             int dispose;
154             
155             synchronized (perl()) {
156                 if (perl().match("/(.*)\\s+(dispose_\\w+)$/i", name)) {
157                     name = perl().group(1);
158                     
159                     String JavaDoc disposeText = perl().group(2);
160                     
161                     if (disposeText.equalsIgnoreCase("DISPOSE_IGNORE")) {
162                         dispose = StatisticalMeasurement.DISPOSE_IGNORE;
163                     } else if (disposeText.equalsIgnoreCase("DISPOSE_MINIMUM")) {
164                         dispose = StatisticalMeasurement.DISPOSE_MINIMUM;
165                     } else if (disposeText.equalsIgnoreCase("DISPOSE_MEDIAN")) {
166                         dispose = StatisticalMeasurement.DISPOSE_MEDIAN;
167                     } else if (disposeText.equalsIgnoreCase("DISPOSE_AVERAGE")) {
168                         dispose = StatisticalMeasurement.DISPOSE_AVERAGE;
169                     } else if (disposeText.equalsIgnoreCase("DISPOSE_STANDARD_DEVIATION")) {
170                         dispose = StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION;
171                     } else if (disposeText.equalsIgnoreCase("DISPOSE_MAXIMUM")) {
172                         dispose = StatisticalMeasurement.DISPOSE_MAXIMUM;
173                     } else if (disposeText.equalsIgnoreCase("DISPOSE_SUM")) {
174                         dispose = StatisticalMeasurement.DISPOSE_SUM;
175                     } else if (disposeText.equalsIgnoreCase("DISPOSE_NB_DATA_POINTS")) {
176                         dispose = StatisticalMeasurement.DISPOSE_NB_DATA_POINTS;
177                     } else {
178                         dispose = StatisticalMeasurement.DISPOSE_IGNORE;
179                     }
180                 } else {
181                     dispose = StatisticalMeasurement.DISPOSE_IGNORE;
182                 }
183             }
184             
185             Measurement measurement = getContext().getMeasurement(name);
186             
187             if (measurement instanceof StatisticalMeasurement) {
188                 StatisticalMeasurement stats = (StatisticalMeasurement) measurement;
189                 
190                 switch (dispose) {
191                     case StatisticalMeasurement.DISPOSE_MINIMUM:
192                         result = stats.getMinimum();
193                         break;
194                     case StatisticalMeasurement.DISPOSE_MEDIAN:
195                         result = stats.getMedian();
196                         break;
197                     case StatisticalMeasurement.DISPOSE_AVERAGE:
198                         result = stats.getAverage();
199                         break;
200                     case StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION:
201                         result = stats.getStandardDeviation();
202                         break;
203                     case StatisticalMeasurement.DISPOSE_MAXIMUM:
204                         result = stats.getMaximum();
205                         break;
206                     case StatisticalMeasurement.DISPOSE_SUM:
207                         result = stats.getSum();
208                         break;
209                     case StatisticalMeasurement.DISPOSE_NB_DATA_POINTS:
210                         result = stats.getNbDataPoints();
211                         break;
212                     case StatisticalMeasurement.DISPOSE_IGNORE:
213                     default:
214                         result = stats.doubleValue();
215                         break;
216                 }
217             } else {
218                 result = measurement.doubleValue();
219             }
220
221             if (super.isEmpty()) {
222                 setEmpty(measurement.isEmpty());
223             }
224         }
225                 
226         return result;
227     }
228 }
229
Popular Tags