1 23 package net.sourceforge.cobertura.reporting; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import net.sourceforge.cobertura.coveragedata.ClassData; 33 import net.sourceforge.cobertura.coveragedata.PackageData; 34 import net.sourceforge.cobertura.coveragedata.ProjectData; 35 import net.sourceforge.cobertura.coveragedata.SourceFileData; 36 import net.sourceforge.cobertura.javancss.Javancss; 37 import net.sourceforge.cobertura.util.FileFinder; 38 39 import org.apache.log4j.Logger; 40 41 42 52 public class ComplexityCalculator { 53 private static final Logger logger = Logger.getLogger(ComplexityCalculator.class); 54 55 public static final Complexity ZERO_COMPLEXITY = new Complexity(0,0); 56 57 private final FileFinder finder; 59 60 private Map sourceFileCNNCache = new HashMap (); 62 63 private Map packageCNNCache = new HashMap (); 65 66 73 public ComplexityCalculator( FileFinder finder) { 74 if( finder==null) 75 throw new NullPointerException (); 76 this.finder = finder; 77 } 78 79 90 private Complexity getAccumlatedCCNForSingleFile(File file) { 91 Javancss javancss = new Javancss(file.getAbsolutePath()); 92 93 List methodComplexities = javancss.getMethodComplexities(); 94 if (methodComplexities.size() <= 0) 95 return ZERO_COMPLEXITY; 96 97 int ccnAccumulator = 0; 98 Iterator iter = methodComplexities.iterator(); 99 while (iter.hasNext()) 100 { 101 ccnAccumulator += ((Integer )iter.next()).intValue(); 102 } 103 104 return new Complexity( ccnAccumulator, methodComplexities.size()); 105 } 106 107 108 117 public double getCCNForProject( ProjectData projectData) { 118 Complexity act = new Complexity(); 120 for( Iterator it = projectData.getPackages().iterator(); it.hasNext();) { 121 PackageData packageData = (PackageData)it.next(); 122 act.add( getCCNForPackageInternal( packageData)); 123 } 124 125 return act.averageCCN(); 127 } 128 129 137 public double getCCNForPackage(PackageData packageData) { 138 return getCCNForPackageInternal(packageData).averageCCN(); 139 } 140 141 private Complexity getCCNForPackageInternal(PackageData packageData) { 142 Complexity cachedCCN = (Complexity) packageCNNCache.get( packageData.getName()); 144 if( cachedCCN!=null) { 145 return cachedCCN; 146 } 147 148 Complexity act = new Complexity(); 150 for( Iterator it = packageData.getSourceFiles().iterator(); it.hasNext();) { 151 SourceFileData sourceData = (SourceFileData)it.next(); 152 act.add( getCCNForSourceFileNameInternal( sourceData.getName())); 153 } 154 155 packageCNNCache.put( packageData.getName(), act); 157 return act; 158 } 159 160 161 168 public double getCCNForSourceFile(SourceFileData sourceFile) { 169 return getCCNForSourceFileNameInternal( sourceFile.getName()).averageCCN(); 170 } 171 172 private Complexity getCCNForSourceFileNameInternal(String sourceFileName) { 173 Complexity cachedCCN = (Complexity) sourceFileCNNCache.get( sourceFileName); 175 if( cachedCCN!=null) { 176 return cachedCCN; 177 } 178 179 Complexity result = ZERO_COMPLEXITY; 181 try { 182 result = getAccumlatedCCNForSingleFile( finder.getFileForSource(sourceFileName)); 183 } catch( IOException ex) { 184 logger.info( "Cannot find source file during CCN computation, source=["+sourceFileName+"]"); 185 } 186 sourceFileCNNCache.put( sourceFileName, result); 187 return result; 188 } 189 190 197 public double getCCNForClass(ClassData classData) { 198 return getCCNForSourceFileNameInternal( classData.getSourceFileName()).averageCCN(); 199 } 200 201 202 206 private static class Complexity { 207 private double accumlatedCCN; 208 private int methodsNum; 209 public Complexity(double accumlatedCCN, int methodsNum) { 210 this.accumlatedCCN = accumlatedCCN; 211 this.methodsNum = methodsNum; 212 } 213 public Complexity() { 214 this(0,0); 215 } 216 public double averageCCN() { 217 if( methodsNum==0) { 218 return 0; 219 } 220 return accumlatedCCN/methodsNum; 221 } 222 public void add( Complexity second) { 223 accumlatedCCN += second.accumlatedCCN; 224 methodsNum += second.methodsNum; 225 } 226 } 227 } 228 | Popular Tags |