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.

IssueMetricFormula.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.server.measure.live;
  21. import java.util.Collection;
  22. import java.util.Optional;
  23. import java.util.function.BiConsumer;
  24. import org.sonar.api.measures.Metric;
  25. import org.sonar.db.component.ComponentDto;
  26. import org.sonar.server.measure.DebtRatingGrid;
  27. import org.sonar.server.measure.Rating;
  28. import static java.util.Collections.emptyList;
  29. class IssueMetricFormula {
  30. private final Metric metric;
  31. private final boolean onLeak;
  32. private final BiConsumer<Context, IssueCounter> formula;
  33. private final Collection<Metric> dependentMetrics;
  34. IssueMetricFormula(Metric metric, boolean onLeak, BiConsumer<Context, IssueCounter> formula) {
  35. this(metric, onLeak, formula, emptyList());
  36. }
  37. IssueMetricFormula(Metric metric, boolean onLeak, BiConsumer<Context, IssueCounter> formula, Collection<Metric> dependentMetrics) {
  38. this.metric = metric;
  39. this.onLeak = onLeak;
  40. this.formula = formula;
  41. this.dependentMetrics = dependentMetrics;
  42. }
  43. Metric getMetric() {
  44. return metric;
  45. }
  46. boolean isOnLeak() {
  47. return onLeak;
  48. }
  49. Collection<Metric> getDependentMetrics() {
  50. return dependentMetrics;
  51. }
  52. void compute(Context context, IssueCounter issues) {
  53. formula.accept(context, issues);
  54. }
  55. interface Context {
  56. ComponentDto getComponent();
  57. DebtRatingGrid getDebtRatingGrid();
  58. /**
  59. * Value that was just refreshed, otherwise value as computed
  60. * during last analysis.
  61. * The metric must be declared in the formula dependencies
  62. * (see {@link IssueMetricFormula#getDependentMetrics()}).
  63. */
  64. Optional<Double> getValue(Metric metric);
  65. Optional<Double> getLeakValue(Metric metric);
  66. void setValue(double value);
  67. void setValue(Rating value);
  68. void setLeakValue(double value);
  69. void setLeakValue(Rating value);
  70. }
  71. }