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 abstract class Printer implements MeasurementVisitor {
039: private PrintWriter out;
040:
041: private boolean showEmptyMetrics = false;
042: private boolean showHiddenMeasurements = false;
043: private String indentText = " ";
044: private int indentLevel = 0;
045:
046: public Printer(PrintWriter out) {
047: this .out = out;
048: }
049:
050: public String getIndentText() {
051: return indentText;
052: }
053:
054: public void setIndentText(String indentText) {
055: this .indentText = indentText;
056: }
057:
058: public boolean isShowEmptyMetrics() {
059: return showEmptyMetrics;
060: }
061:
062: public void setShowEmptyMetrics(boolean showEmptyMetrics) {
063: this .showEmptyMetrics = showEmptyMetrics;
064: }
065:
066: public boolean isShowHiddenMeasurements() {
067: return showHiddenMeasurements;
068: }
069:
070: public void setShowHiddenMeasurements(boolean showHiddenMeasurements) {
071: this .showHiddenMeasurements = showHiddenMeasurements;
072: }
073:
074: protected Printer append(boolean b) {
075: out.print(b);
076: return this ;
077: }
078:
079: protected Printer append(char c) {
080: out.print(c);
081: return this ;
082: }
083:
084: protected Printer append(char[] s) {
085: out.print(s);
086: return this ;
087: }
088:
089: protected Printer append(double d) {
090: out.print(d);
091: return this ;
092: }
093:
094: protected Printer append(float f) {
095: out.print(f);
096: return this ;
097: }
098:
099: protected Printer append(int i) {
100: out.print(i);
101: return this ;
102: }
103:
104: protected Printer append(long l) {
105: out.print(l);
106: return this ;
107: }
108:
109: protected Printer append(Object obj) {
110: out.print(obj);
111: return this ;
112: }
113:
114: protected Printer append(String s) {
115: out.print(s);
116: return this ;
117: }
118:
119: protected Printer indent() {
120: for (int i = 0; i < indentLevel; i++) {
121: append(getIndentText());
122: }
123:
124: return this ;
125: }
126:
127: protected Printer eol() {
128: out.println();
129: return this ;
130: }
131:
132: protected void raiseIndent() {
133: indentLevel++;
134: }
135:
136: protected void lowerIndent() {
137: indentLevel--;
138: }
139:
140: public void visitMetrics(Collection<Metrics> metrics) {
141: for (Metrics metric : metrics) {
142: visitMetrics(metric);
143: }
144: }
145:
146: public abstract void visitMetrics(Metrics metrics);
147:
148: public void visitRatioMeasurement(RatioMeasurement measurement) {
149: visitMeasurement(measurement);
150: }
151:
152: public void visitNbSubMetricsMeasurement(
153: NbSubMetricsMeasurement measurement) {
154: visitMeasurement(measurement);
155: }
156:
157: public void visitCounterMeasurement(CounterMeasurement measurement) {
158: visitMeasurement(measurement);
159: }
160:
161: public void visitContextAccumulatorMeasurement(
162: ContextAccumulatorMeasurement measurement) {
163: visitMeasurement(measurement);
164: }
165:
166: public void visitNameListMeasurement(NameListMeasurement measurement) {
167: visitMeasurement(measurement);
168: }
169:
170: public void visitSubMetricsAccumulatorMeasurement(
171: SubMetricsAccumulatorMeasurement measurement) {
172: visitMeasurement(measurement);
173: }
174:
175: public void visitSumMeasurement(SumMeasurement measurement) {
176: visitMeasurement(measurement);
177: }
178:
179: protected abstract void visitMeasurement(Measurement measurement);
180: }
|