1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
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 | 0 | private static final NumberFormat valueFormat = new DecimalFormat("#.##"); |
41 | 0 | private static final NumberFormat ratioFormat = new DecimalFormat("#%"); |
42 | |
|
43 | |
private List<MeasurementDescriptor> descriptors; |
44 | |
|
45 | |
private boolean expandCollectionMeasurements; |
46 | |
|
47 | 0 | private Metrics currentMetrics = null; |
48 | |
|
49 | |
public TextPrinter(PrintWriter out, List<MeasurementDescriptor> descriptors) { |
50 | 0 | super(out); |
51 | |
|
52 | 0 | this.descriptors = descriptors; |
53 | 0 | } |
54 | |
|
55 | |
public boolean isExpandCollectionMeasurements() { |
56 | 0 | return expandCollectionMeasurements; |
57 | |
} |
58 | |
|
59 | |
public void setExpandCollectionMeasurements(boolean expandCollectionMeasurements) { |
60 | 0 | this.expandCollectionMeasurements = expandCollectionMeasurements; |
61 | 0 | } |
62 | |
|
63 | |
public void visitMetrics(Metrics metrics) { |
64 | 0 | if (isShowEmptyMetrics() || isShowHiddenMeasurements() || !metrics.isEmpty()) { |
65 | 0 | currentMetrics = metrics; |
66 | |
|
67 | 0 | indent().append(metrics.getName()).eol(); |
68 | 0 | raiseIndent(); |
69 | |
|
70 | 0 | for (MeasurementDescriptor descriptor : descriptors) { |
71 | 0 | if (isShowHiddenMeasurements() || descriptor.isVisible()) { |
72 | 0 | metrics.getMeasurement(descriptor.getShortName()).accept(this); |
73 | |
} |
74 | |
} |
75 | |
|
76 | 0 | lowerIndent(); |
77 | |
|
78 | 0 | eol(); |
79 | |
} |
80 | 0 | } |
81 | |
|
82 | |
public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { |
83 | 0 | indent().append(measurement.getLongName()).append(" (").append(measurement.getShortName()).append("): ").append(valueFormat.format(measurement.getValue())); |
84 | |
|
85 | |
try { |
86 | 0 | RatioMeasurement ratio = (RatioMeasurement) currentMetrics.getMeasurement(measurement.getShortName() + "R"); |
87 | 0 | append(" (").append(ratioFormat.format(ratio.getValue())).append(")"); |
88 | 0 | } catch (ClassCastException ex) { |
89 | |
|
90 | 0 | } |
91 | |
|
92 | 0 | append(" ").append(measurement); |
93 | |
|
94 | 0 | eol(); |
95 | 0 | } |
96 | |
|
97 | |
public void visitRatioMeasurement(RatioMeasurement measurement) { |
98 | 0 | if (!measurement.getShortName().endsWith("R")) { |
99 | 0 | super.visitRatioMeasurement(measurement); |
100 | |
} |
101 | 0 | } |
102 | |
|
103 | |
public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { |
104 | 0 | super.visitContextAccumulatorMeasurement(measurement); |
105 | |
|
106 | 0 | visitCollectionMeasurement(measurement); |
107 | 0 | } |
108 | |
|
109 | |
public void visitNameListMeasurement(NameListMeasurement measurement) { |
110 | 0 | super.visitNameListMeasurement(measurement); |
111 | |
|
112 | 0 | visitCollectionMeasurement(measurement); |
113 | 0 | } |
114 | |
|
115 | |
public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { |
116 | 0 | super.visitSubMetricsAccumulatorMeasurement(measurement); |
117 | |
|
118 | 0 | visitCollectionMeasurement(measurement); |
119 | 0 | } |
120 | |
|
121 | |
protected void visitCollectionMeasurement(CollectionMeasurement measurement) { |
122 | 0 | if (isExpandCollectionMeasurements()) { |
123 | 0 | raiseIndent(); |
124 | 0 | for (String value : measurement.getValues()) { |
125 | 0 | indent().append(value).eol(); |
126 | |
} |
127 | 0 | lowerIndent(); |
128 | |
} |
129 | 0 | } |
130 | |
|
131 | |
protected void visitMeasurement(Measurement measurement) { |
132 | 0 | indent().append(measurement.getLongName()).append(" (").append(measurement.getShortName()).append("): ").append(valueFormat.format(measurement.getValue())); |
133 | |
|
134 | |
try { |
135 | 0 | RatioMeasurement ratio = (RatioMeasurement) currentMetrics.getMeasurement(measurement.getShortName() + "R"); |
136 | 0 | append(" (").append(ratioFormat.format(ratio.getValue())).append(")"); |
137 | 0 | } catch (ClassCastException ex) { |
138 | |
|
139 | 0 | } |
140 | |
|
141 | 0 | eol(); |
142 | 0 | } |
143 | |
} |