KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > internal > performance > db > Report


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test.internal.performance.db;
12
13 import java.io.PrintStream JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 public class Report {
17     
18     private static final String JavaDoc LEFT= "l"; //$NON-NLS-1$
19
private static final String JavaDoc RIGHT= "r"; //$NON-NLS-1$
20

21     private int fGap;
22     private int fColumn;
23     private int fRow;
24     private int fRows;
25     private HashMap JavaDoc fContent= new HashMap JavaDoc();
26     private HashMap JavaDoc fWidths= new HashMap JavaDoc();
27     private HashMap JavaDoc fAlignment= new HashMap JavaDoc();
28     
29     public Report(int gap) {
30         fGap= gap;
31     }
32     
33     public void addCell(String JavaDoc value) {
34         setCell(fColumn, fRow, value, LEFT);
35         fColumn++;
36     }
37     
38     public void addCellRight(String JavaDoc value) {
39         setCell(fColumn, fRow, value, RIGHT);
40         fColumn++;
41     }
42     
43     public void nextRow() {
44         fRow++;
45         fColumn= 0;
46     }
47     
48     private void setCell(int x, int y, String JavaDoc value, String JavaDoc align) {
49         fContent.put(x + "/" + y, value); //$NON-NLS-1$
50
fAlignment.put(x + "/" + y, align); //$NON-NLS-1$
51
Integer JavaDoc w= (Integer JavaDoc) fWidths.get(Integer.toString(x));
52         if (w == null)
53             w= new Integer JavaDoc(value.length());
54         else
55             w= new Integer JavaDoc(Math.max(w.intValue(), value.length()));
56         fWidths.put(Integer.toString(x), w);
57         fRows= Math.max(fRows, y+1);
58     }
59     
60     private String JavaDoc getCell(int x, int y) {
61         return (String JavaDoc) fContent.get(x + "/" + y); //$NON-NLS-1$
62
}
63     
64     public void print(PrintStream JavaDoc ps) {
65         int n= fWidths.size();
66         for (int y= 0; y < fRows; y++) {
67             for (int x= 0; x < n; x++) {
68                 Integer JavaDoc w= (Integer JavaDoc) fWidths.get(Integer.toString(x));
69                 int ww= w.intValue();
70                 String JavaDoc s= getCell(x, y);
71                 if (s == null)
72                     s= ""; //$NON-NLS-1$
73

74                 if (x > 0)
75                     for (int g= 0; g < fGap; g++)
76                         ps.print(' ');
77    
78                 int www= ww-s.length();
79                 String JavaDoc align= (String JavaDoc) fAlignment.get(x + "/" + y); //$NON-NLS-1$
80
if (LEFT.equalsIgnoreCase(align))
81                     ps.print(s);
82                 for (int l= 0; l < www; l++)
83                     ps.print(' ');
84                 if (RIGHT.equalsIgnoreCase(align))
85                     ps.print(s);
86             }
87             ps.println();
88         }
89     }
90 }
91
Popular Tags