-
Notifications
You must be signed in to change notification settings - Fork 0
/
MethodCounter.java
71 lines (57 loc) · 2.03 KB
/
MethodCounter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class MethodCounter {
static Map<String, Integer> counter = new HashMap<String, Integer>();
public static void increase(String method) {
if (counter.containsKey(method)) {
counter.put(method, counter.get(method) + 1);
} else {
counter.put(method, 1);
}
}
public static void report() {
reportCounter(counter);
}
public static void reportCounter(Map<String, Integer> counter) {
int maxLength = computeMaxLength(counter);
IntegerComparator comparator = new IntegerComparator(counter);
Map<String, Integer> sortedMethodToCounter = new TreeMap<String, Integer>(comparator);
sortedMethodToCounter.putAll(counter);
String tableFormat = "| %-" + (maxLength) + "s | %-11d |%n";
String repeated = String.format(String.format("%%0%dd", maxLength + 2), 0).replace("0","-");
String spaces = String.format(String.format("%%0%dd", maxLength > 17 ? maxLength - 15 : 0), 0).replace("0"," ");
System.out.format("+" + repeated + "+-------------+%n");
System.out.printf("| Method Signature" + spaces + "| # calls |%n");
System.out.format("+" + repeated + "+-------------+%n");
for (Map.Entry<String, Integer> method :
sortedMethodToCounter.entrySet()) {
System.out.format(tableFormat, method.getKey(), method.getValue());
}
System.out.format("+" + repeated + "+-------------+%n");
System.out.println("Total number of methods: " + counter.size());
}
private static int computeMaxLength(Map<String, Integer> counter) {
int maxLength = 0;
for (String sig : counter.keySet()) {
if (sig.length() > maxLength) {
maxLength = sig.length();
}
}
return maxLength;
}
static class IntegerComparator implements Comparator<String> {
private Map<String, Integer> base;
public IntegerComparator(Map<String, Integer> base) {
this.base = base;
}
@Override
public int compare(String o1, String o2) {
if (base.get(o1) >= base.get(o2)) {
return -1;
}
return 1;
}
}
}