net.sourceforge.cobertura.reporting

Class ComplexityCalculator

public class ComplexityCalculator extends 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
static classComplexityCalculator.Complexity
Represents complexity of source file, package or project.
Field Summary
FileFinderfinder
static Loggerlogger
MappackageCNNCache
MapsourceFileCNNCache
static ComplexityCalculator.ComplexityZERO_COMPLEXITY
Constructor Summary
ComplexityCalculator(FileFinder finder)
Creates new calculator.
Method Summary
ComplexityCalculator.ComplexitygetAccumlatedCCNForSingleFile(File file)
Calculates the code complexity number for single source file.
doublegetCCNForClass(ClassData classData)
Computes CCN for source file the specified class belongs to.
doublegetCCNForPackage(PackageData packageData)
Computes CCN for all sources contained in the specified package.
ComplexityCalculator.ComplexitygetCCNForPackageInternal(PackageData packageData)
doublegetCCNForProject(ProjectData projectData)
Computes CCN for all sources contained in the project.
doublegetCCNForSourceFile(SourceFileData sourceFile)
Computes CCN for single source file.
ComplexityCalculator.ComplexitygetCCNForSourceFileNameInternal(String sourceFileName)

Field Detail

finder

private final FileFinder finder

logger

private static final Logger logger

packageCNNCache

private Map packageCNNCache

sourceFileCNNCache

private Map sourceFileCNNCache

ZERO_COMPLEXITY

public static final ComplexityCalculator.Complexity ZERO_COMPLEXITY

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: NullPointerException if finder is null

Method Detail

getAccumlatedCCNForSingleFile

private ComplexityCalculator.Complexity getAccumlatedCCNForSingleFile(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

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: NullPointerException if classData 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: NullPointerException if packageData is null

getCCNForPackageInternal

private ComplexityCalculator.Complexity getCCNForPackageInternal(PackageData packageData)

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: NullPointerException if projectData is null

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: NullPointerException if sourceFile is null

getCCNForSourceFileNameInternal

private ComplexityCalculator.Complexity getCCNForSourceFileNameInternal(String sourceFileName)