3 * Copyright (C) 2009-2019 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.qualitygate;
22 import com.google.common.base.MoreObjects;
23 import javax.annotation.CheckForNull;
24 import javax.annotation.Nullable;
25 import javax.annotation.concurrent.Immutable;
26 import org.sonar.ce.task.projectanalysis.metric.Metric;
28 import static java.util.Objects.hash;
29 import static java.util.Objects.requireNonNull;
32 public class Condition {
34 public enum Operator {
35 EQUALS("EQ"), NOT_EQUALS("NE"), GREATER_THAN("GT"), LESS_THAN("LT");
37 private final String dbValue;
39 Operator(String dbValue) {
40 this.dbValue = dbValue;
43 public String getDbValue() {
48 private final Metric metric;
49 private final Operator operator;
51 private final String warningThreshold;
53 private final String errorThreshold;
54 private final boolean useVariation;
56 public Condition(Metric metric, String operator,
57 @Nullable String errorThreshold, @Nullable String warningThreshold) {
58 this.metric = requireNonNull(metric);
59 this.operator = parseFromDbValue(requireNonNull(operator));
60 this.useVariation = metric.getKey().startsWith("new_");
61 this.errorThreshold = errorThreshold;
62 this.warningThreshold = warningThreshold;
65 private static Operator parseFromDbValue(String str) {
66 for (Operator operator : Operator.values()) {
67 if (operator.dbValue.equals(str)) {
71 throw new IllegalArgumentException(String.format("Unsupported operator value: '%s'", str));
74 public Metric getMetric() {
78 public boolean useVariation() {
82 public Operator getOperator() {
87 public String getWarningThreshold() {
88 return warningThreshold;
92 public String getErrorThreshold() {
93 return errorThreshold;
97 public boolean equals(@Nullable Object o) {
101 if (o == null || getClass() != o.getClass()) {
104 Condition that = (Condition) o;
105 return java.util.Objects.equals(metric, that.metric);
109 public int hashCode() {
114 public String toString() {
115 return MoreObjects.toStringHelper(this)
116 .add("metric", metric)
117 .add("operator", operator)
118 .add("warningThreshold", warningThreshold)
119 .add("errorThreshold", errorThreshold)