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.

MapBasedRawMeasureRepository.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.ce.task.projectanalysis.measure;
  21. import gnu.trove.map.hash.THashMap;
  22. import java.util.Collections;
  23. import java.util.Map;
  24. import java.util.Optional;
  25. import java.util.function.Function;
  26. import org.sonar.ce.task.projectanalysis.component.Component;
  27. import org.sonar.ce.task.projectanalysis.metric.Metric;
  28. import static com.google.common.base.Preconditions.checkArgument;
  29. import static java.lang.String.format;
  30. import static java.util.Objects.requireNonNull;
  31. /**
  32. * Map based implementation of MeasureRepository which supports only raw measures.
  33. * Intended to be used as a delegate of other MeasureRepository implementations (hence the final keyword).
  34. */
  35. public final class MapBasedRawMeasureRepository<T> implements MeasureRepository {
  36. private final Function<Component, T> componentToKey;
  37. private final Map<T, Map<String, Measure>> measures = new THashMap<>();
  38. public MapBasedRawMeasureRepository(Function<Component, T> componentToKey) {
  39. this.componentToKey = requireNonNull(componentToKey);
  40. }
  41. /**
  42. * @throws UnsupportedOperationException all the time, not supported
  43. */
  44. @Override
  45. public Optional<Measure> getBaseMeasure(Component component, Metric metric) {
  46. throw new UnsupportedOperationException("This implementation of MeasureRepository supports only raw measures");
  47. }
  48. @Override
  49. public Optional<Measure> getRawMeasure(final Component component, final Metric metric) {
  50. // fail fast
  51. requireNonNull(component);
  52. requireNonNull(metric);
  53. return find(component, metric);
  54. }
  55. @Override
  56. public void add(Component component, Metric metric, Measure measure) {
  57. requireNonNull(component);
  58. checkValueTypeConsistency(metric, measure);
  59. Optional<Measure> existingMeasure = find(component, metric);
  60. if (existingMeasure.isPresent()) {
  61. throw new UnsupportedOperationException(
  62. format(
  63. "a measure can be set only once for a specific Component (key=%s), Metric (key=%s). Use update method",
  64. component.getDbKey(),
  65. metric.getKey()));
  66. }
  67. add(component, metric, measure, OverridePolicy.OVERRIDE);
  68. }
  69. @Override
  70. public void update(Component component, Metric metric, Measure measure) {
  71. requireNonNull(component);
  72. checkValueTypeConsistency(metric, measure);
  73. Optional<Measure> existingMeasure = find(component, metric);
  74. if (!existingMeasure.isPresent()) {
  75. throw new UnsupportedOperationException(
  76. format(
  77. "a measure can be updated only if one already exists for a specific Component (key=%s), Metric (key=%s). Use add method",
  78. component.getDbKey(),
  79. metric.getKey()));
  80. }
  81. add(component, metric, measure, OverridePolicy.OVERRIDE);
  82. }
  83. private static void checkValueTypeConsistency(Metric metric, Measure measure) {
  84. checkArgument(
  85. measure.getValueType() == Measure.ValueType.NO_VALUE || measure.getValueType() == metric.getType().getValueType(),
  86. "Measure's ValueType (%s) is not consistent with the Metric's ValueType (%s)",
  87. measure.getValueType(), metric.getType().getValueType());
  88. }
  89. @Override
  90. public Map<String, Measure> getRawMeasures(Component component) {
  91. T componentKey = componentToKey.apply(component);
  92. return measures.getOrDefault(componentKey, Collections.emptyMap());
  93. }
  94. private Optional<Measure> find(Component component, Metric metric) {
  95. T componentKey = componentToKey.apply(component);
  96. Map<String, Measure> measuresPerMetric = measures.get(componentKey);
  97. if (measuresPerMetric == null) {
  98. return Optional.empty();
  99. }
  100. return Optional.ofNullable(measuresPerMetric.get(metric.getKey()));
  101. }
  102. public void add(Component component, Metric metric, Measure measure, OverridePolicy overridePolicy) {
  103. requireNonNull(component);
  104. requireNonNull(measure);
  105. requireNonNull(measure);
  106. requireNonNull(overridePolicy);
  107. T componentKey = componentToKey.apply(component);
  108. Map<String, Measure> measuresPerMetric = measures.computeIfAbsent(componentKey, key -> new THashMap<>());
  109. if (!measuresPerMetric.containsKey(metric.getKey()) || overridePolicy == OverridePolicy.OVERRIDE) {
  110. measuresPerMetric.put(metric.getKey(), measure);
  111. }
  112. }
  113. public enum OverridePolicy {
  114. OVERRIDE, DO_NOT_OVERRIDE
  115. }
  116. }