KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jarg > Statistics


1 /* ====================================================================
2  * Copyright (c) 2002, Hidetoshi Ohuchi <hchacha@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the hchacha nor the names of its contributors
17  * may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  * ====================================================================
33  */

34 package jarg;
35
36 import java.util.*;
37
38 /**
39  * The statistics of classes in a new jar file.
40  *
41  * @version $Id: Statistics.java,v 1.3 2002/05/01 05:21:58 hchacha Exp $
42  * @author Hidetoshi Ohuchi &lt;hchacha@users.sourceforge.net&gt;
43  */

44 class Statistics {
45    Statistics() {
46    }
47
48    //------------------------------------------------------------------
49
private int oldClassFileSize;
50    void addOldClassFileSize(int sz) {
51       oldClassFileSize += sz;
52    }
53
54    private int oldClassCount;
55    void incOldClassCount() {
56       oldClassCount++;
57    }
58
59    private int oldCpEntrySize;
60    void addOldCpEntrySize(int sz) {
61       oldCpEntrySize += sz;
62    }
63
64    private int oldCpEntryCount;
65    void addOldCpEntryCount(int count) {
66       oldCpEntryCount += count;
67    }
68
69    private int oldFieldCount;
70    void incOldFieldCount() {
71       oldFieldCount++;
72    }
73
74    private int oldMethodCount;
75    void incOldMethodCount() {
76       oldMethodCount++;
77    }
78
79    private int oldByteCodeCount;
80    void addOldByteCodeCount(int count) {
81       oldByteCodeCount += count;
82    }
83
84    private int oldByteCodeSize;
85    void addOldByteCodeSize(int sz) {
86       oldByteCodeSize += sz;
87    }
88
89    //------------------------------------------------------------------
90
private int newClassFileSize;
91    void addNewClassFileSize(int sz) {
92       newClassFileSize += sz;
93    }
94
95    private int newClassCount;
96    void incNewClassCount() {
97       newClassCount++;
98    }
99
100    private int newCpEntrySize;
101    void addNewCpEntrySize(int sz) {
102       newCpEntrySize += sz;
103    }
104
105    private int newCpEntryCount;
106    void addNewCpEntryCount(int count) {
107       newCpEntryCount += count;
108    }
109
110    private int newFieldCount;
111    void incNewFieldCount() {
112       newFieldCount++;
113    }
114
115    private int newMethodCount;
116    void incNewMethodCount() {
117       newMethodCount++;
118    }
119
120    private int newByteCodeCount;
121    void addNewByteCodeCount(int count) {
122       newByteCodeCount += count;
123    }
124
125    private int newByteCodeSize;
126    void addNewByteCodeSize(int sz) {
127       newByteCodeSize += sz;
128    }
129
130    //------------------------------------------------------------------
131
void print() {
132       System.out.println();
133       System.out.println("[Statistics]");
134       System.out.println(" OLD : NEW : REDUCED : RATIO :");
135       printLine("class sz", oldClassFileSize, newClassFileSize);
136       printLine("class #", oldClassCount, newClassCount);
137       printLine("cp size ", oldCpEntrySize, newCpEntrySize);
138       printLine("cp entr#", oldCpEntryCount, newCpEntryCount);
139       printLine("field #", oldFieldCount, newFieldCount);
140       printLine("method #", oldMethodCount, newMethodCount);
141       printLine("bcode sz", oldByteCodeSize, newByteCodeSize);
142       printLine("bcode #", oldByteCodeCount, newByteCodeCount);
143    }
144
145    private void printLine(String JavaDoc label, int oldInt, int newInt) {
146       System.out.print(" ");
147       System.out.print(label);
148       System.out.print(" : ");
149       System.out.print(formatNum(oldInt));
150       System.out.print(" : ");
151       System.out.print(formatNum(newInt));
152       System.out.print(" : ");
153       System.out.print(formatNum(oldInt - newInt));
154       System.out.print(" : ");
155       System.out.print(formatNum(newInt*100/oldInt));
156       System.out.print("%");
157       System.out.println();
158    }
159
160    private static final int NUM_COL = 8;
161
162    private String JavaDoc formatNum(int val) {
163       String JavaDoc v = String.valueOf(val);
164       if (v.length() < NUM_COL) {
165          StringBuffer JavaDoc str = new StringBuffer JavaDoc();
166          for (int i=NUM_COL-v.length(); i>0; i--) {
167             str.append(' ');
168          }
169          str.append(v);
170          v = str.toString();
171       }
172       return v;
173    }
174 /*
175 [Statistics]
176                   OLD : NEW : REDUCED : RATIO :
177   class sz : 649496 : 625170 : 24326 : 96%
178   class # : 326 : 326 : 0 : 100%
179   cp size : 457894 : 438458 : 19436 : 95%
180   cp entr# : 31711 : 31071 : 640 : 97%
181   field # : 954 : 942 : 12 : 98%
182   method # : 2470 : 2466 : 4 : 99%
183   bcode sz : 110494 : 108976 : 1518 : 98%
184   bcode # : 53445 : 52986 : 459 : 99%
185 */

186 }
187
Popular Tags