3 * Copyright (C) 2009-2023 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.ce.task.projectanalysis.api.posttask;
22 import javax.annotation.CheckForNull;
23 import javax.annotation.concurrent.Immutable;
24 import org.sonar.api.ce.posttask.QualityGate;
26 import static com.google.common.base.Preconditions.checkArgument;
27 import static com.google.common.base.Preconditions.checkState;
28 import static java.util.Objects.requireNonNull;
29 import static org.sonar.api.ce.posttask.QualityGate.EvaluationStatus.NO_VALUE;
32 class ConditionImpl implements QualityGate.Condition {
33 private final QualityGate.EvaluationStatus status;
34 private final String metricKey;
35 private final QualityGate.Operator operator;
36 private final String errorThreshold;
37 private final boolean onLeakPeriod;
39 private final String value;
41 private ConditionImpl(Builder builder) {
42 requireNonNull(builder.status, "status can not be null");
43 requireNonNull(builder.metricKey, "metricKey can not be null");
44 requireNonNull(builder.operator, "operator can not be null");
45 requireNonNull(builder.errorThreshold, "errorThreshold can not be null");
48 this.status = builder.status;
49 this.metricKey = builder.metricKey;
50 this.operator = builder.operator;
51 this.errorThreshold = builder.errorThreshold;
52 this.onLeakPeriod = builder.metricKey.startsWith("new_");
53 this.value = builder.value;
56 private static void verifyValue(Builder builder) {
57 if (builder.status == NO_VALUE) {
58 checkArgument(builder.value == null, "value must be null when status is %s", NO_VALUE);
60 checkArgument(builder.value != null, "value can not be null when status is not %s", NO_VALUE);
64 public static Builder newBuilder() {
68 public static class Builder {
69 private String metricKey;
70 private QualityGate.Operator operator;
71 private String errorThreshold;
74 private QualityGate.EvaluationStatus status;
77 // enforce use of static method
80 public Builder setMetricKey(String metricKey) {
81 this.metricKey = metricKey;
85 public Builder setOperator(QualityGate.Operator operator) {
86 this.operator = operator;
90 public Builder setErrorThreshold(String errorThreshold) {
91 this.errorThreshold = errorThreshold;
96 * @deprecated in 7.6. This method has no longer any effect.
99 public Builder setWarningThreshold(String warningThreshold) {
104 * @deprecated in 7.6. This method has no longer any effect.
107 public Builder setOnLeakPeriod(boolean onLeakPeriod) {
111 public Builder setValue(String value) {
116 public Builder setStatus(QualityGate.EvaluationStatus status) {
117 this.status = status;
121 public ConditionImpl build() {
122 return new ConditionImpl(this);
127 public QualityGate.EvaluationStatus getStatus() {
132 public String getMetricKey() {
137 public QualityGate.Operator getOperator() {
142 public String getErrorThreshold() {
143 return errorThreshold;
148 public String getWarningThreshold() {
153 * @deprecated in 7.6. Conditions "on leak period" were removed. Use "New X" conditions instead.
157 public boolean isOnLeakPeriod() {
162 public String getValue() {
163 checkState(status != NO_VALUE, "There is no value when status is %s", NO_VALUE);
169 public String toString() {
170 return "ConditionImpl{" +
172 ", metricKey='" + metricKey + '\'' +
173 ", operator=" + operator +
174 ", errorThreshold='" + errorThreshold + '\'' +
175 ", value='" + value + '\'' +