1 package jdepend.framework; 2 3 import java.util.*; 4 5 11 12 public class JavaPackage { 13 14 private String name; 15 private int volatility; 16 private HashSet classes; 17 private List afferents; 18 private List efferents; 19 20 21 public JavaPackage(String name) { 22 this(name, 1); 23 } 24 25 public JavaPackage(String 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 getName() { 34 return name; 35 } 36 37 40 public int getVolatility() { 41 return volatility; 42 } 43 44 47 public void setVolatility(int v) { 48 volatility = v; 49 } 50 51 public boolean containsCycle() { 52 return collectCycle(new ArrayList()); 53 } 54 55 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 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 166 public void dependsUpon(JavaPackage imported) { 167 addEfferent(imported); 168 imported.addAfferent(this); 169 } 170 171 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 211 public int afferentCoupling() { 212 return afferents.size(); 213 } 214 215 218 public int efferentCoupling() { 219 return efferents.size(); 220 } 221 222 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 240 public float abstractness() { 241 242 if (getClassCount() > 0) { 243 return (float) getAbstractClassCount() / (float) getClassCount(); 244 } 245 246 return 0; 247 } 248 249 252 public float distance() { 253 float d = Math.abs(abstractness() + instability() - 1); 254 return d * volatility; 255 } 256 257 public boolean equals(Object 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 |