13
13
import org .hjug .git .GitLogReader ;
14
14
import org .hjug .git .RepositoryLogReader ;
15
15
import 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 .*;
19
17
20
18
@ Slf4j
21
19
public class CostBenefitCalculator {
22
20
23
- public List <RankedDisharmony > calculateCostBenefitValues (String repositoryPath ) {
21
+ Map <String , ByteArrayOutputStream > filesToScan = new HashMap <>();
22
+
23
+ public List <RankedDisharmony > calculateGodClassCostBenefitValues (String repositoryPath ) {
24
24
25
25
RepositoryLogReader repositoryLogReader = new GitLogReader ();
26
26
Repository repository = null ;
@@ -31,7 +31,7 @@ public List<RankedDisharmony> calculateCostBenefitValues(String repositoryPath)
31
31
log .error ("Failure to access Git repository" , e );
32
32
}
33
33
34
- List <GodClass > godClasses = getGodClasses (repositoryLogReader , repository );
34
+ List <GodClass > godClasses = getGodClasses (getFilesToScan ( repositoryLogReader , repository ) );
35
35
36
36
List <ScmLogInfo > scmLogInfos = getRankedChangeProneness (repositoryLogReader , repository , godClasses );
37
37
@@ -46,12 +46,12 @@ public List<RankedDisharmony> calculateCostBenefitValues(String repositoryPath)
46
46
return rankedDisharmonies ;
47
47
}
48
48
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 ) {
51
51
List <ScmLogInfo > scmLogInfos = new ArrayList <>();
52
52
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 ();
55
55
ScmLogInfo scmLogInfo = null ;
56
56
try {
57
57
scmLogInfo = repositoryLogReader .fileLog (repository , path );
@@ -67,17 +67,10 @@ List<ScmLogInfo> getRankedChangeProneness(
67
67
return scmLogInfos ;
68
68
}
69
69
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 ) {
79
71
PMDGodClassRuleRunner ruleRunner = new PMDGodClassRuleRunner ();
80
72
73
+ log .info ("Identifying God Classes from files in repository" );
81
74
List <GodClass > godClasses = new ArrayList <>();
82
75
for (Map .Entry <String , ByteArrayOutputStream > entry : filesToScan .entrySet ()) {
83
76
String filePath = entry .getKey ();
@@ -92,4 +85,61 @@ private List<GodClass> getGodClasses(RepositoryLogReader repositoryLogReader, Re
92
85
godClassRanker .rankGodClasses (godClasses );
93
86
return godClasses ;
94
87
}
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
+ }
95
145
}
0 commit comments