Coverage Report - com.jeantessier.metrics.TextPrinter
 
Classes in this File Line Coverage Branch Coverage Complexity
TextPrinter
0%
0/52
0%
0/18
2
 
 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.io.*;
 36  
 import java.text.*;
 37  
 import java.util.*;
 38  
 
 39  
 public class TextPrinter extends Printer {
 40  0
     private static final NumberFormat valueFormat = new DecimalFormat("#.##");
 41  0
     private static final NumberFormat ratioFormat = new DecimalFormat("#%");
 42  
 
 43  
     private List<MeasurementDescriptor> descriptors;
 44  
 
 45  
     private boolean expandCollectionMeasurements;
 46  
     
 47  0
     private Metrics currentMetrics = null;
 48  
     
 49  
     public TextPrinter(PrintWriter out, List<MeasurementDescriptor> descriptors) {
 50  0
         super(out);
 51  
         
 52  0
         this.descriptors = descriptors;
 53  0
     }
 54  
 
 55  
     public boolean isExpandCollectionMeasurements() {
 56  0
         return expandCollectionMeasurements;
 57  
     }
 58  
 
 59  
     public void setExpandCollectionMeasurements(boolean expandCollectionMeasurements) {
 60  0
         this.expandCollectionMeasurements = expandCollectionMeasurements;
 61  0
     }
 62  
     
 63  
     public void visitMetrics(Metrics metrics) {
 64  0
         if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) {
 65  0
             currentMetrics = metrics;
 66  
             
 67  0
             indent().append(metrics.getName()).eol();
 68  0
             raiseIndent();
 69  
 
 70  0
             for (MeasurementDescriptor descriptor : descriptors) {
 71  0
                 if (isShowHiddenMeasurements() || descriptor.isVisible()) {
 72  0
                     metrics.getMeasurement(descriptor.getShortName()).accept(this);
 73  
                 }
 74  
             }
 75  
             
 76  0
             lowerIndent();
 77  
             
 78  0
             eol();
 79  
         }
 80  0
     }
 81  
 
 82  
     public void visitStatisticalMeasurement(StatisticalMeasurement measurement) {
 83  0
         indent().append(measurement.getLongName()).append(" (").append(measurement.getShortName()).append("): ").append(valueFormat.format(measurement.getValue()));
 84  
 
 85  
         try {
 86  0
             RatioMeasurement ratio = (RatioMeasurement) currentMetrics.getMeasurement(measurement.getShortName() + "R");
 87  0
             append(" (").append(ratioFormat.format(ratio.getValue())).append(")");
 88  0
         } catch (ClassCastException ex) {
 89  
             // Do nothing, no ratio for this measurement
 90  0
         }
 91  
 
 92  0
         append(" ").append(measurement);
 93  
         
 94  0
         eol();
 95  0
     }
 96  
     
 97  
     public void visitRatioMeasurement(RatioMeasurement measurement) {
 98  0
         if (!measurement.getShortName().endsWith("R")) {
 99  0
             super.visitRatioMeasurement(measurement);
 100  
         }
 101  0
     }
 102  
     
 103  
     public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) {
 104  0
         super.visitContextAccumulatorMeasurement(measurement);
 105  
 
 106  0
         visitCollectionMeasurement(measurement);
 107  0
     }
 108  
     
 109  
     public void visitNameListMeasurement(NameListMeasurement measurement) {
 110  0
         super.visitNameListMeasurement(measurement);
 111  
 
 112  0
         visitCollectionMeasurement(measurement);
 113  0
     }
 114  
     
 115  
     public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) {
 116  0
         super.visitSubMetricsAccumulatorMeasurement(measurement);
 117  
 
 118  0
         visitCollectionMeasurement(measurement);
 119  0
     }
 120  
     
 121  
     protected void visitCollectionMeasurement(CollectionMeasurement measurement) {
 122  0
         if (isExpandCollectionMeasurements()) {
 123  0
             raiseIndent();
 124  0
             for (String value : measurement.getValues()) {
 125  0
                 indent().append(value).eol();
 126  
             }
 127  0
             lowerIndent();
 128  
         }
 129  0
     }
 130  
     
 131  
     protected void visitMeasurement(Measurement measurement) {
 132  0
         indent().append(measurement.getLongName()).append(" (").append(measurement.getShortName()).append("): ").append(valueFormat.format(measurement.getValue()));
 133  
 
 134  
         try {
 135  0
             RatioMeasurement ratio = (RatioMeasurement) currentMetrics.getMeasurement(measurement.getShortName() + "R");
 136  0
             append(" (").append(ratioFormat.format(ratio.getValue())).append(")");
 137  0
         } catch (ClassCastException ex) {
 138  
             // Do nothing, no ratio for this measurement
 139  0
         }
 140  
 
 141  0
         eol();
 142  0
     }
 143  
 }