KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > CSVPrinter


1 /*
2  * Copyright (c) 2001-2005, 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.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