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.

ImpactMeasureBuilder.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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;
  21. import com.google.gson.Gson;
  22. import com.google.gson.GsonBuilder;
  23. import com.google.gson.reflect.TypeToken;
  24. import java.lang.reflect.Type;
  25. import java.util.Arrays;
  26. import java.util.LinkedHashMap;
  27. import java.util.Map;
  28. import javax.annotation.CheckForNull;
  29. import org.sonar.api.issue.impact.Severity;
  30. import static org.sonar.api.utils.Preconditions.checkArgument;
  31. /**
  32. * Builder class to help build measures based on impacts with payload such as @{link {@link org.sonar.api.measures.CoreMetrics#RELIABILITY_ISSUES}}.
  33. */
  34. public class ImpactMeasureBuilder {
  35. private static final Gson GSON = new GsonBuilder().create();
  36. private static final Type GSON_MAP_TYPE = new TypeToken<Map<String, Long>>() {
  37. }.getType();
  38. public static final String TOTAL_KEY = "total";
  39. private final Map<String, Long> map;
  40. private ImpactMeasureBuilder(Map<String, Long> map) {
  41. this.map = new LinkedHashMap<>(map);
  42. }
  43. public static ImpactMeasureBuilder createEmpty() {
  44. Map<String, Long> severityMap = new LinkedHashMap<>();
  45. for (Severity severity : Severity.values()) {
  46. severityMap.put(severity.name(), 0L);
  47. }
  48. severityMap.put(TOTAL_KEY, 0L);
  49. return new ImpactMeasureBuilder(severityMap);
  50. }
  51. public static ImpactMeasureBuilder newInstance() {
  52. return new ImpactMeasureBuilder(new LinkedHashMap<>());
  53. }
  54. public static ImpactMeasureBuilder fromMap(Map<String, Long> map) {
  55. checkImpactMap(map);
  56. return new ImpactMeasureBuilder(map);
  57. }
  58. private static void checkImpactMap(Map<String, Long> map) {
  59. checkArgument(map.containsKey(TOTAL_KEY), "Map must contain a total key");
  60. Arrays.stream(Severity.values()).forEach(severity -> checkArgument(map.containsKey(severity.name()), "Map must contain a key for severity " + severity.name()));
  61. }
  62. public static ImpactMeasureBuilder fromString(String value) {
  63. Map<String, Long> impactMap = GSON.fromJson(value, GSON_MAP_TYPE);
  64. checkImpactMap(impactMap);
  65. return new ImpactMeasureBuilder(impactMap);
  66. }
  67. public ImpactMeasureBuilder setSeverity(Severity severity, long value) {
  68. map.put(severity.name(), value);
  69. return this;
  70. }
  71. public ImpactMeasureBuilder setTotal(long value) {
  72. map.put(TOTAL_KEY, value);
  73. return this;
  74. }
  75. @CheckForNull
  76. public Long getTotal() {
  77. return map.get(TOTAL_KEY);
  78. }
  79. public ImpactMeasureBuilder add(ImpactMeasureBuilder other) {
  80. other.buildAsMap().forEach((key, val) -> map.merge(key, val, Long::sum));
  81. return this;
  82. }
  83. public String buildAsString() {
  84. checkImpactMap(map);
  85. return GSON.toJson(map);
  86. }
  87. public Map<String, Long> buildAsMap() {
  88. checkImpactMap(map);
  89. return map;
  90. }
  91. }