Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
trustworthy-module-registry/src/metrics/CalculatorFactory.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
45 lines (42 sloc)
1.88 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MetricCalculator } from './MetricCalculator'; | |
import { LicenseCalculator } from './LicenseCalculator'; | |
import { BusFactorCalculator } from './BusFactorCalculator'; | |
import { CorrectnessCalculator } from './CorrectnessCalculator'; | |
import { RampUpCalculator } from './RampUpCalculator'; | |
import { ResponsiveMaintainerCalculator } from './ResponsiveMaintainerCalculator'; | |
// Define an enum to manage types of calculators, ensuring type safety and clarity | |
enum CalculatorTypes { | |
License = "License", | |
BusFactor = "BusFactor", | |
Correctness = "Correctness", | |
RampUp = "RampUp", | |
ResponsiveMaintainer = "ResponsiveMaintainer", | |
// Add more calculator types as our project expands | |
} | |
class CalculatorFactory { | |
/** | |
* Creates and returns an instance of a MetricCalculator subclass based on the type provided. | |
* @param type The type of calculator to instantiate. | |
* @param data The data required for the calculator. | |
* @returns An instance of a subclass of MetricCalculator or undefined if the type is not supported. | |
*/ | |
static createCalculator(type: CalculatorTypes, data: any): MetricCalculator | undefined { | |
switch (type) { | |
case CalculatorTypes.License: | |
return new LicenseCalculator(data); | |
case CalculatorTypes.BusFactor: | |
return new BusFactorCalculator(data); | |
case CalculatorTypes.Correctness: | |
return new CorrectnessCalculator(data); | |
case CalculatorTypes.RampUp: | |
return new RampUpCalculator(data); | |
case CalculatorTypes.ResponsiveMaintainer: | |
return new ResponsiveMaintainerCalculator(data); | |
// Implement cases for other types as needed | |
default: | |
console.error('Unknown Calculator Type'); | |
return undefined; | |
} | |
} | |
} | |
export { CalculatorFactory, CalculatorTypes }; |