KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > Metrics


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.metrics;
34
35 import java.util.*;
36
37 import org.apache.log4j.*;
38
39 public class Metrics {
40     public static final String JavaDoc PACKAGES = "P";
41     
42     public static final String JavaDoc CLASSES = "C";
43     public static final String JavaDoc PUBLIC_CLASSES = "PuC";
44     public static final String JavaDoc FINAL_CLASSES = "FC";
45     public static final String JavaDoc ABSTRACT_CLASSES = "AC";
46     public static final String JavaDoc SYNTHETIC_CLASSES = "SynthC";
47     public static final String JavaDoc INTERFACES = "I";
48     public static final String JavaDoc DEPRECATED_CLASSES = "DC";
49     public static final String JavaDoc STATIC_CLASSES = "SC";
50
51     public static final String JavaDoc PUBLIC_METHODS = "PuM";
52     public static final String JavaDoc PROTECTED_METHODS = "ProM";
53     public static final String JavaDoc PRIVATE_METHODS = "ProM";
54     public static final String JavaDoc PACKAGE_METHODS = "PaM";
55     public static final String JavaDoc FINAL_METHODS = "FM";
56     public static final String JavaDoc ABSTRACT_METHODS = "AM";
57     public static final String JavaDoc DEPRECATED_METHODS = "DM";
58     public static final String JavaDoc SYNTHETIC_METHODS = "SynthM";
59     public static final String JavaDoc STATIC_METHODS = "SM";
60     public static final String JavaDoc SYNCHRONIZED_METHODS = "SynchM";
61     public static final String JavaDoc NATIVE_METHODS = "NM";
62     public static final String JavaDoc TRIVIAL_METHODS = "TM";
63
64     public static final String JavaDoc ATTRIBUTES = "A";
65     public static final String JavaDoc PUBLIC_ATTRIBUTES = "PuA";
66     public static final String JavaDoc PROTECTED_ATTRIBUTES = "ProA";
67     public static final String JavaDoc PRIVATE_ATTRIBUTES = "PriA";
68     public static final String JavaDoc PACKAGE_ATTRIBUTES = "PaA";
69     public static final String JavaDoc FINAL_ATTRIBUTES = "FA";
70     public static final String JavaDoc DEPRECATED_ATTRIBUTES = "DA";
71     public static final String JavaDoc SYNTHETIC_ATTRIBUTES = "SynthA";
72     public static final String JavaDoc STATIC_ATTRIBUTES = "SA";
73     public static final String JavaDoc TRANSIENT_ATTRIBUTES = "TA";
74     public static final String JavaDoc VOLATILE_ATTRIBUTES = "VA";
75
76     public static final String JavaDoc INNER_CLASSES = "IC";
77     public static final String JavaDoc PUBLIC_INNER_CLASSES = "PuIC";
78     public static final String JavaDoc PROTECTED_INNER_CLASSES = "ProIC";
79     public static final String JavaDoc PRIVATE_INNER_CLASSES = "PriIC";
80     public static final String JavaDoc PACKAGE_INNER_CLASSES = "PaIC";
81     public static final String JavaDoc ABSTRACT_INNER_CLASSES = "AIC";
82     public static final String JavaDoc FINAL_INNER_CLASSES = "FIC";
83     public static final String JavaDoc STATIC_INNER_CLASSES = "SIC";
84
85     public static final String JavaDoc DEPTH_OF_INHERITANCE = "DOI";
86     public static final String JavaDoc SUBCLASSES = "SUB";
87     public static final String JavaDoc CLASS_SLOC = "class SLOC";
88
89     public static final String JavaDoc SLOC = "SLOC";
90     public static final String JavaDoc PARAMETERS = "PARAM";
91     public static final String JavaDoc LOCAL_VARIABLES = "LVAR";
92
93     public static final String JavaDoc INBOUND_INTRA_PACKAGE_DEPENDENCIES = "IIP";
94     public static final String JavaDoc INBOUND_EXTRA_PACKAGE_DEPENDENCIES = "IEP";
95     public static final String JavaDoc OUTBOUND_INTRA_PACKAGE_DEPENDENCIES = "OIP";
96     public static final String JavaDoc OUTBOUND_EXTRA_PACKAGE_DEPENDENCIES = "OEP";
97     
98     public static final String JavaDoc INBOUND_INTRA_CLASS_METHOD_DEPENDENCIES = "IICM";
99     public static final String JavaDoc INBOUND_INTRA_PACKAGE_METHOD_DEPENDENCIES = "IIPM";
100     public static final String JavaDoc INBOUND_EXTRA_PACKAGE_METHOD_DEPENDENCIES = "IEPM";
101     public static final String JavaDoc OUTBOUND_INTRA_CLASS_FEATURE_DEPENDENCIES = "OICF";
102     public static final String JavaDoc OUTBOUND_INTRA_PACKAGE_FEATURE_DEPENDENCIES = "OIPF";
103     public static final String JavaDoc OUTBOUND_INTRA_PACKAGE_CLASS_DEPENDENCIES = "OIPC";
104     public static final String JavaDoc OUTBOUND_EXTRA_PACKAGE_FEATURE_DEPENDENCIES = "OEPF";
105     public static final String JavaDoc OUTBOUND_EXTRA_PACKAGE_CLASS_DEPENDENCIES = "OEPC";
106
107     private static final Measurement NULL_MEASUREMENT = new NullMeasurement();
108     
109     private Metrics parent;
110     private String JavaDoc name;
111
112     private Map measurements = new TreeMap();
113     private Map submetrics = new TreeMap();
114
115     public Metrics(String JavaDoc name) {
116         this(null, name);
117     }
118     
119     /**
120      * @param name The name of the element being measured
121      * (e.g., class name, method name).
122      */

123     public Metrics(Metrics parent, String JavaDoc name) {
124         this.parent = parent;
125         this.name = name;
126
127         if (parent == null) {
128             Logger.getLogger(getClass()).debug("Created top-level metrics \"" + name + "\"");
129         } else {
130             Logger.getLogger(getClass()).debug("Created metrics \"" + name + "\" under \"" + parent.getName() + "\"");
131         }
132     }
133
134     public Metrics getParent() {
135         return parent;
136     }
137
138     /**
139      * @return The name of the element being measured
140      * (e.g., class name, method name).
141      */

142     public String JavaDoc getName() {
143         return name;
144     }
145
146     void track(Measurement measurement) {
147         track(measurement.getShortName(), measurement);
148     }
149     
150     void track(String JavaDoc name, Measurement measurement) {
151         measurements.put(name, measurement);
152     }
153
154     public void addToMeasurement(String JavaDoc name) {
155         addToMeasurement(name, 1);
156     }
157     
158     public void addToMeasurement(String JavaDoc name, int delta) {
159         getMeasurement(name).add(delta);
160     }
161     
162     public void addToMeasurement(String JavaDoc name, long delta) {
163         getMeasurement(name).add(delta);
164     }
165     
166     public void addToMeasurement(String JavaDoc name, float delta) {
167         getMeasurement(name).add(delta);
168     }
169     
170     public void addToMeasurement(String JavaDoc name, double delta) {
171         getMeasurement(name).add(delta);
172     }
173     
174     public void addToMeasurement(String JavaDoc name, Object JavaDoc delta) {
175         getMeasurement(name).add(delta);
176     }
177
178     public Measurement getMeasurement(String JavaDoc name) {
179         Measurement result = (Measurement) measurements.get(name);
180         
181         if (result == null) {
182             result = NULL_MEASUREMENT;
183             Logger.getLogger(getClass()).info("Null measurement \"" + name + "\" on \"" + getName() + "\"");
184         }
185
186         return result;
187     }
188
189     public boolean hasMeasurement(String JavaDoc name) {
190         return measurements.get(name) != null;
191     }
192     
193     public Collection getMeasurementNames() {
194         return Collections.unmodifiableCollection(new TreeSet(measurements.keySet()));
195     }
196     
197     public Metrics addSubMetrics(Metrics metrics) {
198         return (Metrics) submetrics.put(metrics.getName(), metrics);
199     }
200     
201     public Collection getSubMetrics() {
202         return Collections.unmodifiableCollection(submetrics.values());
203     }
204
205     public boolean isEmpty() {
206         boolean result = true;
207
208         Iterator i;
209
210         i = measurements.values().iterator();
211         while (result && i.hasNext()) {
212             Measurement measurement = (Measurement) i.next();
213             if (measurement.getDescriptor().isVisible()) {
214                 result = measurement.isEmpty();
215             }
216         }
217
218         i = submetrics.values().iterator();
219         while (result && i.hasNext()) {
220             result = ((Metrics) i.next()).isEmpty();
221         }
222         
223         return result;
224     }
225
226     public boolean isInRange() {
227         boolean result = true;
228
229         Iterator i = measurements.values().iterator();
230         while (result && i.hasNext()) {
231             result = ((Measurement) i.next()).isInRange();
232         }
233         
234         return result;
235     }
236     
237     public String JavaDoc toString() {
238         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
239
240         result.append(getClass().getName()).append(" ").append(getName()).append(" with [");
241
242         Iterator i = getMeasurementNames().iterator();
243         while(i.hasNext()) {
244             String JavaDoc name = (String JavaDoc) i.next();
245             Measurement measure = getMeasurement(name);
246
247             result.append("\"").append(name).append("\"(").append(measure.getClass().getName()).append(")");
248             if (i.hasNext()) {
249                 result.append(", ");
250             }
251         }
252
253         result.append("]");
254
255         return result.toString();
256     }
257 }
258
Popular Tags