KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > MetricsGatherer


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
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 Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * 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 FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.util.*;
36
37 public class MetricsGatherer extends VisitorBase {
38     private Collection packages = new LinkedList();
39     private Collection classes = new LinkedList();
40     private Collection features = new LinkedList();
41
42     private long nbOutbound = 0;
43     private long nbInbound = 0;
44     private long nbOutboundPackages = 0;
45     private long nbInboundPackages = 0;
46     private long nbOutboundClasses = 0;
47     private long nbInboundClasses = 0;
48     private long nbOutboundFeatures = 0;
49     private long nbInboundFeatures = 0;
50
51     private Map chartData = new TreeMap();
52     private int chartMaximum = 0;
53     public static final int CHART_INDEX = 0;
54     public static final int CLASSES_PER_PACKAGE = 1;
55     public static final int FEATURES_PER_CLASS = 2;
56     public static final int INBOUNDS_PER_PACKAGE = 3;
57     public static final int OUTBOUNDS_PER_PACKAGE = 4;
58     public static final int INBOUNDS_PER_CLASS = 5;
59     public static final int OUTBOUNDS_PER_CLASS = 6;
60     public static final int INBOUNDS_PER_FEATURE = 7;
61     public static final int OUTBOUNDS_PER_FEATURE = 8;
62     public static final int NB_CHARTS = 9;
63
64     private static final String JavaDoc[] CHART_NAMES = {"n",
65                                                  "Classes per Package",
66                                                  "Feafures per Class",
67                                                  "Inbounds per Package",
68                                                  "Outbounds per Package",
69                                                  "Inbounds per Class",
70                                                  "Outbounds per Class",
71                                                  "Inbounds per Feature",
72                                                  "Outbounds per Feature"};
73
74     public static int getNbCharts() {
75         return NB_CHARTS;
76     }
77     
78     public static String JavaDoc getChartName(int i) {
79         return CHART_NAMES[i];
80     }
81     
82     public MetricsGatherer() {
83         super();
84     }
85
86     public MetricsGatherer(TraversalStrategy strategy) {
87         super(strategy);
88     }
89
90     public long[] getChartData(int i) {
91         long[] result = null;
92
93         Integer JavaDoc key = new Integer JavaDoc(i);
94         result = (long[]) chartData.get(key);
95
96         if (result == null) {
97             result = new long[NB_CHARTS];
98             result[CHART_INDEX] = i;
99             chartData.put(key, result);
100
101             if (chartMaximum < i) {
102                 chartMaximum = i;
103             }
104         }
105
106         return result;
107     }
108
109     public int getChartMaximum() {
110         return chartMaximum;
111     }
112     
113     public Collection getPackages() {
114         return packages;
115     }
116
117     public Collection getClasses() {
118         return classes;
119     }
120
121     public Collection getFeatures() {
122         return features;
123     }
124
125     public long getNbOutbound() {
126         return nbOutbound;
127     }
128
129     public long getNbInbound() {
130         return nbInbound;
131     }
132
133     public long getNbOutboundPackages() {
134         return nbOutboundPackages;
135     }
136
137     public long getNbInboundPackages() {
138         return nbInboundPackages;
139     }
140
141     public long getNbOutboundClasses() {
142         return nbOutboundClasses;
143     }
144
145     public long getNbInboundClasses() {
146         return nbInboundClasses;
147     }
148
149     public long getNbOutboundFeatures() {
150         return nbOutboundFeatures;
151     }
152
153     public long getNbInboundFeatures() {
154         return nbInboundFeatures;
155     }
156     
157     public void preprocessPackageNode(PackageNode node) {
158         super.preprocessPackageNode(node);
159
160         packages.add(node);
161         
162         getChartData(node.getClasses().size())[CLASSES_PER_PACKAGE]++;
163         getChartData(node.getInboundDependencies().size())[INBOUNDS_PER_PACKAGE]++;
164         getChartData(node.getOutboundDependencies().size())[OUTBOUNDS_PER_PACKAGE]++;
165     }
166
167     /**
168      * PackageNode --> CurrentNode()
169      */

170     public void visitInboundPackageNode(PackageNode node) {
171         if (getStrategy().isInFilter(node)) {
172             nbInbound++;
173             nbOutboundPackages++;
174         }
175     }
176
177     /**
178      * CurrentNode() --> PackageNode
179      */

180     public void visitOutboundPackageNode(PackageNode node) {
181         if (getStrategy().isInFilter(node)) {
182             nbOutbound++;
183             nbInboundPackages++;
184         }
185     }
186
187     public void preprocessClassNode(ClassNode node) {
188         super.preprocessClassNode(node);
189
190         classes.add(node);
191         
192         getChartData(node.getFeatures().size())[FEATURES_PER_CLASS]++;
193         getChartData(node.getInboundDependencies().size())[INBOUNDS_PER_CLASS]++;
194         getChartData(node.getOutboundDependencies().size())[OUTBOUNDS_PER_CLASS]++;
195     }
196
197     /**
198      * ClassNode --> CurrentNode()
199      */

200     public void visitInboundClassNode(ClassNode node) {
201         if (getStrategy().isInFilter(node)) {
202             nbInbound++;
203             nbOutboundClasses++;
204         }
205     }
206
207     /**
208      * CurrentNode() --> ClassNode
209      */

210     public void visitOutboundClassNode(ClassNode node) {
211         if (getStrategy().isInFilter(node)) {
212             nbOutbound++;
213             nbInboundClasses++;
214         }
215     }
216
217     public void preprocessFeatureNode(FeatureNode node) {
218         super.preprocessFeatureNode(node);
219
220         features.add(node);
221         
222         getChartData(node.getInboundDependencies().size())[INBOUNDS_PER_FEATURE]++;
223         getChartData(node.getOutboundDependencies().size())[OUTBOUNDS_PER_FEATURE]++;
224     }
225
226     /**
227      * FeatureNode --> CurrentNode()
228      */

229     public void visitInboundFeatureNode(FeatureNode node) {
230         if (getStrategy().isInFilter(node)) {
231             nbInbound++;
232             nbOutboundFeatures++;
233         }
234     }
235
236     /**
237      * CurrentNode() --> FeatureNode
238      */

239     public void visitOutboundFeatureNode(FeatureNode node) {
240         if (getStrategy().isInFilter(node)) {
241             nbOutbound++;
242             nbInboundFeatures++;
243         }
244     }
245 }
246
Popular Tags