net.sourceforge.cobertura.reporting

Class 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

static ComplexityCalculator.Complexity
ZERO_COMPLEXITY
private FileFinder
finder
private static Logger
logger
private Map
packageCNNCache
private Map
sourceFileCNNCache

Constructor Summary

ComplexityCalculator(FileFinder finder)
Creates new calculator.

Method Summary

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

Field Details

ZERO_COMPLEXITY

public static final ComplexityCalculator.Complexity ZERO_COMPLEXITY

finder

private final FileFinder finder

logger

private static final Logger logger

packageCNNCache

private Map packageCNNCache

sourceFileCNNCache

private Map sourceFileCNNCache

Constructor Details

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

Method Details

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

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

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

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

getCCNForSourceFileNameInternal

private ComplexityCalculator.Complexity getCCNForSourceFileNameInternal(String sourceFileName)