-
Notifications
You must be signed in to change notification settings - Fork 38
/
Metric.java
256 lines (225 loc) · 8.38 KB
/
Metric.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package edu.hm.hafner.coverage;
import java.util.Locale;
import java.util.NavigableSet;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;
import com.google.errorprone.annotations.Immutable;
import edu.hm.hafner.coverage.Coverage.CoverageBuilder;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* A metric to identify the type of the results. The enum order will be used to sort the
* values for display purposes.
*
* @author Ullrich Hafner
*/
public enum Metric {
/** Nodes that can have children. */
CONTAINER(new LocOfChildrenEvaluator()),
MODULE(new LocOfChildrenEvaluator()),
PACKAGE(new LocOfChildrenEvaluator()),
FILE(new LocOfChildrenEvaluator()),
CLASS(new LocOfChildrenEvaluator()),
METHOD(new LocOfChildrenEvaluator()),
/** Coverage values without children. */
LINE(new ValuesAggregator()),
BRANCH(new ValuesAggregator()),
INSTRUCTION(new ValuesAggregator()),
/** Additional metrics without children. */
MUTATION(new ValuesAggregator()),
TEST_STRENGTH(new ValuesAggregator()),
COMPLEXITY(new ValuesAggregator(), MetricTendency.SMALLER_IS_BETTER),
COMPLEXITY_MAXIMUM(new MethodMaxComplexityFinder(), MetricTendency.SMALLER_IS_BETTER),
COMPLEXITY_DENSITY(new DensityEvaluator(), MetricTendency.SMALLER_IS_BETTER),
LOC(new LocEvaluator(), MetricTendency.SMALLER_IS_BETTER),
TESTS(new ValuesAggregator(), MetricTendency.LARGER_IS_BETTER);
/**
* Returns the metric that belongs to the specified tag.
*
* @param tag
* the tag
*
* @return the metric
* @see #toTagName()
*/
public static Metric fromTag(final String tag) {
return valueOf(tag.toUpperCase(Locale.ENGLISH).replaceAll("-", "_"));
}
@SuppressFBWarnings("SE_BAD_FIELD")
private final MetricEvaluator evaluator;
private final MetricTendency tendency;
Metric(final MetricEvaluator evaluator) {
this(evaluator, MetricTendency.LARGER_IS_BETTER);
}
Metric(final MetricEvaluator evaluator, final MetricTendency tendency) {
this.evaluator = evaluator;
this.tendency = tendency;
}
public MetricTendency getTendency() {
return tendency;
}
/**
* Returns if a given metric is a node metric.
*
* @return if the metric is a node metric
*/
public boolean isContainer() {
return evaluator.isAggregatingChildren();
}
/**
* Returns the name of the metric as a tag, containing only lowercase characters and dashes.
*
* @return the metric tag name
*/
public String toTagName() {
return name().toLowerCase(Locale.ENGLISH).replaceAll("_", "-");
}
/**
* Returns the aggregated value of this metric for the specified tree of nodes.
*
* @param node
* the root of the tree
*
* @return the aggregated value
*/
public Optional<Value> getValueFor(final Node node) {
return evaluator.compute(node, this);
}
public static NavigableSet<Metric> getCoverageMetrics() {
return new TreeSet<>(Set.of(
CONTAINER,
MODULE,
PACKAGE,
FILE,
CLASS,
METHOD,
LINE,
BRANCH,
INSTRUCTION
));
}
/**
* Metric tendency: some metrics are getting better when the value is getting larger, some other metrics are getting
* better when the value is getting smaller.
*/
public enum MetricTendency {
/** Larger values will indicate better results. */
LARGER_IS_BETTER,
/** Smaller values will indicate better results. */
SMALLER_IS_BETTER
}
@Immutable
private abstract static class MetricEvaluator {
abstract Optional<Value> compute(Node node, Metric searchMetric);
abstract boolean isAggregatingChildren();
}
private static class LocOfChildrenEvaluator extends MetricEvaluator {
@Override
public boolean isAggregatingChildren() {
return true;
}
protected Optional<Value> getMetricOf(final Node node, final Metric searchMetric) {
if (node.getMetric().equals(searchMetric)) {
var builder = new CoverageBuilder().withMetric(searchMetric);
if (hasCoverage(node)) {
builder.withCovered(1).withMissed(0);
}
else {
builder.withCovered(0).withMissed(1);
}
return Optional.of(builder.build());
}
return Optional.empty();
}
private boolean hasCoverage(final Node node) {
return hasCoverage(node, INSTRUCTION)
|| hasCoverage(node, LINE)
|| hasCoverage(node, BRANCH);
}
private boolean hasCoverage(final Node node, final Metric metric) {
return node.getValue(metric)
.filter(value -> ((Coverage) value).getCovered() > 0)
.isPresent();
}
@Override
final Optional<Value> compute(final Node node, final Metric searchMetric) {
Optional<Value> aggregatedChildrenValue = node.getChildren().stream()
.map(n -> n.getValue(searchMetric))
.flatMap(Optional::stream)
.reduce(Value::add);
Optional<Value> localMetricValue = getMetricOf(node, searchMetric);
return Stream.of(localMetricValue, aggregatedChildrenValue)
.flatMap(Optional::stream)
.reduce(Value::add);
}
}
private static class LocEvaluator extends MetricEvaluator {
@Override
public boolean isAggregatingChildren() {
return false;
}
@Override
Optional<Value> compute(final Node node, final Metric searchMetric) {
return LINE.getValueFor(node).map(leaf -> new LinesOfCode(((Coverage) leaf).getTotal()));
}
}
private static class DensityEvaluator extends MetricEvaluator {
@Override
public boolean isAggregatingChildren() {
return false;
}
@Override
Optional<Value> compute(final Node node, final Metric searchMetric) {
var locValue = LOC.getValueFor(node);
var complexityValue = COMPLEXITY.getValueFor(node);
if (locValue.isPresent() && complexityValue.isPresent()) {
var loc = (LinesOfCode) locValue.get();
if (loc.getValue() > 0) {
var complexity = (CyclomaticComplexity) complexityValue.get();
return Optional.of(new FractionValue(COMPLEXITY_DENSITY, complexity.getValue(), loc.getValue()));
}
}
return Optional.empty();
}
}
private static class MethodMaxComplexityFinder extends MetricEvaluator {
@Override
public boolean isAggregatingChildren() {
return false;
}
@Override
Optional<Value> compute(final Node node, final Metric searchMetric) {
if (node.getMetric() == METHOD) {
return COMPLEXITY.getValueFor(node)
.map(c -> new CyclomaticComplexity(((CyclomaticComplexity)c).getValue(),
COMPLEXITY_MAXIMUM));
}
return node.getChildren().stream()
.map(c -> compute(c, searchMetric))
.flatMap(Optional::stream)
.reduce(Value::max);
}
}
private static class ValuesAggregator extends MetricEvaluator {
@Override
public boolean isAggregatingChildren() {
return false;
}
@Override
final Optional<Value> compute(final Node node, final Metric searchMetric) {
Optional<Value> localMetricValue = node.getValues()
.stream()
.filter(leaf -> leaf.getMetric().equals(searchMetric))
.findAny();
if (localMetricValue.isPresent()) {
return localMetricValue;
}
// aggregate children
return node.getChildren().stream()
.map(n -> n.getValue(searchMetric))
.flatMap(Optional::stream)
.reduce(Value::add);
}
}
}