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.

MetricImpl.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.metric;
  21. import java.util.Objects;
  22. import javax.annotation.CheckForNull;
  23. import javax.annotation.Nullable;
  24. import javax.annotation.concurrent.Immutable;
  25. import org.sonar.ce.task.projectanalysis.measure.Measure;
  26. import static com.google.common.base.MoreObjects.firstNonNull;
  27. import static com.google.common.base.MoreObjects.toStringHelper;
  28. import static com.google.common.base.Preconditions.checkArgument;
  29. import static com.google.common.base.Preconditions.checkNotNull;
  30. import static com.google.common.base.Preconditions.checkState;
  31. @Immutable
  32. public final class MetricImpl implements Metric {
  33. private final int id;
  34. private final String key;
  35. private final String name;
  36. private final MetricType type;
  37. private final Integer decimalScale;
  38. private final Double bestValue;
  39. private final boolean bestValueOptimized;
  40. private boolean deleteHistoricalData;
  41. public MetricImpl(int id, String key, String name, MetricType type) {
  42. this(id, key, name, type, null, null, false, false);
  43. }
  44. public MetricImpl(int id, String key, String name, MetricType type, @Nullable Integer decimalScale,
  45. @Nullable Double bestValue, boolean bestValueOptimized, boolean deleteHistoricalData) {
  46. checkArgument(!bestValueOptimized || bestValue != null, "A BestValue must be specified if Metric is bestValueOptimized");
  47. this.id = id;
  48. this.key = checkNotNull(key);
  49. this.name = checkNotNull(name);
  50. this.type = checkNotNull(type);
  51. if (type.getValueType() == Measure.ValueType.DOUBLE) {
  52. this.decimalScale = firstNonNull(decimalScale, org.sonar.api.measures.Metric.DEFAULT_DECIMAL_SCALE);
  53. } else {
  54. this.decimalScale = decimalScale;
  55. }
  56. this.bestValueOptimized = bestValueOptimized;
  57. this.bestValue = bestValue;
  58. this.deleteHistoricalData = deleteHistoricalData;
  59. }
  60. @Override
  61. public int getId() {
  62. return id;
  63. }
  64. @Override
  65. public String getKey() {
  66. return key;
  67. }
  68. @Override
  69. public String getName() {
  70. return name;
  71. }
  72. @Override
  73. public MetricType getType() {
  74. return type;
  75. }
  76. @Override
  77. public int getDecimalScale() {
  78. checkState(decimalScale != null, "Decimal scale is not defined on metric %s", key);
  79. return decimalScale;
  80. }
  81. @Override
  82. @CheckForNull
  83. public Double getBestValue() {
  84. return bestValue;
  85. }
  86. @Override
  87. public boolean isBestValueOptimized() {
  88. return bestValueOptimized;
  89. }
  90. @Override
  91. public boolean isDeleteHistoricalData() {
  92. return deleteHistoricalData;
  93. }
  94. @Override
  95. public boolean equals(@Nullable Object o) {
  96. if (this == o) {
  97. return true;
  98. }
  99. if (o == null || getClass() != o.getClass()) {
  100. return false;
  101. }
  102. MetricImpl metric = (MetricImpl) o;
  103. return Objects.equals(key, metric.key);
  104. }
  105. @Override
  106. public int hashCode() {
  107. return key.hashCode();
  108. }
  109. @Override
  110. public String toString() {
  111. return toStringHelper(this)
  112. .add("id", id)
  113. .add("key", key)
  114. .add("name", name)
  115. .add("type", type)
  116. .add("bestValue", bestValue)
  117. .add("bestValueOptimized", bestValueOptimized)
  118. .add("deleteHistoricalData", deleteHistoricalData)
  119. .toString();
  120. }
  121. }