1313import org .hjug .git .GitLogReader ;
1414import org .hjug .git .RepositoryLogReader ;
1515import org .hjug .git .ScmLogInfo ;
16- import org .hjug .metrics .GodClass ;
17- import org .hjug .metrics .GodClassRanker ;
18- import org .hjug .metrics .PMDGodClassRuleRunner ;
16+ import org .hjug .metrics .*;
1917
2018@ Slf4j
2119public class CostBenefitCalculator {
2220
23- public List <RankedDisharmony > calculateCostBenefitValues (String repositoryPath ) {
21+ Map <String , ByteArrayOutputStream > filesToScan = new HashMap <>();
22+
23+ public List <RankedDisharmony > calculateGodClassCostBenefitValues (String repositoryPath ) {
2424
2525 RepositoryLogReader repositoryLogReader = new GitLogReader ();
2626 Repository repository = null ;
@@ -31,7 +31,7 @@ public List<RankedDisharmony> calculateCostBenefitValues(String repositoryPath)
3131 log .error ("Failure to access Git repository" , e );
3232 }
3333
34- List <GodClass > godClasses = getGodClasses (repositoryLogReader , repository );
34+ List <GodClass > godClasses = getGodClasses (getFilesToScan ( repositoryLogReader , repository ) );
3535
3636 List <ScmLogInfo > scmLogInfos = getRankedChangeProneness (repositoryLogReader , repository , godClasses );
3737
@@ -46,12 +46,12 @@ public List<RankedDisharmony> calculateCostBenefitValues(String repositoryPath)
4646 return rankedDisharmonies ;
4747 }
4848
49- List <ScmLogInfo > getRankedChangeProneness (
50- RepositoryLogReader repositoryLogReader , Repository repository , List <GodClass > godClasses ) {
49+ < T extends Disharmony > List <ScmLogInfo > getRankedChangeProneness (
50+ RepositoryLogReader repositoryLogReader , Repository repository , List <T > disharmonies ) {
5151 List <ScmLogInfo > scmLogInfos = new ArrayList <>();
5252 log .info ("Calculating Change Proneness for each God Class" );
53- for (GodClass godClass : godClasses ) {
54- String path = godClass .getFileName ();
53+ for (Disharmony disharmony : disharmonies ) {
54+ String path = disharmony .getFileName ();
5555 ScmLogInfo scmLogInfo = null ;
5656 try {
5757 scmLogInfo = repositoryLogReader .fileLog (repository , path );
@@ -67,17 +67,10 @@ List<ScmLogInfo> getRankedChangeProneness(
6767 return scmLogInfos ;
6868 }
6969
70- private List <GodClass > getGodClasses (RepositoryLogReader repositoryLogReader , Repository repository ) {
71- Map <String , ByteArrayOutputStream > filesToScan = new HashMap <>();
72- log .info ("Identifying God Classes from files in repository" );
73- try {
74- filesToScan = repositoryLogReader .listRepositoryContentsAtHEAD (repository );
75- } catch (IOException e ) {
76- log .error ("Error reading Git repository contents" , e );
77- }
78-
70+ private List <GodClass > getGodClasses (Map <String , ByteArrayOutputStream > filesToScan ) {
7971 PMDGodClassRuleRunner ruleRunner = new PMDGodClassRuleRunner ();
8072
73+ log .info ("Identifying God Classes from files in repository" );
8174 List <GodClass > godClasses = new ArrayList <>();
8275 for (Map .Entry <String , ByteArrayOutputStream > entry : filesToScan .entrySet ()) {
8376 String filePath = entry .getKey ();
@@ -92,4 +85,61 @@ private List<GodClass> getGodClasses(RepositoryLogReader repositoryLogReader, Re
9285 godClassRanker .rankGodClasses (godClasses );
9386 return godClasses ;
9487 }
88+
89+ public List <RankedDisharmony > calculateCBOCostBenefitValues (String repositoryPath ) {
90+
91+ RepositoryLogReader repositoryLogReader = new GitLogReader ();
92+ Repository repository = null ;
93+ log .info ("Initiating Cost Benefit calculation" );
94+ try {
95+ repository = repositoryLogReader .gitRepository (new File (repositoryPath ));
96+ } catch (IOException e ) {
97+ log .error ("Failure to access Git repository" , e );
98+ }
99+
100+ List <CBOClass > cboClasses = getCBOClasses (getFilesToScan (repositoryLogReader , repository ));
101+
102+ List <ScmLogInfo > scmLogInfos = getRankedChangeProneness (repositoryLogReader , repository , cboClasses );
103+
104+ Map <String , ScmLogInfo > rankedLogInfosByPath =
105+ scmLogInfos .stream ().collect (Collectors .toMap (ScmLogInfo ::getPath , logInfo -> logInfo , (a , b ) -> b ));
106+
107+ List <RankedDisharmony > rankedDisharmonies = new ArrayList <>();
108+ for (CBOClass cboClass : cboClasses ) {
109+ rankedDisharmonies .add (new RankedDisharmony (cboClass , rankedLogInfosByPath .get (cboClass .getFileName ())));
110+ }
111+
112+ return rankedDisharmonies ;
113+ }
114+
115+ private List <CBOClass > getCBOClasses (Map <String , ByteArrayOutputStream > filesToScan ) {
116+
117+ CBORuleRunner ruleRunner = new CBORuleRunner ();
118+
119+ log .info ("Identifying highly coupled classes from files in repository" );
120+ List <CBOClass > cboClasses = new ArrayList <>();
121+ for (Map .Entry <String , ByteArrayOutputStream > entry : filesToScan .entrySet ()) {
122+ String filePath = entry .getKey ();
123+ ByteArrayOutputStream value = entry .getValue ();
124+
125+ ByteArrayInputStream inputStream = new ByteArrayInputStream (value .toByteArray ());
126+ Optional <CBOClass > godClassOptional = ruleRunner .runCBOClassRule (filePath , inputStream );
127+ godClassOptional .ifPresent (cboClasses ::add );
128+ }
129+
130+ return cboClasses ;
131+ }
132+
133+ private Map <String , ByteArrayOutputStream > getFilesToScan (
134+ RepositoryLogReader repositoryLogReader , Repository repository ) {
135+
136+ try {
137+ if (filesToScan .isEmpty ()) {
138+ filesToScan = repositoryLogReader .listRepositoryContentsAtHEAD (repository );
139+ }
140+ } catch (IOException e ) {
141+ log .error ("Error reading Git repository contents" , e );
142+ }
143+ return filesToScan ;
144+ }
95145}
0 commit comments