001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: public class CSVPrinter extends Printer {
039: private List<MeasurementDescriptor> descriptors;
040:
041: public CSVPrinter(PrintWriter out,
042: List<MeasurementDescriptor> descriptors) {
043: super (out);
044:
045: this .descriptors = descriptors;
046:
047: appendHeader();
048: }
049:
050: private void appendHeader() {
051: appendLongNames();
052: appendShortNames();
053: appendStatSubNames();
054: }
055:
056: private void appendLongNames() {
057: append("\"name\", ");
058:
059: Iterator<MeasurementDescriptor> i = descriptors.iterator();
060: while (i.hasNext()) {
061: MeasurementDescriptor descriptor = i.next();
062:
063: if (descriptor.isVisible()) {
064: if (descriptor.getClassFor().equals(
065: StatisticalMeasurement.class)) {
066: append("\"").append(descriptor.getLongName())
067: .append("\", ");
068: append("\"").append(descriptor.getLongName())
069: .append("\", ");
070: append("\"").append(descriptor.getLongName())
071: .append("\", ");
072: append("\"").append(descriptor.getLongName())
073: .append("\", ");
074: append("\"").append(descriptor.getLongName())
075: .append("\", ");
076: append("\"").append(descriptor.getLongName())
077: .append("\", ");
078: append("\"").append(descriptor.getLongName())
079: .append("\"");
080: } else {
081: append("\"").append(descriptor.getLongName())
082: .append("\"");
083: }
084:
085: if (i.hasNext()) {
086: append(", ");
087: }
088: }
089: }
090:
091: eol();
092: }
093:
094: private void appendShortNames() {
095: append(", ");
096:
097: Iterator<MeasurementDescriptor> i = descriptors.iterator();
098: while (i.hasNext()) {
099: MeasurementDescriptor descriptor = i.next();
100:
101: if (descriptor.isVisible()) {
102: if (descriptor.getClassFor().equals(
103: StatisticalMeasurement.class)) {
104: append("\"").append(descriptor.getShortName())
105: .append("\", ");
106: append("\"").append(descriptor.getShortName())
107: .append("\", ");
108: append("\"").append(descriptor.getShortName())
109: .append("\", ");
110: append("\"").append(descriptor.getShortName())
111: .append("\", ");
112: append("\"").append(descriptor.getShortName())
113: .append("\", ");
114: append("\"").append(descriptor.getShortName())
115: .append("\", ");
116: append("\"").append(descriptor.getShortName())
117: .append("\"");
118: } else {
119: append("\"").append(descriptor.getShortName())
120: .append("\"");
121: }
122:
123: if (i.hasNext()) {
124: append(", ");
125: }
126: }
127: }
128:
129: eol();
130: }
131:
132: private void appendStatSubNames() {
133: append(", ");
134:
135: Iterator<MeasurementDescriptor> i = descriptors.iterator();
136: while (i.hasNext()) {
137: MeasurementDescriptor descriptor = i.next();
138:
139: if (descriptor.isVisible()) {
140: if (descriptor.getClassFor().equals(
141: StatisticalMeasurement.class)) {
142: append("minimum, ");
143: append("median, ");
144: append("average, ");
145: append("std dev, ");
146: append("maxium, ");
147: append("sum, ");
148: append("nb");
149: }
150:
151: if (i.hasNext()) {
152: append(", ");
153: }
154: }
155: }
156:
157: eol();
158: }
159:
160: public void visitMetrics(Metrics metrics) {
161: if (isShowEmptyMetrics() || isShowHiddenMeasurements()
162: || !metrics.isEmpty()) {
163: append("\"").append(metrics.getName()).append("\", ");
164:
165: Iterator<MeasurementDescriptor> i = descriptors.iterator();
166: while (i.hasNext()) {
167: MeasurementDescriptor descriptor = i.next();
168:
169: if (isShowHiddenMeasurements()
170: || descriptor.isVisible()) {
171: Measurement measurement = metrics
172: .getMeasurement(descriptor.getShortName());
173:
174: measurement.accept(this );
175:
176: if (i.hasNext()) {
177: append(", ");
178: }
179: }
180: }
181:
182: eol();
183: }
184: }
185:
186: public void visitStatisticalMeasurement(
187: StatisticalMeasurement measurement) {
188: append(measurement.getMinimum()).append(", ");
189: append(measurement.getMedian()).append(", ");
190: append(measurement.getAverage()).append(", ");
191: append(measurement.getStandardDeviation()).append(", ");
192: append(measurement.getMaximum()).append(", ");
193: append(measurement.getSum()).append(", ");
194: append(measurement.getNbDataPoints());
195: }
196:
197: protected void visitMeasurement(Measurement measurement) {
198: append(measurement.getValue());
199: }
200: }
|