1 32 33 package com.jeantessier.metrics; 34 35 import java.io.*; 36 import java.util.*; 37 38 public class CSVPrinter extends Printer { 39 private List descriptors; 40 41 public CSVPrinter(PrintWriter out, List descriptors) { 42 super(out); 43 44 this.descriptors = descriptors; 45 46 appendHeader(); 47 } 48 49 private void appendHeader() { 50 appendLongNames(); 51 appendShortNames(); 52 appendStatSubNames(); 53 } 54 55 private void appendLongNames() { 56 append("\"name\", "); 57 58 Iterator i = descriptors.iterator(); 59 while (i.hasNext()) { 60 MeasurementDescriptor descriptor = (MeasurementDescriptor) i.next(); 61 62 if (descriptor.isVisible()) { 63 if (descriptor.getClassFor().equals(StatisticalMeasurement.class)) { 64 append("\"").append(descriptor.getLongName()).append("\", "); 65 append("\"").append(descriptor.getLongName()).append("\", "); 66 append("\"").append(descriptor.getLongName()).append("\", "); 67 append("\"").append(descriptor.getLongName()).append("\", "); 68 append("\"").append(descriptor.getLongName()).append("\", "); 69 append("\"").append(descriptor.getLongName()).append("\", "); 70 append("\"").append(descriptor.getLongName()).append("\""); 71 } else { 72 append("\"").append(descriptor.getLongName()).append("\""); 73 } 74 75 if (i.hasNext()) { 76 append(", "); 77 } 78 } 79 } 80 81 eol(); 82 } 83 84 private void appendShortNames() { 85 append(", "); 86 87 Iterator i = descriptors.iterator(); 88 while (i.hasNext()) { 89 MeasurementDescriptor descriptor = (MeasurementDescriptor) i.next(); 90 91 if (descriptor.isVisible()) { 92 if (descriptor.getClassFor().equals(StatisticalMeasurement.class)) { 93 append("\"").append(descriptor.getShortName()).append("\", "); 94 append("\"").append(descriptor.getShortName()).append("\", "); 95 append("\"").append(descriptor.getShortName()).append("\", "); 96 append("\"").append(descriptor.getShortName()).append("\", "); 97 append("\"").append(descriptor.getShortName()).append("\", "); 98 append("\"").append(descriptor.getShortName()).append("\", "); 99 append("\"").append(descriptor.getShortName()).append("\""); 100 } else { 101 append("\"").append(descriptor.getShortName()).append("\""); 102 } 103 104 if (i.hasNext()) { 105 append(", "); 106 } 107 } 108 } 109 110 eol(); 111 } 112 113 private void appendStatSubNames() { 114 append(", "); 115 116 Iterator i = descriptors.iterator(); 117 while (i.hasNext()) { 118 MeasurementDescriptor descriptor = (MeasurementDescriptor) i.next(); 119 120 if (descriptor.isVisible()) { 121 if (descriptor.getClassFor().equals(StatisticalMeasurement.class)) { 122 append("minimum, "); 123 append("median, "); 124 append("average, "); 125 append("std dev, "); 126 append("maxium, "); 127 append("sum, "); 128 append("nb"); 129 } 130 131 if (i.hasNext()) { 132 append(", "); 133 } 134 } 135 } 136 137 eol(); 138 } 139 140 public void visitMetrics(Metrics metrics) { 141 if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { 142 append("\"").append(metrics.getName()).append("\", "); 143 144 Iterator i = descriptors.iterator(); 145 while (i.hasNext()) { 146 MeasurementDescriptor descriptor = (MeasurementDescriptor) i.next(); 147 148 if (isShowHiddenMeasurements() || descriptor.isVisible()) { 149 Measurement measurement = metrics.getMeasurement(descriptor.getShortName()); 150 151 measurement.accept(this); 152 153 if (i.hasNext()) { 154 append(", "); 155 } 156 } 157 } 158 159 eol(); 160 } 161 } 162 163 public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { 164 append(measurement.getMinimum()).append(", "); 165 append(measurement.getMedian()).append(", "); 166 append(measurement.getAverage()).append(", "); 167 append(measurement.getStandardDeviation()).append(", "); 168 append(measurement.getMaximum()).append(", "); 169 append(measurement.getSum()).append(", "); 170 append(measurement.getNbDataPoints()); 171 } 172 173 protected void visitMeasurement(Measurement measurement) { 174 append(measurement.getValue()); 175 } 176 } 177 | Popular Tags |