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.6KB

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