KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc ex) {
92             // Do nothing, no ratio for this measurement
93
}
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 JavaDoc ex) {
142             // Do nothing, no ratio for this measurement
143
}
144         
145         eol();
146     }
147 }
148
Popular Tags