1 32 33 package com.jeantessier.metrics; 34 35 import java.util.*; 36 37 import org.apache.log4j.*; 38 39 public class Metrics { 40 public static final String PACKAGES = "P"; 41 42 public static final String CLASSES = "C"; 43 public static final String PUBLIC_CLASSES = "PuC"; 44 public static final String FINAL_CLASSES = "FC"; 45 public static final String ABSTRACT_CLASSES = "AC"; 46 public static final String SYNTHETIC_CLASSES = "SynthC"; 47 public static final String INTERFACES = "I"; 48 public static final String DEPRECATED_CLASSES = "DC"; 49 public static final String STATIC_CLASSES = "SC"; 50 51 public static final String PUBLIC_METHODS = "PuM"; 52 public static final String PROTECTED_METHODS = "ProM"; 53 public static final String PRIVATE_METHODS = "ProM"; 54 public static final String PACKAGE_METHODS = "PaM"; 55 public static final String FINAL_METHODS = "FM"; 56 public static final String ABSTRACT_METHODS = "AM"; 57 public static final String DEPRECATED_METHODS = "DM"; 58 public static final String SYNTHETIC_METHODS = "SynthM"; 59 public static final String STATIC_METHODS = "SM"; 60 public static final String SYNCHRONIZED_METHODS = "SynchM"; 61 public static final String NATIVE_METHODS = "NM"; 62 public static final String TRIVIAL_METHODS = "TM"; 63 64 public static final String ATTRIBUTES = "A"; 65 public static final String PUBLIC_ATTRIBUTES = "PuA"; 66 public static final String PROTECTED_ATTRIBUTES = "ProA"; 67 public static final String PRIVATE_ATTRIBUTES = "PriA"; 68 public static final String PACKAGE_ATTRIBUTES = "PaA"; 69 public static final String FINAL_ATTRIBUTES = "FA"; 70 public static final String DEPRECATED_ATTRIBUTES = "DA"; 71 public static final String SYNTHETIC_ATTRIBUTES = "SynthA"; 72 public static final String STATIC_ATTRIBUTES = "SA"; 73 public static final String TRANSIENT_ATTRIBUTES = "TA"; 74 public static final String VOLATILE_ATTRIBUTES = "VA"; 75 76 public static final String INNER_CLASSES = "IC"; 77 public static final String PUBLIC_INNER_CLASSES = "PuIC"; 78 public static final String PROTECTED_INNER_CLASSES = "ProIC"; 79 public static final String PRIVATE_INNER_CLASSES = "PriIC"; 80 public static final String PACKAGE_INNER_CLASSES = "PaIC"; 81 public static final String ABSTRACT_INNER_CLASSES = "AIC"; 82 public static final String FINAL_INNER_CLASSES = "FIC"; 83 public static final String STATIC_INNER_CLASSES = "SIC"; 84 85 public static final String DEPTH_OF_INHERITANCE = "DOI"; 86 public static final String SUBCLASSES = "SUB"; 87 public static final String CLASS_SLOC = "class SLOC"; 88 89 public static final String SLOC = "SLOC"; 90 public static final String PARAMETERS = "PARAM"; 91 public static final String LOCAL_VARIABLES = "LVAR"; 92 93 public static final String INBOUND_INTRA_PACKAGE_DEPENDENCIES = "IIP"; 94 public static final String INBOUND_EXTRA_PACKAGE_DEPENDENCIES = "IEP"; 95 public static final String OUTBOUND_INTRA_PACKAGE_DEPENDENCIES = "OIP"; 96 public static final String OUTBOUND_EXTRA_PACKAGE_DEPENDENCIES = "OEP"; 97 98 public static final String INBOUND_INTRA_CLASS_METHOD_DEPENDENCIES = "IICM"; 99 public static final String INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES = "IIPM"; 100 public static final String INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES = "IEPM"; 101 public static final String OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES = "OICF"; 102 public static final String OUTBOUND_INTRA_PACKAGE_FEATURE_DEPENDENCIES = "OIPF"; 103 public static final String OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES = "OIPC"; 104 public static final String OUTBOUND_EXTRA_PACKAGE_FEATURE_DEPENDENCIES = "OEPF"; 105 public static final String OUTBOUND_EXTRA_PACKAGE_CLASS_DEPENDENCIES = "OEPC"; 106 107 private static final Measurement NULL_MEASUREMENT = new NullMeasurement(); 108 109 private Metrics parent; 110 private String name; 111 112 private Map measurements = new TreeMap(); 113 private Map submetrics = new TreeMap(); 114 115 public Metrics(String name) { 116 this(null, name); 117 } 118 119 123 public Metrics(Metrics parent, String name) { 124 this.parent = parent; 125 this.name = name; 126 127 if (parent == null) { 128 Logger.getLogger(getClass()).debug("Created top-level metrics \"" + name + "\""); 129 } else { 130 Logger.getLogger(getClass()).debug("Created metrics \"" + name + "\" under \"" + parent.getName() + "\""); 131 } 132 } 133 134 public Metrics getParent() { 135 return parent; 136 } 137 138 142 public String getName() { 143 return name; 144 } 145 146 void track(Measurement measurement) { 147 track(measurement.getShortName(), measurement); 148 } 149 150 void track(String name, Measurement measurement) { 151 measurements.put(name, measurement); 152 } 153 154 public void addToMeasurement(String name) { 155 addToMeasurement(name, 1); 156 } 157 158 public void addToMeasurement(String name, int delta) { 159 getMeasurement(name).add(delta); 160 } 161 162 public void addToMeasurement(String name, long delta) { 163 getMeasurement(name).add(delta); 164 } 165 166 public void addToMeasurement(String name, float delta) { 167 getMeasurement(name).add(delta); 168 } 169 170 public void addToMeasurement(String name, double delta) { 171 getMeasurement(name).add(delta); 172 } 173 174 public void addToMeasurement(String name, Object delta) { 175 getMeasurement(name).add(delta); 176 } 177 178 public Measurement getMeasurement(String name) { 179 Measurement result = (Measurement) measurements.get(name); 180 181 if (result == null) { 182 result = NULL_MEASUREMENT; 183 Logger.getLogger(getClass()).info("Null measurement \"" + name + "\" on \"" + getName() + "\""); 184 } 185 186 return result; 187 } 188 189 public boolean hasMeasurement(String name) { 190 return measurements.get(name) != null; 191 } 192 193 public Collection getMeasurementNames() { 194 return Collections.unmodifiableCollection(new TreeSet(measurements.keySet())); 195 } 196 197 public Metrics addSubMetrics(Metrics metrics) { 198 return (Metrics) submetrics.put(metrics.getName(), metrics); 199 } 200 201 public Collection getSubMetrics() { 202 return Collections.unmodifiableCollection(submetrics.values()); 203 } 204 205 public boolean isEmpty() { 206 boolean result = true; 207 208 Iterator i; 209 210 i = measurements.values().iterator(); 211 while (result && i.hasNext()) { 212 Measurement measurement = (Measurement) i.next(); 213 if (measurement.getDescriptor().isVisible()) { 214 result = measurement.isEmpty(); 215 } 216 } 217 218 i = submetrics.values().iterator(); 219 while (result && i.hasNext()) { 220 result = ((Metrics) i.next()).isEmpty(); 221 } 222 223 return result; 224 } 225 226 public boolean isInRange() { 227 boolean result = true; 228 229 Iterator i = measurements.values().iterator(); 230 while (result && i.hasNext()) { 231 result = ((Measurement) i.next()).isInRange(); 232 } 233 234 return result; 235 } 236 237 public String toString() { 238 StringBuffer result = new StringBuffer (); 239 240 result.append(getClass().getName()).append(" ").append(getName()).append(" with ["); 241 242 Iterator i = getMeasurementNames().iterator(); 243 while(i.hasNext()) { 244 String name = (String ) i.next(); 245 Measurement measure = getMeasurement(name); 246 247 result.append("\"").append(name).append("\"(").append(measure.getClass().getName()).append(")"); 248 if (i.hasNext()) { 249 result.append(", "); 250 } 251 } 252 253 result.append("]"); 254 255 return result.toString(); 256 } 257 } 258 | Popular Tags |