1 32 33 package com.jeantessier.metrics; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.log4j.*; 39 40 60 public abstract class AccumulatorMeasurement extends MeasurementBase implements CollectionMeasurement { 61 private Map terms = new HashMap(); 62 private Collection values = new TreeSet(); 63 64 public AccumulatorMeasurement(MeasurementDescriptor descriptor, Metrics context, String initText) { 65 super(descriptor, context, initText); 66 67 if (initText != null) { 68 try { 69 BufferedReader in = new BufferedReader(new StringReader(initText)); 70 String line; 71 72 while ((line = in.readLine()) != null) { 73 synchronized (perl()) { 74 if (perl().match("/^\\s*(\\S+)\\s*(.*)/", line)) { 75 String name = perl().group(1); 76 String re = perl().group(2); 77 78 Collection res = (Collection) terms.get(name); 79 if (res == null) { 80 res = new ArrayList(); 81 terms.put(name, res); 82 } 83 84 if (re != null && re.length() > 0) { 85 res.add(re); 86 } 87 } 88 } 89 } 90 91 in.close(); 92 } catch (Exception ex) { 93 Logger.getLogger(getClass()).debug("Cannot initialize with \"" + initText + "\"", ex); 94 terms.clear(); 95 } 96 } 97 98 logTerms(initText); 99 } 100 101 private void logTerms(String initText) { 102 Logger.getLogger(getClass()).debug("Initialize with\n" + initText); 103 Logger.getLogger(getClass()).debug("Terms:"); 104 105 Iterator i = terms.entrySet().iterator(); 106 while (i.hasNext()) { 107 Map.Entry entry = (Map.Entry) i.next(); 108 Logger.getLogger(getClass()).debug("\t" + entry.getKey()); 109 110 Iterator j = ((Collection) entry.getValue()).iterator(); 111 while (j.hasNext()) { 112 Logger.getLogger(getClass()).debug("\t\t" + j.next()); 113 } 114 } 115 } 116 117 public Number getValue() { 118 return new Integer (getValues().size()); 119 } 120 121 public boolean isEmpty() { 122 return getValues().isEmpty(); 123 } 124 125 protected double compute() { 126 return getValues().size(); 127 } 128 129 public Collection getValues() { 130 if (!isCached()) { 131 values.clear(); 132 133 populateValues(); 134 135 setCached(true); 136 } 137 138 return Collections.unmodifiableCollection(values); 139 } 140 141 protected abstract void populateValues(); 142 143 protected void filterMetrics(Metrics metrics) { 144 Iterator i = terms.entrySet().iterator(); 145 while (i.hasNext()) { 146 Map.Entry entry = (Map.Entry) i.next(); 147 String name = (String ) entry.getKey(); 148 Collection res = (Collection) entry.getValue(); 149 150 Measurement measurement = metrics.getMeasurement(name); 151 if (measurement instanceof CollectionMeasurement) { 152 filterMeasurement((CollectionMeasurement) measurement, res); 153 } 154 } 155 } 156 157 private void filterMeasurement(CollectionMeasurement measurement, Collection res) { 158 if (res.isEmpty()) { 159 values.addAll(measurement.getValues()); 160 } else { 161 Iterator i = measurement.getValues().iterator(); 162 while (i.hasNext()) { 163 filterElement((String ) i.next(), res); 164 } 165 } 166 } 167 168 private void filterElement(String element, Collection res) { 169 boolean found = false; 170 Iterator i = res.iterator(); 171 while (!found && i.hasNext()) { 172 found = evaluateRE((String ) i.next(), element); 173 } 174 } 175 176 private boolean evaluateRE(String re, String element) { 177 boolean result = false; 178 179 synchronized (perl()) { 180 if (perl().match(re, element)) { 181 result = true; 182 if (perl().group(1) != null) { 183 values.add(perl().group(1)); 184 } else { 185 values.add(element); 186 } 187 } 188 } 189 190 return result; 191 } 192 } 193 | Popular Tags |