3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.metric;
22 import com.google.common.base.MoreObjects;
23 import java.util.Objects;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import javax.annotation.concurrent.Immutable;
27 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
29 import static com.google.common.base.Preconditions.checkArgument;
30 import static com.google.common.base.Preconditions.checkNotNull;
31 import static com.google.common.base.Preconditions.checkState;
34 public final class MetricImpl implements Metric {
37 private final String key;
38 private final String name;
39 private final MetricType type;
40 private final Integer decimalScale;
41 private final Double bestValue;
42 private final boolean bestValueOptimized;
44 public MetricImpl(int id, String key, String name, MetricType type) {
45 this(id, key, name, type, null, null, false);
48 public MetricImpl(int id, String key, String name, MetricType type, @Nullable Integer decimalScale,
49 @Nullable Double bestValue, boolean bestValueOptimized) {
50 checkArgument(!bestValueOptimized || bestValue != null, "A BestValue must be specified if Metric is bestValueOptimized");
52 this.key = checkNotNull(key);
53 this.name = checkNotNull(name);
54 this.type = checkNotNull(type);
55 if (type.getValueType() == Measure.ValueType.DOUBLE) {
56 this.decimalScale = MoreObjects.firstNonNull(decimalScale, org.sonar.api.measures.Metric.DEFAULT_DECIMAL_SCALE);
58 this.decimalScale = decimalScale;
60 this.bestValueOptimized = bestValueOptimized;
61 this.bestValue = bestValue;
70 public String getKey() {
75 public String getName() {
80 public MetricType getType() {
85 public int getDecimalScale() {
86 checkState(decimalScale != null, "Decimal scale is not defined on metric %s", key);
92 public Double getBestValue() {
97 public boolean isBestValueOptimized() {
98 return bestValueOptimized;
102 public boolean equals(@Nullable Object o) {
106 if (o == null || getClass() != o.getClass()) {
109 MetricImpl metric = (MetricImpl) o;
110 return Objects.equals(key, metric.key);
114 public int hashCode() {
115 return key.hashCode();
119 public String toString() {
120 return com.google.common.base.MoreObjects.toStringHelper(this)
125 .add("bestValue", bestValue)
126 .add("bestValueOptimized", bestValueOptimized)