3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualityprofile.ws;
22 import java.util.Arrays;
23 import java.util.Objects;
24 import javax.annotation.Nullable;
25 import org.sonar.api.resources.Language;
26 import org.sonar.api.resources.Languages;
27 import org.sonar.api.server.ws.Request;
28 import org.sonar.api.server.ws.WebService;
29 import org.sonar.core.util.stream.MoreCollectors;
31 import static com.google.common.base.Preconditions.checkState;
32 import static java.util.Objects.requireNonNull;
33 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
34 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
37 * Reference to a Quality profile as defined by requests to web services api/qualityprofiles.
38 * The two exclusive options to reference a profile are:
40 * <li>by its id (to be deprecated)</li>
41 * <li>by the tuple {organizationKey, language, name}</li>
44 public class QProfileReference {
50 private final Type type;
51 private final String key;
52 private final String language;
53 private final String name;
55 private QProfileReference(Type type, @Nullable String key, @Nullable String language, @Nullable String name) {
57 if (type == Type.KEY) {
58 this.key = requireNonNull(key);
63 this.language = requireNonNull(language);
64 this.name = requireNonNull(name);
69 * @return {@code true} if key is defined and {@link #getKey()} can be called. If {@code false}, then
70 * the couple {language, name} is defined and the methods {@link #getLanguage()}/{@link #getName()}
73 public boolean hasKey() {
74 return type == Type.KEY;
78 * @return non-null key
79 * @throws IllegalStateException if {@link #hasKey()} does not return {@code true}
81 public String getKey() {
82 checkState(key != null, "Key is not defined. Please call hasKey().");
87 * @return non-null language
88 * @throws IllegalStateException if {@link #hasKey()} does not return {@code false}
90 public String getLanguage() {
91 checkState(type == Type.NAME, "Language is not defined. Please call hasKey().");
96 * @return non-null name
97 * @throws IllegalStateException if {@link #hasKey()} does not return {@code false}
99 public String getName() {
100 checkState(type == Type.NAME, "Name is not defined. Please call hasKey().");
105 public boolean equals(Object o) {
109 if (o == null || getClass() != o.getClass()) {
112 QProfileReference that = (QProfileReference) o;
113 return Objects.equals(key, that.key) && Objects.equals(language, that.language) && Objects.equals(name, that.name);
117 public int hashCode() {
118 return Objects.hash(key, language, name);
121 public static QProfileReference fromName(Request request) {
122 String lang = request.mandatoryParam(PARAM_LANGUAGE);
123 String name = request.mandatoryParam(PARAM_QUALITY_PROFILE);
124 return fromName(lang, name);
127 public static QProfileReference fromKey(String key) {
128 return new QProfileReference(Type.KEY, key, null, null);
131 public static QProfileReference fromName(String lang, String name) {
132 return new QProfileReference(Type.NAME, null, requireNonNull(lang), requireNonNull(name));
135 public static void defineParams(WebService.NewAction action, Languages languages) {
136 action.createParam(PARAM_QUALITY_PROFILE)
137 .setDescription("Quality profile name.")
139 .setExampleValue("Sonar way");
141 action.createParam(PARAM_LANGUAGE)
142 .setDescription("Quality profile language.")
144 .setPossibleValues(Arrays.stream(languages.all()).map(Language::getKey).collect(MoreCollectors.toSet()));