net.sourceforge.cobertura.reporting
Class ComplexityCalculator

java.lang.Object
  extended bynet.sourceforge.cobertura.reporting.ComplexityCalculator

public class ComplexityCalculator
extends java.lang.Object

Allows complexity computing for source files, packages and a whole project. Average McCabe's number for methods contained in the specified entity is returned. This class depends on FileFinder which is used to map source file names to existing files.

One instance of this class should be used for the same set of source files - an object of this class can cache computed results.

Author:
Grzegorz Lukasik

Nested Class Summary
private static class ComplexityCalculator.Complexity
          Represents complexity of source file, package or project.
 
Field Summary
private  FileFinder finder
           
private static org.apache.log4j.Logger logger
           
private  java.util.Map packageCNNCache
           
private  java.util.Map sourceFileCNNCache
           
static ComplexityCalculator.Complexity ZERO_COMPLEXITY
           
 
Constructor Summary
ComplexityCalculator(FileFinder finder)
          Creates new calculator.
 
Method Summary
private  ComplexityCalculator.Complexity getAccumlatedCCNForSingleFile(java.io.File file)
          Calculates the code complexity number for single source file.
 double getCCNForClass(ClassData classData)
          Computes CCN for source file the specified class belongs to.
 double getCCNForPackage(PackageData packageData)
          Computes CCN for all sources contained in the specified package.
private  ComplexityCalculator.Complexity getCCNForPackageInternal(PackageData packageData)
           
 double getCCNForProject(ProjectData projectData)
          Computes CCN for all sources contained in the project.
 double getCCNForSourceFile(SourceFileData sourceFile)
          Computes CCN for single source file.
private  ComplexityCalculator.Complexity getCCNForSourceFileNameInternal(java.lang.String sourceFileName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static final org.apache.log4j.Logger logger

ZERO_COMPLEXITY

public static final ComplexityCalculator.Complexity ZERO_COMPLEXITY

finder

private final FileFinder finder

sourceFileCNNCache

private java.util.Map sourceFileCNNCache

packageCNNCache

private java.util.Map packageCNNCache
Constructor Detail

ComplexityCalculator

public ComplexityCalculator(FileFinder finder)
Creates new calculator. Passed FileFinder will be used to map source file names to existing files when needed.

Parameters:
finder - FileFinder that allows to find source files
Throws:
java.lang.NullPointerException - if finder is null
Method Detail

getAccumlatedCCNForSingleFile

private ComplexityCalculator.Complexity getAccumlatedCCNForSingleFile(java.io.File file)
Calculates the code complexity number for single source file. "CCN" stands for "code complexity number." This is sometimes referred to as McCabe's number. This method calculates the average cyclomatic code complexity of all methods of all classes in a given directory.

Parameters:
file - The source file for which you want to calculate the complexity
Returns:
average complexity for the specified source file

getCCNForProject

public double getCCNForProject(ProjectData projectData)
Computes CCN for all sources contained in the project. CCN for whole project is an average CCN for source files. All source files for which CCN cannot be computed are ignored.

Parameters:
projectData - project to compute CCN for
Returns:
CCN for project or 0 if no source files were found
Throws:
java.lang.NullPointerException - if projectData is null

getCCNForPackage

public double getCCNForPackage(PackageData packageData)
Computes CCN for all sources contained in the specified package. All source files that cannot be mapped to existing files are ignored.

Parameters:
packageData - package to compute CCN for
Returns:
CCN for the specified package or 0 if no source files were found
Throws:
java.lang.NullPointerException - if packageData is null

getCCNForPackageInternal

private ComplexityCalculator.Complexity getCCNForPackageInternal(PackageData packageData)

getCCNForSourceFile

public double getCCNForSourceFile(SourceFileData sourceFile)
Computes CCN for single source file.

Parameters:
sourceFile - source file to compute CCN for
Returns:
CCN for the specified source file, 0 if cannot map sourceFile to existing file
Throws:
java.lang.NullPointerException - if sourceFile is null

getCCNForSourceFileNameInternal

private ComplexityCalculator.Complexity getCCNForSourceFileNameInternal(java.lang.String sourceFileName)

getCCNForClass

public double getCCNForClass(ClassData classData)
Computes CCN for source file the specified class belongs to.

Parameters:
classData - package to compute CCN for
Returns:
CCN for source file the specified class belongs to
Throws:
java.lang.NullPointerException - if classData is null