001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.web;
021
022import com.google.common.base.Joiner;
023import com.google.common.base.Preconditions;
024import com.google.common.collect.ImmutableSortedSet;
025
026import java.util.Set;
027
028/**
029 * Definition of a criterion to be used to narrow down a {@link Filter}.
030 *
031 * @since 3.1
032 */
033public final class Criterion {
034  private static final String METRIC_FAMILY = "metric";
035  private static final String QUALIFIER_FAMILY = "qualifier";
036  public static final String EQ = "eq";
037  public static final String GT = "gt";
038  public static final String GTE = "gte";
039  public static final String LT = "lt";
040  public static final String LTE = "lte";
041  public static final Set<String> OPERATORS = ImmutableSortedSet.of(EQ, GT, GTE, LT, LTE);
042
043  private final String family;
044  private final String key;
045  private final String operator;
046  private final Float value;
047  private final String textValue;
048  private final boolean variation;
049
050  private Criterion(String family, String key, String operator, Float value, String textValue, boolean variation) {
051    Preconditions.checkArgument(OPERATORS.contains(operator), "Valid operators are %s, not '%s'", OPERATORS, operator);
052
053    this.family = family;
054    this.key = key;
055    this.operator = operator;
056    this.value = value;
057    this.textValue = textValue;
058    this.variation = variation;
059  }
060
061  /**
062   * Creates a new {@link Criterion} with a numerical value.
063   *
064   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
065   *
066   * @throws IllegalArgumentException if {@code operator} is not valid
067   */
068  public static Criterion create(String family, String key, String operator, Float value, boolean variation) {
069    return new Criterion(family, key, operator, value, null, variation);
070  }
071
072  /**
073   * Creates a new {@link Criterion} with a text value.
074   *
075   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
076   *
077   * @throws IllegalArgumentException if {@code operator} is not valid
078   */
079  public static Criterion create(String family, String key, String operator, String textValue, boolean variation) {
080    return new Criterion(family, key, operator, null, textValue, variation);
081  }
082
083  /**
084   * Creates a new {@link Criterion} on a metric, with a numerical value.
085   *
086   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
087   *
088   * @throws IllegalArgumentException if {@code operator} is not valid
089   */
090  public static Criterion createForMetric(String key, String operator, Float value, boolean variation) {
091    return new Criterion(METRIC_FAMILY, key, operator, value, null, variation);
092  }
093
094  /**
095   * Creates a new {@link Criterion} on a metric, with a text value.
096   *
097   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
098   *
099   * @throws IllegalArgumentException if {@code operator} is not valid
100   */
101  public static Criterion createForMetric(String key, String operator, String textValue, boolean variation) {
102    return new Criterion(METRIC_FAMILY, key, operator, null, textValue, variation);
103  }
104
105  /**
106   * Creates a new {@link Criterion} on a qualifier.
107   */
108  public static Criterion createForQualifier(Object... values) {
109    return new Criterion(QUALIFIER_FAMILY, null, EQ, null, Joiner.on(',').join(values), false);
110  }
111
112  /**
113   * Get the the criterion's family.
114   *
115   * @return the family
116   */
117  public String getFamily() {
118    return family;
119  }
120
121  /**
122   * Get the the criterion's key.
123   *
124   * @return the key
125   */
126  public String getKey() {
127    return key;
128  }
129
130  /**
131   * Get the the criterion's operator.
132   *
133   * <p>Valid values for the {@code operator} are {@code #EQ}, {@code #GT}, {@code #GTE}, {@code #LT} and {@code #LTE}</p>
134   *
135   * @return the operator
136   */
137  public String getOperator() {
138    return operator;
139  }
140
141  /**
142   * Get the the criterion's value.
143   *
144   * @return the value
145   */
146  public Float getValue() {
147    return value;
148  }
149
150  /**
151   * Get the the criterion's value as text.
152   *
153   * @return the value as text
154   */
155  public String getTextValue() {
156    return textValue;
157  }
158
159  /**
160   * A criterion can be based on the varation of a value rather than on the value itself.
161   *
162   * @return <code>true</code> when the variation is used rather than the value
163   */
164  public boolean isVariation() {
165    return variation;
166  }
167}