package org.sonar.api.web;
import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSortedSet;
-import java.util.List;
+import java.util.Set;
/**
* Definition of a criterion to be used to narrow down a {@see Filter}.
* @since 3.1
*/
public class Criterion {
- public static final List<String> OPERATORS = ImmutableList.of("=", ">", "<", ">=", "<=");
+ public static final Set<String> OPERATORS = ImmutableSortedSet.of("=", ">", "<", ">=", "<=");
- private String family;
- private String key;
- private String operator;
- private Float value;
- private String textValue;
- private boolean variation;
+ 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() {
- // The factory method should be used
+ 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}.
+ * Creates a new {@link Criterion} with a numerical value.
+ *
+ * <p>Valid values for the {@code operator} are <code>=</code>, <code>></code>, <code>>=</code>, <code><</code> and <code><=</code></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() {
- return new Criterion();
+ public static Criterion create(String family, String key, String operator, Float value, boolean variation) {
+ return new Criterion(family, key, operator, value, null, variation);
}
- public String getFamily() {
- return family;
+ /**
+ * Creates a new {@link Criterion} with a text value.
+ *
+ * <p>Valid values for the {@code operator} are <code>=</code>, <code>></code>, <code>>=</code>, <code><</code> and <code><=</code></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);
}
- public Criterion setFamily(String family) {
- this.family = family;
- return this;
+ /**
+ * 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;
}
- public Criterion setKey(String key) {
- this.key = key;
- return this;
- }
-
+ /**
+ * Get the the criterion's operator.
+ *
+ * @return the operator
+ */
public String getOperator() {
return operator;
}
- public Criterion setOperator(String operator) {
- Preconditions.checkArgument(OPERATORS.contains(operator), "Valid operators are %s, not %s", OPERATORS, operator);
- this.operator = operator;
- return this;
- }
-
+ /**
+ * Get the the criterion's value.
+ *
+ * @return the value
+ */
public Float getValue() {
return value;
}
- public Criterion setValue(Float value) {
- this.value = value;
- return this;
- }
-
+ /**
+ * Get the the criterion's value as text.
+ *
+ * @return the value as text
+ */
public String getTextValue() {
return textValue;
}
- public Criterion setTextValue(String textValue) {
- this.textValue = textValue;
- return this;
- }
-
+ /**
+ * 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;
}
-
- public Criterion setVariation(boolean variation) {
- this.variation = variation;
- return this;
- }
}
*/
package org.sonar.api.web;
+import com.google.common.collect.ImmutableSortedSet;
+
+import java.util.Set;
+
+import com.google.common.base.Preconditions;
+
/**
* Definition of a {@see Filter} column.
*
* @since 3.1
*/
public class FilterColumn {
- private String family;
- private String key;
- private String sortDirection;
- private Long orderIndex;
- private boolean variation;
+ public static final Set<String> DIRECTIONS = ImmutableSortedSet.of("ASC", "DESC");
+
+ private final String family;
+ private final String key;
+ private final String sortDirection;
+ private final int orderIndex;
+ private final boolean variation;
+
+ private FilterColumn(String family, String key, String sortDirection, int orderIndex, boolean variation) {
+ Preconditions.checkArgument(DIRECTIONS.contains(sortDirection), "Valid directions are %s, not '%s'", DIRECTIONS, sortDirection);
- private FilterColumn() {
- // The factory method should be used
+ this.family = family;
+ this.key = key;
+ this.sortDirection = sortDirection;
+ this.orderIndex = orderIndex;
+ this.variation = variation;
}
/**
* Creates a new {@link FilterColumn}.
+ *
+ * <p>Valid values for the {@code sortDirection} are <code>ASC</code> and <code>DESC</code></p>
+ *
+ * <p>When the @{see Filter} is persisted, a validation is made on the {@code family} and the {@code key}.
+ * They should point to a valid column description.</p>
+ *
+ * @throws IllegalArgumentException if {@code sortDirection} is not valid
*/
- public static FilterColumn create() {
- return new FilterColumn();
+ public static FilterColumn create(String family, String key, String sortDirection, int orderIndex, boolean variation) {
+ return new FilterColumn(family, key, sortDirection, orderIndex, variation);
}
+ /**
+ * Get the the column's family.
+ *
+ * @return the family
+ */
public String getFamily() {
return family;
}
- public FilterColumn setFamily(String family) {
- this.family = family;
- return this;
- }
-
+ /**
+ * Get the the column's key.
+ *
+ * @return the key
+ */
public String getKey() {
return key;
}
- public FilterColumn setKey(String key) {
- this.key = key;
- return this;
- }
-
+ /**
+ * Get the the column's sort direction.
+ *
+ * @return the sort direction
+ */
public String getSortDirection() {
return sortDirection;
}
- public FilterColumn setSortDirection(String sortDirection) {
- this.sortDirection = sortDirection;
- return this;
- }
-
- public Long getOrderIndex() {
+ /**
+ * Get the the column's index.
+ *
+ * @return the index
+ */
+ public int getOrderIndex() {
return orderIndex;
}
- public FilterColumn setOrderIndex(Long orderIndex) {
- this.orderIndex = orderIndex;
- return this;
- }
-
+ /**
+ * A column 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;
}
-
- public FilterColumn setVariation(boolean variation) {
- this.variation = variation;
- return this;
- }
}
@Test
public void should_accept_valid_operators() {
- Criterion.create().setOperator("<=");
- Criterion.create().setOperator("<");
- Criterion.create().setOperator("=");
- Criterion.create().setOperator(">");
- Criterion.create().setOperator(">=");
+ Criterion.create("", "", "<=", "", false);
+ Criterion.create("", "", "<", "", false);
+ Criterion.create("", "", "=", "", false);
+ Criterion.create("", "", ">", "", false);
+ Criterion.create("", "", ">=", "", false);
}
@Test
public void should_fail_on_invalid_operators() {
exception.expect(IllegalArgumentException.class);
- exception.expectMessage("Valid operators are [=, >, <, >=, <=], not <>");
+ exception.expectMessage("Valid operators are [<, <=, =, >, >=], not '<>'");
- Criterion.create().setOperator("<>");
+ Criterion.create("", "", "<>", "", false);
}
}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.web;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class FilterColumnTest {
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
+ @Test
+ public void should_accept_valid_direction() {
+ FilterColumn.create("", "", "ASC", 0, false);
+ FilterColumn.create("", "", "DESC", 0, false);
+ }
+
+ @Test
+ public void should_fail_on_invalid_direction() {
+ exception.expect(IllegalArgumentException.class);
+ exception.expectMessage("Valid directions are [ASC, DESC], not 'UNKNOWN'");
+
+ FilterColumn.create("", "", "UNKNOWN", 0, false);
+ }
+
+}
filterDto.add(new FilterColumnDto()
.setFamily(column.getFamily())
.setKey(column.getKey())
- .setOrderIndex(column.getOrderIndex())
+ .setOrderIndex((long) column.getOrderIndex())
.setSortDirection(column.getSortDirection())
.setVariation(column.isVariation()));
}
.setLanguage("java")
.setSearchFor("TRK,BRC")
.setPageSize(200)
- .add(Criterion.create().setFamily("metric").setKey("complexity").setOperator("<").setValue(12f))
- .add(FilterColumn.create().setFamily("metric").setKey("distance").setOrderIndex(1L).setSortDirection("ASC"))
+ .add(Criterion.create("metric", "complexity", "<", 12f, false))
+ .add(Criterion.create("metric", "LCOM4", ">=", "5", true))
+ .add(FilterColumn.create("metric", "distance", "ASC", 1, false))
+ .add(FilterColumn.create("metric", "instability", "DESC", 2, true))
);
FilterDto dto = register.createDtoFromExtension("Fake", filterTemplate.createFilter());
assertThat(dto.getDefaultView()).isEqualTo("list");
assertThat(dto.getPageSize()).isEqualTo(200L);
- assertThat(dto.getCriteria()).hasSize(5);
+ assertThat(dto.getCriteria()).hasSize(6);
assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("key").setOperator("=").setTextValue("*KEY*")));
assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("name").setOperator("=").setTextValue("*NAME*")));
assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("language").setOperator("=").setTextValue("java")));
assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("qualifier").setOperator("=").setTextValue("TRK,BRC")));
assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("metric").setKey("complexity").setOperator("<").setValue(12f).setVariation(false)));
+ assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("metric").setKey("LCOM4").setOperator(">=").setTextValue("5").setVariation(true)));
- assertThat(dto.getColumns()).hasSize(1);
+ assertThat(dto.getColumns()).hasSize(2);
assertThat(dto.getColumns()).satisfies(contains(new FilterColumnDto().setFamily("metric").setKey("distance").setOrderIndex(1L).setSortDirection("ASC").setVariation(false)));
+ assertThat(dto.getColumns()).satisfies(contains(new FilterColumnDto().setFamily("metric").setKey("instability").setOrderIndex(2L).setSortDirection("DESC").setVariation(true)));
}
}