Clover coverage report - Dependency Finder
Coverage timestamp: Mon Nov 29 2010 15:00:50 PST
file stats: LOC: 219   Methods: 27
NCLOC: 131   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Metrics.java 100% 91.4% 81.5% 90.1%
coverage coverage
 1    /*
 2    * Copyright (c) 2001-2009, 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.util.*;
 36   
 37    import org.apache.log4j.*;
 38   
 39    public class Metrics {
 40    private static final Measurement NULL_MEASUREMENT = new NullMeasurement();
 41   
 42    private Metrics parent;
 43    private String name;
 44   
 45    private Map<String, Measurement> measurements = new TreeMap<String, Measurement>();
 46    private Map<String, Metrics> submetrics = new TreeMap<String, Metrics>();
 47   
 48  547 public Metrics(String name) {
 49  547 this(null, name);
 50    }
 51   
 52    /**
 53    * @param parent The context for this metrics (e.g., methods's class, class'
 54    * package). You may pass <code>null</code> to create
 55    * top-level metrics.
 56    * @param name The name of the element being measured
 57    * (e.g., class name, method name).
 58    */
 59  2314 public Metrics(Metrics parent, String name) {
 60  2314 this.parent = parent;
 61  2314 this.name = name;
 62   
 63  2314 if (parent == null) {
 64  547 Logger.getLogger(getClass()).debug("Created top-level metrics \"" + name + "\"");
 65    } else {
 66  1767 Logger.getLogger(getClass()).debug("Created metrics \"" + name + "\" under \"" + parent.getName() + "\"");
 67    }
 68    }
 69   
 70  17137 public Metrics getParent() {
 71  17137 return parent;
 72    }
 73   
 74    /**
 75    * @return The name of the element being measured
 76    * (e.g., class name, method name).
 77    */
 78  57283 public String getName() {
 79  57283 return name;
 80    }
 81   
 82  66645 void track(Measurement measurement) {
 83  66645 track(measurement.getShortName(), measurement);
 84    }
 85   
 86  67167 void track(String name, Measurement measurement) {
 87  67167 measurements.put(name, measurement);
 88    }
 89   
 90  350 public void addToMeasurement(BasicMeasurements name) {
 91  350 addToMeasurement(name.getAbbreviation());
 92    }
 93   
 94  350 public void addToMeasurement(String name) {
 95  350 addToMeasurement(name, 1);
 96    }
 97   
 98  5547 public void addToMeasurement(BasicMeasurements name, int delta) {
 99  5547 addToMeasurement(name.getAbbreviation(), delta);
 100    }
 101   
 102  6407 public void addToMeasurement(String name, int delta) {
 103  6407 getMeasurement(name).add(delta);
 104    }
 105   
 106  0 public void addToMeasurement(BasicMeasurements name, long delta) {
 107  0 addToMeasurement(name.getAbbreviation(), delta);
 108    }
 109   
 110  0 public void addToMeasurement(String name, long delta) {
 111  0 getMeasurement(name).add(delta);
 112    }
 113   
 114  0 public void addToMeasurement(BasicMeasurements name, float delta) {
 115  0 addToMeasurement(name.getAbbreviation(), delta);
 116    }
 117   
 118  0 public void addToMeasurement(String name, float delta) {
 119  0 getMeasurement(name).add(delta);
 120    }
 121   
 122  0 public void addToMeasurement(BasicMeasurements name, double delta) {
 123  0 addToMeasurement(name.getAbbreviation(), delta);
 124    }
 125   
 126  8 public void addToMeasurement(String name, double delta) {
 127  8 getMeasurement(name).add(delta);
 128    }
 129   
 130  15154 public void addToMeasurement(BasicMeasurements name, Object delta) {
 131  15154 addToMeasurement(name.getAbbreviation(), delta);
 132    }
 133   
 134  15394 public void addToMeasurement(String name, Object delta) {
 135  15394 getMeasurement(name).add(delta);
 136    }
 137   
 138  108 public Measurement getMeasurement(BasicMeasurements name) {
 139  108 return getMeasurement(name.getAbbreviation());
 140    }
 141   
 142  23256 public Measurement getMeasurement(String name) {
 143  23256 Measurement result = measurements.get(name);
 144   
 145  23256 if (result == null) {
 146  1656 result = NULL_MEASUREMENT;
 147  1656 Logger.getLogger(getClass()).info("Null measurement \"" + name + "\" on \"" + getName() + "\"");
 148    }
 149   
 150  23256 return result;
 151    }
 152   
 153  6 public boolean hasMeasurement(String name) {
 154  6 return measurements.get(name) != null;
 155    }
 156   
 157  264 public Collection<String> getMeasurementNames() {
 158  264 return Collections.unmodifiableCollection(measurements.keySet());
 159    }
 160   
 161  3954 public Metrics addSubMetrics(Metrics metrics) {
 162  3954 return submetrics.put(metrics.getName(), metrics);
 163    }
 164   
 165  1415 public Collection<Metrics> getSubMetrics() {
 166  1415 return Collections.unmodifiableCollection(submetrics.values());
 167    }
 168   
 169  36 public boolean isEmpty() {
 170  36 boolean result = true;
 171   
 172  36 Iterator<Measurement> i = measurements.values().iterator();
 173  36 while (result && i.hasNext()) {
 174  46 Measurement measurement = i.next();
 175  46 if (measurement.getDescriptor().isVisible()) {
 176  43 result = measurement.isEmpty();
 177    }
 178    }
 179   
 180  36 Iterator<Metrics> j = submetrics.values().iterator();
 181  36 while (result && j.hasNext()) {
 182  7 result = j.next().isEmpty();
 183    }
 184   
 185  36 return result;
 186    }
 187   
 188  3 public boolean isInRange() {
 189  3 boolean result = true;
 190   
 191  3 Iterator<Measurement> i = measurements.values().iterator();
 192  3 while (result && i.hasNext()) {
 193  4 result = i.next().isInRange();
 194    }
 195   
 196  3 return result;
 197    }
 198   
 199  223 public String toString() {
 200  223 StringBuffer result = new StringBuffer();
 201   
 202  223 result.append(getClass().getName()).append(" ").append(getName()).append(" with [");
 203   
 204  223 Iterator<String> i = getMeasurementNames().iterator();
 205  223 while (i.hasNext()) {
 206  261 String name = i.next();
 207  261 Measurement measure = getMeasurement(name);
 208   
 209  261 result.append("\"").append(name).append("\"(").append(measure.getClass().getName()).append(")");
 210  261 if (i.hasNext()) {
 211  38 result.append(", ");
 212    }
 213    }
 214   
 215  223 result.append("]");
 216   
 217  223 return result.toString();
 218    }
 219    }