1 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 private static final NumberFormat valueFormat = new DecimalFormat("#.##"); 41 private static final NumberFormat ratioFormat = new DecimalFormat("#%"); 42 43 private List descriptors; 44 45 private boolean expandCollectionMeasurements; 46 47 private Metrics currentMetrics = null; 48 49 public TextPrinter(PrintWriter out, List descriptors) { 50 super(out); 51 52 this.descriptors = descriptors; 53 } 54 55 public boolean isExpandCollectionMeasurements() { 56 return expandCollectionMeasurements; 57 } 58 59 public void setExpandCollectionMeasurements(boolean expandCollectionMeasurements) { 60 this.expandCollectionMeasurements = expandCollectionMeasurements; 61 } 62 63 public void visitMetrics(Metrics metrics) { 64 if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { 65 currentMetrics = metrics; 66 67 indent().append(metrics.getName()).eol(); 68 raiseIndent(); 69 70 Iterator i = descriptors.iterator(); 71 while (i.hasNext()) { 72 MeasurementDescriptor descriptor = (MeasurementDescriptor) i.next(); 73 74 if (isShowHiddenMeasurements() || descriptor.isVisible()) { 75 metrics.getMeasurement(descriptor.getShortName()).accept(this); 76 } 77 } 78 79 lowerIndent(); 80 81 eol(); 82 } 83 } 84 85 public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { 86 indent().append(measurement.getLongName()).append(" (").append(measurement.getShortName()).append("): ").append(valueFormat.format(measurement.doubleValue())); 87 88 try { 89 RatioMeasurement ratio = (RatioMeasurement) currentMetrics.getMeasurement(measurement.getShortName() + "R"); 90 append(" (").append(ratioFormat.format(ratio.getValue())).append(")"); 91 } catch (ClassCastException ex) { 92 } 94 95 append(" ").append(measurement); 96 97 eol(); 98 } 99 100 public void visitRatioMeasurement(RatioMeasurement measurement) { 101 if (!measurement.getShortName().endsWith("R")) { 102 super.visitRatioMeasurement(measurement); 103 } 104 } 105 106 public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { 107 super.visitContextAccumulatorMeasurement(measurement); 108 109 visitCollectionMeasurement(measurement); 110 } 111 112 public void visitNameListMeasurement(NameListMeasurement measurement) { 113 super.visitNameListMeasurement(measurement); 114 115 visitCollectionMeasurement(measurement); 116 } 117 118 public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { 119 super.visitSubMetricsAccumulatorMeasurement(measurement); 120 121 visitCollectionMeasurement(measurement); 122 } 123 124 protected void visitCollectionMeasurement(CollectionMeasurement measurement) { 125 if (isExpandCollectionMeasurements()) { 126 raiseIndent(); 127 Iterator i = measurement.getValues().iterator(); 128 while (i.hasNext()) { 129 indent().append(i.next()).eol(); 130 } 131 lowerIndent(); 132 } 133 } 134 135 protected void visitMeasurement(Measurement measurement) { 136 indent().append(measurement.getLongName()).append(" (").append(measurement.getShortName()).append("): ").append(valueFormat.format(measurement.getValue())); 137 138 try { 139 RatioMeasurement ratio = (RatioMeasurement) currentMetrics.getMeasurement(measurement.getShortName() + "R"); 140 append(" (").append(ratioFormat.format(ratio.getValue())).append(")"); 141 } catch (ClassCastException ex) { 142 } 144 145 eol(); 146 } 147 } 148 | Popular Tags |