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.

MeasureQuery.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.db.measure;
  21. import java.util.Collection;
  22. import java.util.Objects;
  23. import javax.annotation.CheckForNull;
  24. import javax.annotation.Nullable;
  25. import static com.google.common.base.Preconditions.checkArgument;
  26. import static java.util.Collections.singleton;
  27. import static java.util.Objects.requireNonNull;
  28. public class MeasureQuery {
  29. @CheckForNull
  30. private final String analysisUuid;
  31. @CheckForNull
  32. private final Collection<String> projectUuids;
  33. @CheckForNull
  34. private final Collection<String> componentUuids;
  35. @CheckForNull
  36. private final Collection<Integer> metricIds;
  37. @CheckForNull
  38. private final Collection<String> metricKeys;
  39. private MeasureQuery(Builder builder) {
  40. this(builder.analysisUuid, builder.projectUuids, builder.componentUuids, builder.metricIds, builder.metricKeys);
  41. }
  42. private MeasureQuery(@Nullable String analysisUuid,
  43. @Nullable Collection<String> projectUuids,
  44. @Nullable Collection<String> componentUuids,
  45. @Nullable Collection<Integer> metricIds,
  46. @Nullable Collection<String> metricKeys) {
  47. checkArgument(metricIds == null || metricKeys == null, "Metric IDs and keys must not be set both");
  48. checkArgument(projectUuids != null || componentUuids != null, "At least one filter on component UUID is expected");
  49. checkArgument(componentUuids == null || componentUuids.size() == 1 || (projectUuids != null && projectUuids.size() == 1),
  50. "Component UUIDs can only be used when a single project UUID is set");
  51. this.analysisUuid = analysisUuid;
  52. this.projectUuids = projectUuids;
  53. this.componentUuids = componentUuids;
  54. this.metricIds = metricIds;
  55. this.metricKeys = metricKeys;
  56. }
  57. @CheckForNull
  58. public String getAnalysisUuid() {
  59. return analysisUuid;
  60. }
  61. @CheckForNull
  62. public Collection<String> getProjectUuids() {
  63. return projectUuids;
  64. }
  65. @CheckForNull
  66. public String getProjectUuid() {
  67. return isOnComponents() ? projectUuids.iterator().next() : null;
  68. }
  69. @CheckForNull
  70. public Collection<String> getComponentUuids() {
  71. return componentUuids;
  72. }
  73. @CheckForNull
  74. public String getComponentUuid() {
  75. return isOnSingleComponent() ? componentUuids.iterator().next() : null;
  76. }
  77. @CheckForNull
  78. public Collection<Integer> getMetricIds() {
  79. return metricIds;
  80. }
  81. @CheckForNull
  82. public Collection<String> getMetricKeys() {
  83. return metricKeys;
  84. }
  85. public boolean returnsEmpty() {
  86. return (projectUuids != null && projectUuids.isEmpty())
  87. || (componentUuids != null && componentUuids.isEmpty())
  88. || (metricIds != null && metricIds.isEmpty())
  89. || (metricKeys != null && metricKeys.isEmpty());
  90. }
  91. public boolean isOnProjects() {
  92. return projectUuids != null && componentUuids == null;
  93. }
  94. public boolean isOnComponents() {
  95. return projectUuids != null && projectUuids.size() == 1 && componentUuids != null;
  96. }
  97. public boolean isOnSingleComponent() {
  98. return projectUuids == null && componentUuids != null && componentUuids.size() == 1;
  99. }
  100. @Override
  101. public boolean equals(@Nullable Object o) {
  102. if (this == o) {
  103. return true;
  104. }
  105. if (o == null || getClass() != o.getClass()) {
  106. return false;
  107. }
  108. MeasureQuery that = (MeasureQuery) o;
  109. return Objects.equals(analysisUuid, that.analysisUuid) &&
  110. Objects.equals(projectUuids, that.projectUuids) &&
  111. Objects.equals(componentUuids, that.componentUuids) &&
  112. Objects.equals(metricIds, that.metricIds) &&
  113. Objects.equals(metricKeys, that.metricKeys);
  114. }
  115. @Override
  116. public int hashCode() {
  117. return Objects.hash(analysisUuid, componentUuids, metricIds, metricKeys);
  118. }
  119. public static Builder builder() {
  120. return new Builder();
  121. }
  122. static MeasureQuery copyWithSubsetOfProjectUuids(MeasureQuery query, Collection<String> projectUuids) {
  123. return new MeasureQuery(query.analysisUuid, projectUuids, query.componentUuids, query.metricIds, query.metricKeys);
  124. }
  125. static MeasureQuery copyWithSubsetOfComponentUuids(MeasureQuery query, Collection<String> componentUuids) {
  126. return new MeasureQuery(query.analysisUuid, query.projectUuids, componentUuids, query.metricIds, query.metricKeys);
  127. }
  128. public static final class Builder {
  129. private String analysisUuid;
  130. private Collection<String> projectUuids;
  131. private Collection<String> componentUuids;
  132. private Collection<Integer> metricIds;
  133. private Collection<String> metricKeys;
  134. private Builder() {
  135. // see MeasureQuery#builder()
  136. }
  137. public Builder setAnalysisUuid(String analysisUuid) {
  138. this.analysisUuid = analysisUuid;
  139. return this;
  140. }
  141. /**
  142. * List of projects
  143. */
  144. public Builder setProjectUuids(@Nullable Collection<String> projectUuids) {
  145. this.projectUuids = projectUuids;
  146. return this;
  147. }
  148. /**
  149. * List of components of a project
  150. */
  151. public Builder setComponentUuids(String projectUuid, Collection<String> componentUuids) {
  152. setProjectUuids(singleton(requireNonNull(projectUuid)));
  153. this.componentUuids = componentUuids;
  154. return this;
  155. }
  156. /**
  157. * Single component
  158. */
  159. public Builder setComponentUuid(String componentUuid) {
  160. this.componentUuids = singleton(componentUuid);
  161. return this;
  162. }
  163. /**
  164. * All the measures are returned if parameter is {@code null}.
  165. */
  166. public Builder setMetricIds(@Nullable Collection<Integer> metricIds) {
  167. this.metricIds = metricIds;
  168. return this;
  169. }
  170. public Builder setMetricId(int metricId) {
  171. this.metricIds = singleton(metricId);
  172. return this;
  173. }
  174. /**
  175. * All the measures are returned if parameter is {@code null}.
  176. */
  177. public Builder setMetricKeys(@Nullable Collection<String> s) {
  178. this.metricKeys = s;
  179. return this;
  180. }
  181. public Builder setMetricKey(String s) {
  182. this.metricKeys = singleton(s);
  183. return this;
  184. }
  185. public MeasureQuery build() {
  186. return new MeasureQuery(this);
  187. }
  188. }
  189. }