You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultFileLinesContext.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import org.sonar.api.batch.fs.InputFile;
  26. import org.sonar.api.batch.measure.MetricFinder;
  27. import org.sonar.api.batch.sensor.internal.SensorStorage;
  28. import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
  29. import org.sonar.api.measures.CoreMetrics;
  30. import org.sonar.api.measures.FileLinesContext;
  31. import org.sonar.api.utils.KeyValueFormat;
  32. import static java.util.stream.Collectors.toMap;
  33. import static org.sonar.api.utils.Preconditions.checkArgument;
  34. public class DefaultFileLinesContext implements FileLinesContext {
  35. private final InputFile inputFile;
  36. private final MetricFinder metricFinder;
  37. /**
  38. * metric key -> line -> value
  39. */
  40. private final Map<String, Map<Integer, Object>> map = new HashMap<>();
  41. private final SensorStorage sensorStorage;
  42. public DefaultFileLinesContext(SensorStorage sensorStorage, InputFile inputFile, MetricFinder metricFinder) {
  43. this.sensorStorage = sensorStorage;
  44. this.inputFile = inputFile;
  45. this.metricFinder = metricFinder;
  46. }
  47. @Override
  48. public void setIntValue(String metricKey, int line, int value) {
  49. checkNotNull(metricKey);
  50. checkLineRange(line);
  51. setValue(metricKey, line, value);
  52. }
  53. private void checkLineRange(int line) {
  54. checkArgument(line > 0, "Line number should be positive for file %s.", inputFile);
  55. checkArgument(line <= inputFile.lines(), "Line %s is out of range for file %s. File has %s lines.", line, inputFile, inputFile.lines());
  56. }
  57. @Override
  58. public void setStringValue(String metricKey, int line, String value) {
  59. checkNotNull(metricKey);
  60. checkLineRange(line);
  61. checkNotNull(value);
  62. setValue(metricKey, line, value);
  63. }
  64. private static void checkNotNull(Object object) {
  65. if (object == null) {
  66. throw new NullPointerException();
  67. }
  68. }
  69. private void setValue(String metricKey, int line, Object value) {
  70. map.computeIfAbsent(metricKey, k -> new HashMap<>())
  71. .put(line, value);
  72. }
  73. @Override
  74. public void save() {
  75. for (Map.Entry<String, Map<Integer, Object>> entry : map.entrySet()) {
  76. String metricKey = entry.getKey();
  77. Map<Integer, Object> lines = entry.getValue();
  78. if (shouldSave(lines)) {
  79. String data = KeyValueFormat.format(optimizeStorage(metricKey, lines));
  80. new DefaultMeasure<String>(sensorStorage)
  81. .on(inputFile)
  82. .forMetric(metricFinder.findByKey(metricKey))
  83. .withValue(data)
  84. .save();
  85. entry.setValue(Collections.unmodifiableMap(lines));
  86. }
  87. }
  88. }
  89. private static Map<Integer, Object> optimizeStorage(String metricKey, Map<Integer, Object> lines) {
  90. // SONAR-7464 Don't store 0 because this is default value anyway
  91. if (CoreMetrics.NCLOC_DATA_KEY.equals(metricKey) || CoreMetrics.EXECUTABLE_LINES_DATA_KEY.equals(metricKey)) {
  92. return lines.entrySet().stream()
  93. .filter(entry -> !entry.getValue().equals(0))
  94. .collect(toMap(Entry::getKey, Entry::getValue));
  95. }
  96. return lines;
  97. }
  98. /**
  99. * Checks that measure was not saved.
  100. *
  101. * @see #save()
  102. */
  103. private static boolean shouldSave(Map<Integer, Object> lines) {
  104. return lines instanceof HashMap;
  105. }
  106. @Override
  107. public String toString() {
  108. return this.getClass().getSimpleName() + "{" + map.toString() + "}";
  109. }
  110. }