Coverage Report - com.jeantessier.metrics.Metrics
 
Classes in this File Line Coverage Branch Coverage Complexity
Metrics
87%
70/80
100%
24/24
1.407
 
 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  2
     private static final Measurement NULL_MEASUREMENT = new NullMeasurement();
 41  
     
 42  
     private Metrics parent;
 43  
     private String  name;
 44  
 
 45  1529
     private Map<String, Measurement> measurements = new TreeMap<String, Measurement>();
 46  1529
     private Map<String, Metrics> submetrics = new TreeMap<String, Metrics>();
 47  
 
 48  
     public Metrics(String name) {
 49  547
         this(null, name);
 50  547
     }
 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  1529
     public Metrics(Metrics parent, String name) {
 60  1529
         this.parent = parent;
 61  1529
         this.name   = name;
 62  
 
 63  1529
         if (parent == null) {
 64  547
             Logger.getLogger(getClass()).debug("Created top-level metrics \"" + name + "\"");
 65  
         } else {
 66  982
             Logger.getLogger(getClass()).debug("Created metrics \"" + name + "\" under \"" + parent.getName() + "\"");
 67  
         }
 68  1529
     }
 69  
 
 70  
     public Metrics getParent() {
 71  5209
         return parent;
 72  
     }
 73  
 
 74  
     /**
 75  
      *  @return The name of the element being measured
 76  
      *          (e.g., class name, method name).
 77  
      */
 78  
     public String getName() {
 79  14114
         return name;
 80  
     }
 81  
 
 82  
     void track(Measurement measurement) {
 83  36952
         track(measurement.getShortName(), measurement);
 84  36952
     }
 85  
     
 86  
     void track(String name, Measurement measurement) {
 87  37474
         measurements.put(name, measurement);
 88  37474
     }
 89  
 
 90  
     public void addToMeasurement(BasicMeasurements name) {
 91  206
         addToMeasurement(name.getAbbreviation());
 92  206
     }
 93  
 
 94  
     public void addToMeasurement(String name) {
 95  206
         addToMeasurement(name, 1);
 96  206
     }
 97  
 
 98  
     public void addToMeasurement(BasicMeasurements name, int delta) {
 99  3113
         addToMeasurement(name.getAbbreviation(), delta);
 100  3113
     }
 101  
 
 102  
     public void addToMeasurement(String name, int delta) {
 103  3829
         getMeasurement(name).add(delta);
 104  3829
     }
 105  
 
 106  
     public void addToMeasurement(BasicMeasurements name, long delta) {
 107  0
         addToMeasurement(name.getAbbreviation(), delta);
 108  0
     }
 109  
 
 110  
     public void addToMeasurement(String name, long delta) {
 111  0
         getMeasurement(name).add(delta);
 112  0
     }
 113  
     
 114  
     public void addToMeasurement(BasicMeasurements name, float delta) {
 115  0
         addToMeasurement(name.getAbbreviation(), delta);
 116  0
     }
 117  
 
 118  
     public void addToMeasurement(String name, float delta) {
 119  0
         getMeasurement(name).add(delta);
 120  0
     }
 121  
     
 122  
     public void addToMeasurement(BasicMeasurements name, double delta) {
 123  0
         addToMeasurement(name.getAbbreviation(), delta);
 124  0
     }
 125  
 
 126  
     public void addToMeasurement(String name, double delta) {
 127  8
         getMeasurement(name).add(delta);
 128  8
     }
 129  
     
 130  
     public void addToMeasurement(BasicMeasurements name, Object delta) {
 131  3652
         addToMeasurement(name.getAbbreviation(), delta);
 132  3652
     }
 133  
 
 134  
     public void addToMeasurement(String name, Object delta) {
 135  3892
         getMeasurement(name).add(delta);
 136  3892
     }
 137  
 
 138  
     public Measurement getMeasurement(BasicMeasurements name) {
 139  110
         return getMeasurement(name.getAbbreviation());
 140  
     }
 141  
 
 142  
     public Measurement getMeasurement(String name) {
 143  9270
         Measurement result = measurements.get(name);
 144  
         
 145  9270
         if (result == null) {
 146  754
             result = NULL_MEASUREMENT;
 147  754
             Logger.getLogger(getClass()).info("Null measurement \"" + name + "\" on \"" + getName() + "\"");
 148  
         }
 149  
 
 150  9270
         return result;
 151  
     }
 152  
 
 153  
     public boolean hasMeasurement(String name) {
 154  6
         return measurements.get(name) != null;
 155  
     }
 156  
     
 157  
     public Collection<String> getMeasurementNames() {
 158  264
         return Collections.unmodifiableCollection(measurements.keySet());
 159  
     }
 160  
     
 161  
     public Metrics addSubMetrics(Metrics metrics) {
 162  2361
         return submetrics.put(metrics.getName(), metrics);
 163  
     }
 164  
     
 165  
     public Collection<Metrics> getSubMetrics() {
 166  1398
         return Collections.unmodifiableCollection(submetrics.values());
 167  
     }
 168  
 
 169  
     public boolean isEmpty() {
 170  36
         boolean result = true;
 171  
 
 172  36
         Iterator<Measurement> i = measurements.values().iterator();
 173  84
         while (result && i.hasNext()) {
 174  48
             Measurement measurement = i.next();
 175  48
             if (measurement.getDescriptor().isVisible()) {
 176  45
                 result = measurement.isEmpty();
 177  
             }
 178  48
         }
 179  
 
 180  36
         Iterator<Metrics> j = submetrics.values().iterator();
 181  43
         while (result && j.hasNext()) {
 182  7
             result = j.next().isEmpty();
 183  
         }
 184  
         
 185  36
         return result;
 186  
     }
 187  
 
 188  
     public boolean isInRange() {
 189  3
         boolean result = true;
 190  
 
 191  3
         Iterator<Measurement> i = measurements.values().iterator();
 192  7
         while (result && i.hasNext()) {
 193  4
             result = i.next().isInRange();
 194  
         }
 195  
         
 196  3
         return result;
 197  
     }
 198  
     
 199  
     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  484
         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  261
         }
 214  
 
 215  223
         result.append("]");
 216  
 
 217  223
         return result.toString();
 218  
     }
 219  
 }