KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > metrics > CouplingMetric


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Johannes Bellert
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.com
21  * e-Mail: Johannes.Bellert@ercgroup.com
22  *
23  * * Created on Apr 11, 2004
24  *
25  */

26 package org.hammurapi.inspectors.metrics;
27
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 /**
32  * @author mucbj0
33  *
34  file:///D:/anwend/java/JDepend-2.6/ltcaemodel.xml.html
35
36     Instability
37         The ratio of efferent coupling (Ce) to total coupling (Ce / (Ce + Ca)). This metric is an indicator of the package's resilience to change.
38
39         The range for this metric is 0 to 1, with I=0 indicating a completely stable package and I=1 indicating a completely instable package.
40
41
42  **/

43 public class CouplingMetric {
44
45     public float instability = 0;
46
47         public int afferentMethodCounter = 0;
48         public int efferentMethodCounter = 0;
49
50     public int afferentMethodCounterProjectInternal = 0;
51     public int efferentMethodCounterProjectInternal = 0;
52
53         public int efferentMethodUnresolvedCounter = 0;
54         public int reflectiveMethodCounter = 0;
55
56         public int afferentVariableCounter = 0;
57         public int efferentVariableCounter = 0;
58
59         public int reflectiveVariableCounter = 0;
60
61     public CouplingMetric(){
62                 super();
63             }
64
65         public StringBuffer JavaDoc toXmlCore() {
66             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
67
68             sb.append("<Instability>");
69             sb.append(this.getInstability());
70             sb.append("</Instability>");
71
72             sb.append("<ReflectiveVariableCounter>");
73             sb.append(reflectiveVariableCounter);
74             sb.append("</ReflectiveVariableCounter>");
75
76             sb.append("<AfferentMethodCounterProjectInternal>");
77             sb.append(afferentMethodCounterProjectInternal);
78             sb.append("</AfferentMethodCounterProjectInternal>");
79             sb.append("<EfferentMethodCounterProjectInternal>");
80             sb.append(efferentMethodCounterProjectInternal);
81             sb.append("</EfferentMethodCounterProjectInternal>");
82
83             sb.append("<AfferentMethodCounter>");
84             sb.append(afferentMethodCounter);
85             sb.append("</AfferentMethodCounter>");
86             sb.append("<EfferentMethodCounter>");
87             sb.append(efferentMethodCounter);
88             sb.append("</EfferentMethodCounter>");
89
90
91             sb.append("<AfferentVariableCounter>");
92             sb.append(afferentVariableCounter);
93             sb.append("</AfferentVariableCounter>");
94             sb.append("<EfferentVariableCounter>");
95             sb.append(efferentVariableCounter);
96             sb.append("</EfferentVariableCounter>");
97
98             sb.append("<EfferentMethodUnresolvedCounter>");
99             sb.append(efferentMethodUnresolvedCounter);
100             sb.append("</EfferentMethodUnresolvedCounter>");
101
102
103             return sb;
104         }
105
106         
107         public Element JavaDoc toDom(Document JavaDoc document, Element JavaDoc root){
108             
109             Element JavaDoc ret=document.createElement("AfferentMethodCounter");
110             ret.setAttribute("number", String.valueOf( afferentVariableCounter ));
111             root.appendChild(ret);
112             
113             Element JavaDoc ret2=document.createElement("EfferentMethodCounter");
114             ret2.setAttribute("number", String.valueOf( efferentVariableCounter ));
115             root.appendChild(ret2);
116             
117             return root;
118         }
119
120     /**
121      * @return (Ce / (Ce + Ca))
122      */

123     public float getInstability() {
124         // (Ce / (Ce + Ca))
125
float instability = 0;
126         float denominator = (this.efferentMethodCounter + this.afferentMethodCounter );
127     // System.out.println (" " + denominator +" = " + this.efferentMethodCounter +" + " + this.afferentMethodCounter );
128
if( denominator != 0 )
129         { instability = this.efferentMethodCounter / denominator; }
130     // System.out.println (" instability " + instability );
131
return instability;
132     }
133
134 }
135
Popular Tags