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.

QPMeasureData.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.qualityprofile;
  21. import com.google.common.collect.ImmutableSortedSet;
  22. import com.google.gson.JsonObject;
  23. import com.google.gson.JsonParser;
  24. import java.io.StringWriter;
  25. import java.util.Comparator;
  26. import java.util.Map;
  27. import java.util.SortedSet;
  28. import java.util.stream.Collectors;
  29. import java.util.stream.StreamSupport;
  30. import javax.annotation.concurrent.Immutable;
  31. import org.sonar.api.utils.text.JsonWriter;
  32. import org.sonar.core.util.UtcDateUtils;
  33. import static java.util.function.Function.identity;
  34. /**
  35. * Represents the array of JSON objects stored in the value of the
  36. * {@link org.sonar.api.measures.CoreMetrics#QUALITY_PROFILES} measures.
  37. */
  38. @Immutable
  39. public class QPMeasureData {
  40. private final SortedSet<QualityProfile> profiles;
  41. public QPMeasureData(Iterable<QualityProfile> qualityProfiles) {
  42. this.profiles = ImmutableSortedSet.copyOf(QualityProfileComparator.INSTANCE, qualityProfiles);
  43. }
  44. public static QPMeasureData fromJson(String json) {
  45. return new QPMeasureData(StreamSupport.stream(JsonParser.parseString(json).getAsJsonArray().spliterator(), false)
  46. .map(jsonElement -> {
  47. JsonObject jsonProfile = jsonElement.getAsJsonObject();
  48. return new QualityProfile(
  49. jsonProfile.get("key").getAsString(),
  50. jsonProfile.get("name").getAsString(),
  51. jsonProfile.get("language").getAsString(),
  52. UtcDateUtils.parseDateTime(jsonProfile.get("rulesUpdatedAt").getAsString()));
  53. }).toList());
  54. }
  55. public static String toJson(QPMeasureData data) {
  56. StringWriter json = new StringWriter();
  57. try (JsonWriter writer = JsonWriter.of(json)) {
  58. writer.beginArray();
  59. for (QualityProfile profile : data.getProfiles()) {
  60. writer
  61. .beginObject()
  62. .prop("key", profile.getQpKey())
  63. .prop("language", profile.getLanguageKey())
  64. .prop("name", profile.getQpName())
  65. .prop("rulesUpdatedAt", UtcDateUtils.formatDateTime(profile.getRulesUpdatedAt()))
  66. .endObject();
  67. }
  68. writer.endArray();
  69. }
  70. return json.toString();
  71. }
  72. public SortedSet<QualityProfile> getProfiles() {
  73. return profiles;
  74. }
  75. public Map<String, QualityProfile> getProfilesByKey() {
  76. return profiles.stream().collect(Collectors.toMap(QualityProfile::getQpKey, identity()));
  77. }
  78. private enum QualityProfileComparator implements Comparator<QualityProfile> {
  79. INSTANCE;
  80. @Override
  81. public int compare(QualityProfile o1, QualityProfile o2) {
  82. int c = o1.getLanguageKey().compareTo(o2.getLanguageKey());
  83. if (c == 0) {
  84. c = o1.getQpName().compareTo(o2.getQpName());
  85. }
  86. return c;
  87. }
  88. }
  89. }