1 package jdepend.framework; 2 3 import java.util.*; 4 5 import jdepend.framework.JavaPackage; 6 7 45 46 public class DependencyConstraint { 47 48 private HashMap packages; 49 50 public DependencyConstraint() { 51 packages = new HashMap(); 52 } 53 54 public JavaPackage addPackage(String packageName) { 55 JavaPackage jPackage = (JavaPackage) packages.get(packageName); 56 if (jPackage == null) { 57 jPackage = new JavaPackage(packageName); 58 addPackage(jPackage); 59 } 60 return jPackage; 61 } 62 63 public void addPackage(JavaPackage jPackage) { 64 if (!packages.containsValue(jPackage)) { 65 packages.put(jPackage.getName(), jPackage); 66 } 67 } 68 69 public Collection getPackages() { 70 return packages.values(); 71 } 72 73 79 public boolean match(Collection expectedPackages) { 80 81 if (packages.size() == expectedPackages.size()) { 82 83 for (Iterator i = expectedPackages.iterator(); i.hasNext();) { 84 Object next = i.next(); 85 if (next instanceof JavaPackage) { 86 JavaPackage nextPackage = (JavaPackage) next; 87 if (!matchPackage(nextPackage)) { 88 return false; 89 } 90 } else { 91 break; 92 } 93 94 return true; 95 } 96 } 97 98 return false; 99 } 100 101 private boolean matchPackage(JavaPackage expectedPackage) { 102 103 JavaPackage actualPackage = (JavaPackage) packages.get(expectedPackage 104 .getName()); 105 106 if (actualPackage != null) { 107 if (equalsDependencies(actualPackage, expectedPackage)) { 108 return true; 109 } 110 } 111 112 return false; 113 } 114 115 private boolean equalsDependencies(JavaPackage a, JavaPackage b) { 116 return equalsAfferents(a, b) && equalsEfferents(a, b); 117 } 118 119 private boolean equalsAfferents(JavaPackage a, JavaPackage b) { 120 121 if (a.equals(b)) { 122 123 Collection otherAfferents = b.getAfferents(); 124 125 if (a.getAfferents().size() == otherAfferents.size()) { 126 for (Iterator i = a.getAfferents().iterator(); i.hasNext();) { 127 JavaPackage afferent = (JavaPackage)i.next(); 128 if (!otherAfferents.contains(afferent)) { 129 return false; 130 } 131 } 132 133 return true; 134 } 135 } 136 137 return false; 138 } 139 140 private boolean equalsEfferents(JavaPackage a, JavaPackage b) { 141 142 if (a.equals(b)) { 143 144 Collection otherEfferents = b.getEfferents(); 145 146 if (a.getEfferents().size() == otherEfferents.size()) { 147 for (Iterator i = a.getEfferents().iterator(); i.hasNext();) { 148 JavaPackage efferent = (JavaPackage)i.next(); 149 if (!otherEfferents.contains(efferent)) { 150 return false; 151 } 152 } 153 154 return true; 155 } 156 } 157 158 return false; 159 } 160 } | Popular Tags |