aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/main/java/org/sonar/db/measure/MeasureDto.java
blob: aff4c4629a1297e207ad91ea4d294659255b19dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.db.measure;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.CheckForNull;
import org.apache.commons.codec.digest.MurmurHash3;

import static java.nio.charset.StandardCharsets.UTF_8;

public class MeasureDto {

  private static final Gson GSON = new Gson();

  private String componentUuid;
  private String branchUuid;
  // measures are kept sorted by metric key so that the value hash is consistent
  private Map<String, Object> metricValues = new TreeMap<>();
  private Long jsonValueHash;

  public MeasureDto() {
    // empty constructor
  }

  public String getComponentUuid() {
    return componentUuid;
  }

  public MeasureDto setComponentUuid(String s) {
    this.componentUuid = s;
    return this;
  }

  public String getBranchUuid() {
    return branchUuid;
  }

  public MeasureDto setBranchUuid(String s) {
    this.branchUuid = s;
    return this;
  }

  public Map<String, Object> getMetricValues() {
    return metricValues;
  }

  public MeasureDto addValue(String metricKey, Object value) {
    metricValues.put(metricKey, value);
    return this;
  }

  // used by MyBatis mapper
  public String getJsonValue() {
    return GSON.toJson(metricValues);
  }

  // used by MyBatis mapper
  public MeasureDto setJsonValue(String jsonValue) {
    metricValues = GSON.fromJson(jsonValue, new TypeToken<TreeMap<String, Object>>() {
    }.getType());
    return this;
  }

  public Long getJsonValueHash() {
    return jsonValueHash;
  }

  public long computeJsonValueHash() {
    jsonValueHash = MurmurHash3.hash128(getJsonValue().getBytes(UTF_8))[0];
    return jsonValueHash;
  }

  @CheckForNull
  public String getString(String metricKey) {
    Object value = metricValues.get(metricKey);
    if (value == null) {
      return null;
    }
    return String.valueOf(value);
  }

  @CheckForNull
  public Double getDouble(String metricKey) {
    Object value = metricValues.get(metricKey);
    if (value == null) {
      return null;
    }
    return Double.parseDouble(value.toString());
  }

  @CheckForNull
  public Integer getInt(String metricKey) {
    Object value = metricValues.get(metricKey);
    if (value == null) {
      return null;
    }
    return (int) Double.parseDouble(value.toString());
  }

  @CheckForNull
  public Long getLong(String metricKey) {
    Object value = metricValues.get(metricKey);
    if (value == null) {
      return null;
    }
    return (long) Double.parseDouble(value.toString());
  }

  @Override
  public String toString() {
    return "MeasureDto{" +
      "componentUuid='" + componentUuid + '\'' +
      ", branchUuid='" + branchUuid + '\'' +
      ", metricValues=" + metricValues +
      ", jsonValueHash=" + jsonValueHash +
      '}';
  }
}