1 32 33 package com.jeantessier.metrics; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.log4j.*; 39 40 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 initText) { 60 super(descriptor, context, initText); 61 62 try { 63 BufferedReader in = new BufferedReader(new StringReader(initText)); 64 String line; 65 66 while ((line = in.readLine()) != null) { 67 terms.add(line.trim()); 68 } 69 70 in.close(); 71 } catch (Exception 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 term = (String ) 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 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 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 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 |