KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jdepend > framework > JavaPackage


1 package jdepend.framework;
2
3 import java.util.*;
4
5 /**
6  * The <code>JavaPackage</code> class represents a Java package.
7  *
8  * @author <b>Mike Clark</b>
9  * @author Clarkware Consulting, Inc.
10  */

11
12 public class JavaPackage {
13
14     private String JavaDoc name;
15     private int volatility;
16     private HashSet classes;
17     private List afferents;
18     private List efferents;
19
20
21     public JavaPackage(String JavaDoc name) {
22         this(name, 1);
23     }
24
25     public JavaPackage(String JavaDoc name, int volatility) {
26         this.name = name;
27         setVolatility(volatility);
28         classes = new HashSet();
29         afferents = new ArrayList();
30         efferents = new ArrayList();
31     }
32
33     public String JavaDoc getName() {
34         return name;
35     }
36
37     /**
38      * @return The package's volatility (0-1).
39      */

40     public int getVolatility() {
41         return volatility;
42     }
43
44     /**
45      * @param v Volatility (0-1).
46      */

47     public void setVolatility(int v) {
48         volatility = v;
49     }
50
51     public boolean containsCycle() {
52         return collectCycle(new ArrayList());
53     }
54
55     /**
56      * Collects the packages participating in the first package dependency cycle
57      * detected which originates from this package.
58      *
59      * @param list Collecting object to be populated with the list of
60      * JavaPackage instances in a cycle.
61      * @return <code>true</code> if a cycle exist; <code>false</code>
62      * otherwise.
63      */

64     public boolean collectCycle(List list) {
65
66         if (list.contains(this)) {
67             list.add(this);
68             return true;
69         }
70
71         list.add(this);
72
73         for (Iterator i = getEfferents().iterator(); i.hasNext();) {
74             JavaPackage efferent = (JavaPackage)i.next();
75             if (efferent.collectCycle(list)) {
76                 return true;
77             }
78         }
79
80         list.remove(this);
81
82         return false;
83     }
84
85     /**
86      * Collects all the packages participating in a package dependency cycle
87      * which originates from this package.
88      * <p>
89      * This is a more exhaustive search than that employed by
90      * <code>collectCycle</code>.
91      *
92      * @param list Collecting object to be populated with the list of
93      * JavaPackage instances in a cycle.
94      * @return <code>true</code> if a cycle exist; <code>false</code>
95      * otherwise.
96      */

97     public boolean collectAllCycles(List list) {
98
99         if (list.contains(this)) {
100             list.add(this);
101             return true;
102         }
103
104         list.add(this);
105
106         boolean containsCycle = false;
107         for (Iterator i = getEfferents().iterator(); i.hasNext();) {
108             JavaPackage efferent = (JavaPackage)i.next();
109             if (efferent.collectAllCycles(list)) {
110                 containsCycle = true;
111             }
112         }
113
114         if (containsCycle) {
115             return true;
116         }
117         
118         list.remove(this);
119         return false;
120     }
121
122     public void addClass(JavaClass clazz) {
123         classes.add(clazz);
124     }
125
126     public Collection getClasses() {
127         return classes;
128     }
129
130     public int getClassCount() {
131         return classes.size();
132     }
133
134     public int getAbstractClassCount() {
135         int count = 0;
136
137         for (Iterator i = classes.iterator(); i.hasNext();) {
138             JavaClass clazz = (JavaClass)i.next();
139             if (clazz.isAbstract()) {
140                 count++;
141             }
142         }
143
144         return count;
145     }
146
147     public int getConcreteClassCount() {
148         int count = 0;
149
150         for (Iterator i = classes.iterator(); i.hasNext();) {
151             JavaClass clazz = (JavaClass)i.next();
152             if (!clazz.isAbstract()) {
153                 count++;
154             }
155         }
156
157         return count;
158     }
159
160     /**
161      * Adds the specified Java package as an efferent of this package
162      * and adds this package as an afferent of it.
163      *
164      * @param imported Java package.
165      */

166     public void dependsUpon(JavaPackage imported) {
167         addEfferent(imported);
168         imported.addAfferent(this);
169     }
170
171     /**
172      * Adds the specified Java package as an afferent of this package.
173      *
174      * @param jPackage Java package.
175      */

176     public void addAfferent(JavaPackage jPackage) {
177         if (!jPackage.getName().equals(getName())) {
178             if (!afferents.contains(jPackage)) {
179                 afferents.add(jPackage);
180             }
181         }
182     }
183
184     public Collection getAfferents() {
185         return afferents;
186     }
187
188     public void setAfferents(Collection afferents) {
189         this.afferents = new ArrayList(afferents);
190     }
191
192     public void addEfferent(JavaPackage jPackage) {
193         if (!jPackage.getName().equals(getName())) {
194             if (!efferents.contains(jPackage)) {
195                 efferents.add(jPackage);
196             }
197         }
198     }
199
200     public Collection getEfferents() {
201         return efferents;
202     }
203
204     public void setEfferents(Collection efferents) {
205         this.efferents = new ArrayList(efferents);
206     }
207
208     /**
209      * @return The afferent coupling (Ca) of this package.
210      */

211     public int afferentCoupling() {
212         return afferents.size();
213     }
214
215     /**
216      * @return The efferent coupling (Ce) of this package.
217      */

218     public int efferentCoupling() {
219         return efferents.size();
220     }
221
222     /**
223      * @return Instability (0-1).
224      */

225     public float instability() {
226
227         float totalCoupling = (float) efferentCoupling()
228                 + (float) afferentCoupling();
229
230         if (totalCoupling > 0) {
231             return efferentCoupling()/totalCoupling;
232         }
233
234         return 0;
235     }
236
237     /**
238      * @return The package's abstractness (0-1).
239      */

240     public float abstractness() {
241
242         if (getClassCount() > 0) {
243             return (float) getAbstractClassCount() / (float) getClassCount();
244         }
245
246         return 0;
247     }
248
249     /**
250      * @return The package's distance from the main sequence (D).
251      */

252     public float distance() {
253         float d = Math.abs(abstractness() + instability() - 1);
254         return d * volatility;
255     }
256
257     public boolean equals(Object JavaDoc other) {
258         if (other instanceof JavaPackage) {
259             JavaPackage otherPackage = (JavaPackage) other;
260             return otherPackage.getName().equals(getName());
261         }
262         return false;
263     }
264
265     public int hashCode() {
266         return getName().hashCode();
267     }
268 }
269
Popular Tags