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.text.*;
037: import java.util.*;
038:
039: public class TextPrinter extends Printer {
040: private static final NumberFormat valueFormat = new DecimalFormat(
041: "#.##");
042: private static final NumberFormat ratioFormat = new DecimalFormat(
043: "#%");
044:
045: private List<MeasurementDescriptor> descriptors;
046:
047: private boolean expandCollectionMeasurements;
048:
049: private Metrics currentMetrics = null;
050:
051: public TextPrinter(PrintWriter out,
052: List<MeasurementDescriptor> descriptors) {
053: super (out);
054:
055: this .descriptors = descriptors;
056: }
057:
058: public boolean isExpandCollectionMeasurements() {
059: return expandCollectionMeasurements;
060: }
061:
062: public void setExpandCollectionMeasurements(
063: boolean expandCollectionMeasurements) {
064: this .expandCollectionMeasurements = expandCollectionMeasurements;
065: }
066:
067: public void visitMetrics(Metrics metrics) {
068: if (isShowEmptyMetrics() || isShowHiddenMeasurements()
069: || !metrics.isEmpty()) {
070: currentMetrics = metrics;
071:
072: indent().append(metrics.getName()).eol();
073: raiseIndent();
074:
075: for (MeasurementDescriptor descriptor : descriptors) {
076: if (isShowHiddenMeasurements()
077: || descriptor.isVisible()) {
078: metrics.getMeasurement(descriptor.getShortName())
079: .accept(this );
080: }
081: }
082:
083: lowerIndent();
084:
085: eol();
086: }
087: }
088:
089: public void visitStatisticalMeasurement(
090: StatisticalMeasurement measurement) {
091: indent().append(measurement.getLongName()).append(" (").append(
092: measurement.getShortName()).append("): ").append(
093: valueFormat.format(measurement.doubleValue()));
094:
095: try {
096: RatioMeasurement ratio = (RatioMeasurement) currentMetrics
097: .getMeasurement(measurement.getShortName() + "R");
098: append(" (").append(ratioFormat.format(ratio.getValue()))
099: .append(")");
100: } catch (ClassCastException ex) {
101: // Do nothing, no ratio for this measurement
102: }
103:
104: append(" ").append(measurement);
105:
106: eol();
107: }
108:
109: public void visitRatioMeasurement(RatioMeasurement measurement) {
110: if (!measurement.getShortName().endsWith("R")) {
111: super .visitRatioMeasurement(measurement);
112: }
113: }
114:
115: public void visitContextAccumulatorMeasurement(
116: ContextAccumulatorMeasurement measurement) {
117: super .visitContextAccumulatorMeasurement(measurement);
118:
119: visitCollectionMeasurement(measurement);
120: }
121:
122: public void visitNameListMeasurement(NameListMeasurement measurement) {
123: super .visitNameListMeasurement(measurement);
124:
125: visitCollectionMeasurement(measurement);
126: }
127:
128: public void visitSubMetricsAccumulatorMeasurement(
129: SubMetricsAccumulatorMeasurement measurement) {
130: super .visitSubMetricsAccumulatorMeasurement(measurement);
131:
132: visitCollectionMeasurement(measurement);
133: }
134:
135: protected void visitCollectionMeasurement(
136: CollectionMeasurement measurement) {
137: if (isExpandCollectionMeasurements()) {
138: raiseIndent();
139: for (String value : measurement.getValues()) {
140: indent().append(value).eol();
141: }
142: lowerIndent();
143: }
144: }
145:
146: protected void visitMeasurement(Measurement measurement) {
147: indent().append(measurement.getLongName()).append(" (").append(
148: measurement.getShortName()).append("): ").append(
149: valueFormat.format(measurement.getValue()));
150:
151: try {
152: RatioMeasurement ratio = (RatioMeasurement) currentMetrics
153: .getMeasurement(measurement.getShortName() + "R");
154: append(" (").append(ratioFormat.format(ratio.getValue()))
155: .append(")");
156: } catch (ClassCastException ex) {
157: // Do nothing, no ratio for this measurement
158: }
159:
160: eol();
161: }
162: }
|