1 /**
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 package cppncss;
30
31 import java.io.PrintStream;
32 import java.util.List;
33 import java.util.Locale;
34
35 /**
36 * Implements a text result output.
37 *
38 * @author Mathieu Champlon
39 */
40 public final class AsciiResultOutput extends AbstractResultOutput
41 {
42 private final PrintStream stream;
43 private boolean isFirstSum = true;
44
45 /**
46 * Create an ascii result output to a given stream.
47 *
48 * @param stream the output print stream
49 */
50 public AsciiResultOutput( final PrintStream stream )
51 {
52 this.stream = stream;
53 }
54
55 /**
56 * {@inheritDoc}
57 */
58 protected void printHeaders( final String type, final List<String> labels )
59 {
60 stream.println();
61 stream.print( "Nr. " );
62 for( String label : labels )
63 if( !label.startsWith( type ) )
64 stream.print( label + " " );
65 stream.println( type );
66 }
67
68 /**
69 * {@inheritDoc}
70 */
71 protected void printIndex( final String item, final int index )
72 {
73 stream.format( "%3d", index );
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 protected void printMeasurement( final String label, final int count )
80 {
81 stream.format( " %" + label.length() + "d", count );
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 protected void printItem( final String item )
88 {
89 stream.format( " %s", item );
90 stream.println();
91 }
92
93 /**
94 * {@inheritDoc}
95 */
96 public void notify( final String type, final String label, final float average )
97 {
98 if( !label.startsWith( type ) )
99 {
100 stream.format( Locale.US, "Average %s %s: %.2f", type, label, average );
101 stream.println();
102 }
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 public void notify( final String type, final String label, final long sum )
109 {
110 if( isFirstSum )
111 {
112 isFirstSum = false;
113 stream.println();
114 }
115 stream.println( type + " " + label + ": " + sum );
116 }
117
118 /**
119 * {@inheritDoc}
120 */
121 public void flush()
122 {
123 }
124 }