aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/web/Criterion.java
blob: 1eaf10eb92039785e616e4df04a063e6eb8106e9 (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
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2013 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube 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.
 *
 * SonarQube 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.api.web;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSortedSet;

import java.util.Set;

/**
 * Definition of a criterion to be used to narrow down a {@link Filter}.
 *
 * @since 3.1
 */
public final class Criterion {
  private static final String METRIC_FAMILY = "metric";
  private static final String QUALIFIER_FAMILY = "qualifier";
  public static final String EQ = "eq";
  public static final String GT = "gt";
  public static final String GTE = "gte";
  public static final String LT = "lt";
  public static final String LTE = "lte";
  public static final Set<String> OPERATORS = ImmutableSortedSet.of(EQ, GT, GTE, LT, LTE);

  private final String family;
  private final String key;
  private final String operator;
  private final Float value;
  private final String textValue;
  private final boolean variation;

  private Criterion(String family, String key, String operator, Float value, String textValue, boolean variation) {
    Preconditions.checkArgument(OPERATORS.contains(operator), "Valid operators are %s, not '%s'", OPERATORS, operator);

    this.family = family;
    this.key = key;
    this.operator = operator;
    this.value = value;
    this.textValue = textValue;
    this.variation = variation;
  }

  /**
   * Creates a new {@link Criterion} with a numerical value.
   *
   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
   *
   * @throws IllegalArgumentException if {@code operator} is not valid
   */
  public static Criterion create(String family, String key, String operator, Float value, boolean variation) {
    return new Criterion(family, key, operator, value, null, variation);
  }

  /**
   * Creates a new {@link Criterion} with a text value.
   *
   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
   *
   * @throws IllegalArgumentException if {@code operator} is not valid
   */
  public static Criterion create(String family, String key, String operator, String textValue, boolean variation) {
    return new Criterion(family, key, operator, null, textValue, variation);
  }

  /**
   * Creates a new {@link Criterion} on a metric, with a numerical value.
   *
   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
   *
   * @throws IllegalArgumentException if {@code operator} is not valid
   */
  public static Criterion createForMetric(String key, String operator, Float value, boolean variation) {
    return new Criterion(METRIC_FAMILY, key, operator, value, null, variation);
  }

  /**
   * Creates a new {@link Criterion} on a metric, with a text value.
   *
   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
   *
   * @throws IllegalArgumentException if {@code operator} is not valid
   */
  public static Criterion createForMetric(String key, String operator, String textValue, boolean variation) {
    return new Criterion(METRIC_FAMILY, key, operator, null, textValue, variation);
  }

  /**
   * Creates a new {@link Criterion} on a qualifier.
   */
  public static Criterion createForQualifier(Object... values) {
    return new Criterion(QUALIFIER_FAMILY, null, EQ, null, Joiner.on(',').join(values), false);
  }

  /**
   * Get the the criterion's family.
   *
   * @return the family
   */
  public String getFamily() {
    return family;
  }

  /**
   * Get the the criterion's key.
   *
   * @return the key
   */
  public String getKey() {
    return key;
  }

  /**
   * Get the the criterion's operator.
   *
   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
   *
   * @return the operator
   */
  public String getOperator() {
    return operator;
  }

  /**
   * Get the the criterion's value.
   *
   * @return the value
   */
  public Float getValue() {
    return value;
  }

  /**
   * Get the the criterion's value as text.
   *
   * @return the value as text
   */
  public String getTextValue() {
    return textValue;
  }

  /**
   * A criterion can be based on the varation of a value rather than on the value itself.
   *
   * @return <code>true</code> when the variation is used rather than the value
   */
  public boolean isVariation() {
    return variation;
  }
}