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
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.batch.scan2;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.sensor.SensorStorage;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.utils.KeyValueFormat;
import java.util.Map;
public class DefaultFileLinesContext implements FileLinesContext {
private final SensorStorage sensorStorage;
private final InputFile inputFile;
/**
* metric key -> line -> value
*/
private final Map<String, Map<Integer, Object>> map = Maps.newHashMap();
private MetricFinder metricFinder;
public DefaultFileLinesContext(MetricFinder metricFinder, SensorStorage sensorStorage, InputFile inputFile) {
this.metricFinder = metricFinder;
this.sensorStorage = sensorStorage;
this.inputFile = inputFile;
}
public void setIntValue(String metricKey, int line, int value) {
Preconditions.checkNotNull(metricKey);
Preconditions.checkArgument(line > 0);
setValue(metricKey, line, value);
}
public Integer getIntValue(String metricKey, int line) {
throw new UnsupportedOperationException();
}
public void setStringValue(String metricKey, int line, String value) {
Preconditions.checkNotNull(metricKey);
Preconditions.checkArgument(line > 0);
Preconditions.checkNotNull(value);
setValue(metricKey, line, value);
}
public String getStringValue(String metricKey, int line) {
throw new UnsupportedOperationException();
}
private Map<Integer, Object> getOrCreateLines(String metricKey) {
Map<Integer, Object> lines = map.get(metricKey);
if (lines == null) {
lines = Maps.newHashMap();
map.put(metricKey, lines);
}
return lines;
}
private void setValue(String metricKey, int line, Object value) {
getOrCreateLines(metricKey).put(line, value);
}
public void save() {
for (Map.Entry<String, Map<Integer, Object>> entry : map.entrySet()) {
String metricKey = entry.getKey();
org.sonar.api.batch.measure.Metric<String> metric = metricFinder.findByKey(metricKey);
if (metric == null) {
throw new IllegalStateException("Unable to find metric with key: " + metricKey);
}
Map<Integer, Object> lines = entry.getValue();
String data = KeyValueFormat.format(lines);
new DefaultMeasure<String>(sensorStorage)
.forMetric(metric)
.onFile(inputFile)
.withValue(data)
.save();
entry.setValue(ImmutableMap.copyOf(lines));
}
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("map", map)
.toString();
}
}
|