aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/main/java/org/sonar/db/measure/MeasureQuery.java
blob: 0b2685ebbe5ccc0ab8cdb649ceeacc086cb09499 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * SonarQube
 * Copyright (C) 2009-2017 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 java.util.Collection;
import java.util.Objects;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Collections.singleton;
import static java.util.Objects.requireNonNull;

public class MeasureQuery {
  @CheckForNull
  private final String analysisUuid;

  @CheckForNull
  private final Collection<String> projectUuids;

  @CheckForNull
  private final Collection<String> componentUuids;

  @CheckForNull
  private final Collection<Integer> metricIds;

  @CheckForNull
  private final Collection<String> metricKeys;

  @CheckForNull
  private final Long personId;

  private MeasureQuery(Builder builder) {
    this(builder.analysisUuid, builder.projectUuids, builder.componentUuids, builder.metricIds, builder.metricKeys, builder.personId);
  }

  private MeasureQuery(@Nullable String analysisUuid,
    @Nullable Collection<String> projectUuids,
    @Nullable Collection<String> componentUuids,
    @Nullable Collection<Integer> metricIds,
    @Nullable Collection<String> metricKeys,
    @Nullable Long personId) {
    checkArgument(metricIds == null || metricKeys == null, "Metric IDs and keys must not be set both");
    checkArgument(projectUuids != null || componentUuids != null, "At least one filter on component UUID is expected");
    checkArgument(componentUuids == null || componentUuids.size() == 1 || (projectUuids != null && projectUuids.size() == 1),
      "Component UUIDs can only be used when a single project UUID is set");

    this.analysisUuid = analysisUuid;
    this.projectUuids = projectUuids;
    this.componentUuids = componentUuids;
    this.metricIds = metricIds;
    this.metricKeys = metricKeys;
    this.personId = personId;
  }

  public String getAnalysisUuid() {
    return analysisUuid;
  }

  @CheckForNull
  public Collection<String> getProjectUuids() {
    return projectUuids;
  }

  @CheckForNull
  public String getProjectUuid() {
    return isOnComponents() ? projectUuids.iterator().next() : null;
  }

  @CheckForNull
  public Collection<String> getComponentUuids() {
    return componentUuids;
  }

  @CheckForNull
  public String getComponentUuid() {
    return isOnSingleComponent() ? componentUuids.iterator().next() : null;
  }

  @CheckForNull
  public Collection<Integer> getMetricIds() {
    return metricIds;
  }

  @CheckForNull
  public Collection<String> getMetricKeys() {
    return metricKeys;
  }

  @CheckForNull
  public Long getPersonId() {
    return personId;
  }

  public boolean returnsEmpty() {
    return (projectUuids != null && projectUuids.isEmpty())
      || (componentUuids != null && componentUuids.isEmpty())
      || (metricIds != null && metricIds.isEmpty())
      || (metricKeys != null && metricKeys.isEmpty());
  }

  public boolean isOnProjects() {
    return projectUuids != null && componentUuids == null;
  }

  public boolean isOnComponents() {
    return projectUuids != null && projectUuids.size() == 1 && componentUuids != null;
  }

  public boolean isOnSingleComponent() {
    return projectUuids == null && componentUuids != null && componentUuids.size() == 1;
  }

  @Override
  public boolean equals(@Nullable Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    MeasureQuery that = (MeasureQuery) o;
    return Objects.equals(analysisUuid, that.analysisUuid) &&
      Objects.equals(projectUuids, that.projectUuids) &&
      Objects.equals(componentUuids, that.componentUuids) &&
      Objects.equals(metricIds, that.metricIds) &&
      Objects.equals(metricKeys, that.metricKeys) &&
      Objects.equals(personId, that.personId);
  }

  @Override
  public int hashCode() {
    return Objects.hash(analysisUuid, componentUuids, metricIds, metricKeys, personId);
  }

  public static Builder builder() {
    return new Builder();
  }

  static MeasureQuery copyWithSubsetOfProjectUuids(MeasureQuery query, Collection<String> projectUuids) {
    return new MeasureQuery(query.analysisUuid, projectUuids, query.componentUuids, query.metricIds, query.metricKeys, query.personId);
  }

  static MeasureQuery copyWithSubsetOfComponentUuids(MeasureQuery query, Collection<String> componentUuids) {
    return new MeasureQuery(query.analysisUuid, query.projectUuids, componentUuids, query.metricIds, query.metricKeys, query.personId);
  }

  public static final class Builder {
    private String analysisUuid;
    private Collection<String> projectUuids;
    private Collection<String> componentUuids;
    private Collection<Integer> metricIds;
    private Collection<String> metricKeys;
    private Long personId;

    private Builder() {
      // see MeasureQuery#builder()
    }

    public Builder setAnalysisUuid(String analysisUuid) {
      this.analysisUuid = analysisUuid;
      return this;
    }

    /**
     * List of projects
     */
    public Builder setProjectUuids(@Nullable Collection<String> projectUuids) {
      this.projectUuids = projectUuids;
      return this;
    }

    /**
     * List of components of a project
     */
    public Builder setComponentUuids(String projectUuid, Collection<String> componentUuids) {
      setProjectUuids(singleton(requireNonNull(projectUuid)));
      this.componentUuids = componentUuids;
      return this;
    }

    /**
     * Single component
     */
    public Builder setComponentUuid(String componentUuid) {
      this.componentUuids = singleton(componentUuid);
      return this;
    }

    /**
     * All the measures are returned if parameter is {@code null}.
     */
    public Builder setMetricIds(@Nullable Collection<Integer> metricIds) {
      this.metricIds = metricIds;
      return this;
    }

    public Builder setMetricId(int metricId) {
      this.metricIds = singleton(metricId);
      return this;
    }

    /**
     * All the measures are returned if parameter is {@code null}.
     */
    public Builder setMetricKeys(@Nullable Collection<String> s) {
      this.metricKeys = s;
      return this;
    }

    public Builder setMetricKey(String s) {
      this.metricKeys = singleton(s);
      return this;
    }

    public Builder setPersonId(@Nullable Long l) {
      this.personId = l;
      return this;
    }

    public MeasureQuery build() {
      return new MeasureQuery(this);
    }
  }
}