3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.computation.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.server.computation.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;
55 private final Integer period;
57 public Condition(Metric metric, String operator,
58 @Nullable String errorThreshold, @Nullable String warningThreshold,
59 @Nullable Integer period) {
60 this.metric = requireNonNull(metric);
61 this.operator = parseFromDbValue(requireNonNull(operator));
63 this.errorThreshold = errorThreshold;
64 this.warningThreshold = warningThreshold;
67 private static Operator parseFromDbValue(String str) {
68 for (Operator operator : Operator.values()) {
69 if (operator.dbValue.equals(str)) {
73 throw new IllegalArgumentException(String.format("Unsupported operator value: '%s'", str));
76 public Metric getMetric() {
81 public Integer getPeriod() {
85 public Operator getOperator() {
90 public String getWarningThreshold() {
91 return warningThreshold;
95 public String getErrorThreshold() {
96 return errorThreshold;
100 public boolean equals(@Nullable Object o) {
104 if (o == null || getClass() != o.getClass()) {
107 Condition that = (Condition) o;
108 return java.util.Objects.equals(metric, that.metric)
109 && java.util.Objects.equals(period, that.period);
113 public int hashCode() {
114 return hash(metric, period);
118 public String toString() {
119 return MoreObjects.toStringHelper(this)
120 .add("metric", metric)
121 .add("period", period)
122 .add("operator", operator)
123 .add("warningThreshold", warningThreshold)
124 .add("errorThreshold", errorThreshold)