Filter filter = Filter.create();
filter.setDisplayAs(Filter.LIST);
filter.setFavouritesOnly(true);
- filter.add(Criterion.create("qualifier", null, Criterion.EQ, "VW,SVW,TRK,BRC,DIR,PAC,FIL,CLA,UTS,LIB", false));
+ filter.add(Criterion.createForQualifier("VW", "SVW", "TRK", "BRC", "DIR", "PAC", "FIL", "CLA", "UTS", "LIB"));
filter.add(FilterColumn.create("metric", "alert_status", FilterColumn.DESC, false));
filter.add(FilterColumn.create("name", null, FilterColumn.ASC, false));
filter.add(FilterColumn.create("metric", "ncloc", FilterColumn.DESC, false));
public Filter createFilter() {
Filter filter = Filter.create();
filter.setDisplayAs(Filter.LIST);
- filter.add(Criterion.create("qualifier", null, Criterion.EQ, "TRK", false));
+ filter.add(Criterion.createForQualifier("TRK"));
filter.add(FilterColumn.create("metric", "alert_status", FilterColumn.DESC, false));
filter.add(FilterColumn.create("name", null, FilterColumn.ASC, false));
filter.add(FilterColumn.create("version", null, FilterColumn.DESC, false));
public Filter createFilter() {
Filter filter = Filter.create();
filter.setDisplayAs(Filter.TREEMAP);
- filter.add(Criterion.create("qualifier", null, Criterion.EQ, "TRK", false));
+ filter.add(Criterion.createForQualifier("TRK"));
filter.add(FilterColumn.create("name", null, FilterColumn.ASC, false));
filter.add(FilterColumn.create("metric", "ncloc", FilterColumn.DESC, false));
filter.add(FilterColumn.create("metric", "violations_density", FilterColumn.DESC, false));
*/
package org.sonar.api.web;
+import com.google.common.base.Joiner;
+
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSortedSet;
*
* <p>Valid values for the {@code operator} are {@value #EQ}, {@value #GT}, {@value #GTE}, {@value #LT} and {@value #LTE}</p>
*
- * <p>When the {@link Filter} is persisted, a validation is made on the {@code family} and the {@code key}.
- * They should point to a valid criterion.</p>
- *
* @throws IllegalArgumentException if {@code operator} is not valid
*/
public static Criterion create(String family, String key, String operator, Float value, boolean variation) {
*
* <p>Valid values for the {@code operator} are {@value #EQ}, {@value #GT}, {@value #GTE}, {@value #LT} and {@value #LTE}</p>
*
- * <p>When the {@link Filter} is persisted, a validation is made on the {@code family} and the {@code key}.
- * They should point to a valid criterion.</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 {@value #EQ}, {@value #GT}, {@value #GTE}, {@value #LT} and {@value #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", 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 {@value #EQ}, {@value #GT}, {@value #GTE}, {@value #LT} and {@value #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", key, operator, null, textValue, variation);
+ }
+
+ /**
+ * Creates a new {@link Criterion} on a qualifier.
+ */
+ public static Criterion createForQualifier(Object... values) {
+ return new Criterion("qualifier", null, EQ, null, Joiner.on(',').join(values), false);
+ }
+
/**
* Get the the criterion's family.
*
@Test
public void should_accept_valid_operators() {
- Criterion.create("", "", "<=", "", false);
- Criterion.create("", "", "<", "", false);
- Criterion.create("", "", "=", "", false);
- Criterion.create("", "", ">", "", false);
- Criterion.create("", "", ">=", "", false);
+ Criterion.createForMetric("", "<=", "", false);
+ Criterion.createForMetric("", "<", "", false);
+ Criterion.createForMetric("", "=", "", false);
+ Criterion.createForMetric("", ">", "", false);
+ Criterion.createForMetric("", ">=", "", false);
}
@Test
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Valid operators are [<, <=, =, >, >=], not '<>'");
- Criterion.create("", "", "<>", "", false);
+ Criterion.createForMetric("", "<>", "", false);
}
}
when(filterTemplate.createFilter()).thenReturn(Filter.create()
.setFavouritesOnly(false)
.setDisplayAs("list")
- .add(Criterion.create("metric", "complexity", Criterion.LT, 12f, false))
- .add(Criterion.create("metric", "LCOM4", Criterion.GTE, "5", true))
+ .add(Criterion.createForMetric("complexity", Criterion.LT, 12f, false))
+ .add(Criterion.createForMetric("LCOM4", Criterion.GTE, "5", true))
.add(FilterColumn.create("metric", "distance", "ASC", false))
.add(FilterColumn.create("metric", "instability", "DESC", true))
);