Skip to content

Commit b791b4a

Browse files
FEAT: Adding the MethodCounter Util Class for the MethodInvocationCounter Metric
1 parent 9fb521e commit b791b4a

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.github.mauricioaniche.ck.util;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
6+
public class MethodCounter {
7+
public class MethodInformation {
8+
private String parentName;
9+
private Map<String, Integer> methodInvocations = new HashMap<>();
10+
11+
public MethodInformation(String parentName) {
12+
this.parentName = parentName;
13+
}
14+
15+
public String getParentName() {
16+
return parentName;
17+
}
18+
19+
public Map<String, Integer> getMethodInvocations() {
20+
return methodInvocations;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "MethodInformation{" +
26+
"parentName='" + parentName + '\'' +
27+
", methodInvocation=" + methodInvocations +
28+
'}';
29+
}
30+
31+
public String toFormattedString() {
32+
return parentName + "[ " + formatMethods(sortMethods(methodInvocations)) + " ] ";
33+
}
34+
35+
private Map<String, Integer> sortMethods(Map<String, Integer> methodInvocation) {
36+
return methodInvocation.entrySet().stream()
37+
.sorted((Map.Entry.<String, Integer>comparingByValue().reversed()))
38+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
39+
}
40+
41+
private String formatMethods(Map<String, Integer> methodInvocation) {
42+
return methodInvocation.entrySet().stream()
43+
.map(entry -> entry.getKey() + "()" + ":" + entry.getValue())
44+
.collect(Collectors.joining(" "));
45+
}
46+
47+
}
48+
49+
public static String formatResult(List<MethodInformation> methodInformations) {
50+
return methodInformations.stream().map(MethodInformation::toFormattedString).collect(Collectors.joining());
51+
}
52+
public static List<MethodInformation> count(String methodList) {
53+
List<MethodInformation> methodInformationList = new ArrayList<>();
54+
55+
String[] methodNames = methodList.split(";");
56+
for (String name : methodNames) {
57+
String[] parts = name.split("/");
58+
String methodName = parts[0];
59+
String parentName = parts[1];
60+
61+
MethodInformation methodInformation = new MethodCounter().new MethodInformation(parentName);
62+
if (methodInformationList.contains(methodInformation)) {
63+
methodInformation = methodInformationList.get(methodInformationList.indexOf(methodInformation));
64+
} else {
65+
methodInformationList.add(methodInformation);
66+
}
67+
68+
if (methodInformation.getMethodInvocations().containsKey(methodName)) {
69+
methodInformation.getMethodInvocations().put(methodName, methodInformation.getMethodInvocations().get(methodName) + 1);
70+
} else {
71+
methodInformation.getMethodInvocations().put(methodName, 1);
72+
}
73+
}
74+
75+
return methodInformationList;
76+
}
77+
}

0 commit comments

Comments
 (0)