<version>${sonarCSharp.version}</version>
<type>sonar-plugin</type>
</dependency>
- <dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- <version>3.2.2</version>
- </dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
- <dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- </dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
// No contextual query case
String queryText = query.getQueryText();
- if (queryText == null || queryText.isEmpty()) {
+ if (StringUtils.isEmpty(queryText)) {
return QueryBuilders.matchAllQuery();
}
FilterBuilders.termFilter(FIELD_RULE_STATUS,
RuleStatus.REMOVED.toString())));
- if (!StringUtils.isEmpty(query.getInternalKey())) {
+ if (StringUtils.isNotEmpty(query.getInternalKey())) {
filters.put(FIELD_RULE_INTERNAL_KEY,
FilterBuilders.termFilter(FIELD_RULE_INTERNAL_KEY, query.getInternalKey()));
}
- if (!StringUtils.isEmpty(query.getRuleKey())) {
+ if (StringUtils.isNotEmpty(query.getRuleKey())) {
filters.put(FIELD_RULE_RULE_KEY,
FilterBuilders.termFilter(FIELD_RULE_RULE_KEY, query.getRuleKey()));
}
- if (!CollectionUtils.isEmpty(query.getLanguages())) {
+ if (isNotEmpty(query.getLanguages())) {
filters.put(FIELD_RULE_LANGUAGE,
FilterBuilders.termsFilter(FIELD_RULE_LANGUAGE, query.getLanguages()));
}
- if (!CollectionUtils.isEmpty(query.getRepositories())) {
+ if (isNotEmpty(query.getRepositories())) {
filters.put(FIELD_RULE_REPOSITORY,
FilterBuilders.termsFilter(FIELD_RULE_REPOSITORY, query.getRepositories()));
}
- if (!CollectionUtils.isEmpty(query.getSeverities())) {
+ if (isNotEmpty(query.getSeverities())) {
filters.put(FIELD_RULE_SEVERITY,
FilterBuilders.termsFilter(FIELD_RULE_SEVERITY, query.getSeverities()));
}
- if (!StringUtils.isEmpty(query.getKey())) {
+ if (StringUtils.isNotEmpty(query.getKey())) {
filters.put(FIELD_RULE_KEY,
FilterBuilders.termFilter(FIELD_RULE_KEY, query.getKey()));
}
- if (!CollectionUtils.isEmpty(query.getTags())) {
+ if (isNotEmpty(query.getTags())) {
filters.put(FIELD_RULE_ALL_TAGS,
FilterBuilders.termsFilter(FIELD_RULE_ALL_TAGS, query.getTags()));
}
- if (!CollectionUtils.isEmpty(query.getTypes())) {
+ if (isNotEmpty(query.getTypes())) {
filters.put(FIELD_RULE_TYPE,
FilterBuilders.termsFilter(FIELD_RULE_TYPE, query.getTypes()));
}
.gte(query.getAvailableSinceLong()));
}
- Collection<RuleStatus> statusValues = query.getStatuses();
- if (statusValues != null && !statusValues.isEmpty()) {
+ if (isNotEmpty(query.getStatuses())) {
Collection<String> stringStatus = new ArrayList<>();
- for (RuleStatus status : statusValues) {
+ for (RuleStatus status : query.getStatuses()) {
stringStatus.add(status.name());
}
filters.put(FIELD_RULE_STATUS,
}
private static BoolFilterBuilder addTermFilter(BoolFilterBuilder filter, String field, @Nullable Collection<String> values) {
- if (values != null && !values.isEmpty()) {
+ if (isNotEmpty(values)) {
BoolFilterBuilder valuesFilter = FilterBuilders.boolFilter();
for (String value : values) {
FilterBuilder valueFilter = FilterBuilders.termFilter(field, value);
}
private static BoolFilterBuilder addTermFilter(BoolFilterBuilder filter, String field, @Nullable String value) {
- if (value != null && !value.isEmpty()) {
+ if (StringUtils.isNotEmpty(value)) {
filter.must(FilterBuilders.termFilter(field, value));
}
return filter;
sort.order(SortOrder.DESC);
}
esSearch.addSort(sort);
- } else if (queryText != null && !queryText.isEmpty()) {
+ } else if (StringUtils.isNotEmpty(queryText)) {
esSearch.addSort(SortBuilders.scoreSort());
} else {
esSearch.addSort(appendSortSuffixIfNeeded(FIELD_RULE_UPDATED_AT), SortOrder.DESC);
public RuleKey apply(@Nonnull String input) {
return RuleKey.parse(input);
}
- }
+ }
private enum RuleStatusToString implements Function<RuleStatus, String> {
INSTANCE;
public String apply(@Nonnull RuleStatus input) {
return input.toString();
}
- }
+ }
private enum NotRemoved implements Predicate<String> {
INSTANCE;
public boolean apply(@Nonnull String input) {
return !RuleStatus.REMOVED.toString().equals(input);
}
+
+ }
+
+ private static boolean isNotEmpty(@Nullable Collection list) {
+ return list != null && !list.isEmpty();
}
}
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
- <dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
*/
package org.sonar.api.measures;
+import com.google.common.collect.Multiset;
+import com.google.common.collect.TreeMultiset;
import java.util.Map;
-import org.apache.commons.collections.SortedBag;
-import org.apache.commons.collections.bag.TreeBag;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.sonar.api.utils.KeyValueFormat;
*/
public class CountDistributionBuilder implements MeasureBuilder {
- private Metric metric;
- // TODO to be replaced by com.google.common.collect.SortedMultiset while upgrading to Guava 11+
- private SortedBag countBag;
+ private final Metric metric;
+ private final Multiset countBag = TreeMultiset.create();
/**
* Creates an empty CountDistributionBuilder for a specified metric
* @param metric the metric
*/
public CountDistributionBuilder(Metric metric) {
- setMetric(metric);
- this.countBag = new TreeBag();
+ if (metric == null || !metric.isDataType()) {
+ throw new SonarException("Metric is null or has invalid type");
+ }
+ this.metric = metric;
}
/**
addZero(value);
} else {
- if (this.countBag.add(value, count)) {
- //hack
+ if (this.countBag.add(value, count) == 0) {
+ // hack
this.countBag.add(value, 1);
}
}
*/
public Measure build(boolean allowEmptyData) {
if (!isEmpty() || allowEmptyData) {
- //-1 is a hack to include zero values
- return new Measure(metric, KeyValueFormat.format(countBag, -1));
+ return new Measure(metric, MultisetDistributionFormat.format(countBag));
}
return null;
}
-
- private void setMetric(Metric metric) {
- if (metric == null || !metric.isDataType()) {
- throw new SonarException("Metric is null or has unvalid type");
- }
- this.metric = metric;
- }
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.measures;
+
+import com.google.common.collect.Multiset;
+import org.sonar.api.utils.KeyValueFormat;
+
+/**
+ * Format internal {@link com.google.common.collect.Multiset} of {@link CountDistributionBuilder}
+ * and {@link RangeDistributionBuilder}
+ */
+class MultisetDistributionFormat {
+
+ private MultisetDistributionFormat() {
+ // only statics
+ }
+
+ static String format(Multiset countBag) {
+ StringBuilder sb = new StringBuilder();
+ boolean first = true;
+ for (Object obj : countBag.elementSet()) {
+ if (!first) {
+ sb.append(KeyValueFormat.PAIR_SEPARATOR);
+ }
+ sb.append(obj.toString());
+ sb.append(KeyValueFormat.FIELD_SEPARATOR);
+ // -1 allows to include zero values
+ sb.append(countBag.count(obj) - 1);
+ first = false;
+ }
+ return sb.toString();
+ }
+}
*/
package org.sonar.api.measures;
+import com.google.common.base.Function;
+import com.google.common.collect.SortedMultiset;
+import com.google.common.collect.TreeMultiset;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
-import org.apache.commons.collections.SortedBag;
-import org.apache.commons.collections.Transformer;
-import org.apache.commons.collections.bag.TransformedSortedBag;
-import org.apache.commons.collections.bag.TreeBag;
import org.apache.commons.lang.NumberUtils;
import org.sonar.api.utils.KeyValueFormat;
-import org.sonar.api.utils.SonarException;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
/**
* Utility to build a distribution based on defined ranges
@Deprecated
public class RangeDistributionBuilder implements MeasureBuilder {
- private Metric<String> metric;
- private SortedBag countBag;
+ private final Metric<String> metric;
+ private final SortedMultiset countBag = TreeMultiset.create();
private boolean isEmpty = true;
private Number[] bottomLimits;
+ private RangeTransformer rangeValueTransformer;
private boolean isValid = true;
/**
* @param bottomLimits the bottom limits of ranges to be used
*/
public RangeDistributionBuilder(Metric<String> metric, Number[] bottomLimits) {
- setMetric(metric);
+ requireNonNull(metric, "Metric must not be null");
+ checkArgument(metric.isDataType(), "Metric %s must have data type", metric.key());
+ this.metric = metric;
init(bottomLimits);
}
+ public RangeDistributionBuilder(Metric<String> metric) {
+ this.metric = metric;
+ }
+
private void init(Number[] bottomLimits) {
this.bottomLimits = new Number[bottomLimits.length];
System.arraycopy(bottomLimits, 0, this.bottomLimits, 0, this.bottomLimits.length);
Arrays.sort(this.bottomLimits);
changeDoublesToInts();
- countBag = TransformedSortedBag.decorate(new TreeBag(), new RangeTransformer());
doClear();
+ this.rangeValueTransformer = new RangeTransformer();
}
private void changeDoublesToInts() {
}
}
- public RangeDistributionBuilder(Metric<String> metric) {
- this.metric = metric;
- }
-
/**
* Gives the bottom limits of ranges used
*
* @param count the number by which to increment
* @return the current object
*/
- public RangeDistributionBuilder add(Number value, int count) {
+ public RangeDistributionBuilder add(@Nullable Number value, int count) {
if (value != null && greaterOrEqualsThan(value, bottomLimits[0])) {
- this.countBag.add(value, count);
+ this.countBag.add(rangeValueTransformer.apply(value), count);
isEmpty = false;
}
return this;
private RangeDistributionBuilder addLimitCount(Number limit, int count) {
for (Number bottomLimit : bottomLimits) {
if (NumberUtils.compare(bottomLimit.doubleValue(), limit.doubleValue()) == 0) {
- this.countBag.add(limit, count);
+ this.countBag.add(rangeValueTransformer.apply(limit), count);
isEmpty = false;
return this;
}
}
private void doClear() {
- if (countBag != null) {
- countBag.clear();
- }
+ countBag.clear();
if (bottomLimits != null) {
Collections.addAll(countBag, bottomLimits);
}
*/
public Measure<String> build(boolean allowEmptyData) {
if (isValid && (!isEmpty || allowEmptyData)) {
- return new Measure<>(metric, KeyValueFormat.format(countBag, -1));
+ return new Measure<>(metric, MultisetDistributionFormat.format(countBag));
}
return null;
}
- private class RangeTransformer implements Transformer {
+ private class RangeTransformer implements Function<Number, Number> {
@Override
- public Object transform(Object o) {
- Number n = (Number) o;
+ public Number apply(Number n) {
for (int i = bottomLimits.length - 1; i >= 0; i--) {
if (greaterOrEqualsThan(n, bottomLimits[i])) {
return bottomLimits[i];
private static boolean greaterOrEqualsThan(Number n1, Number n2) {
return NumberUtils.compare(n1.doubleValue(), n2.doubleValue()) >= 0;
}
-
- private void setMetric(Metric metric) {
- if (metric == null || !metric.isDataType()) {
- throw new SonarException("Metric is null or has unvalid type");
- }
- this.metric = metric;
- }
}
*/
package org.sonar.api.profiles;
+import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
clone.setDefaultProfile(getDefaultProfile());
clone.setParentName(getParentName());
if (activeRules != null && !activeRules.isEmpty()) {
- clone.setActiveRules(new ArrayList<ActiveRule>(CollectionUtils.collect(activeRules, new Transformer() {
- @Override
- public Object transform(Object input) {
- return ((ActiveRule) input).clone();
- }
- })));
+ clone.setActiveRules(Lists.transform(activeRules, CloneFunction.INSTANCE));
}
return clone;
}
return input.getRule().equals(rule);
}
}
+
+ private enum CloneFunction implements Function<ActiveRule, ActiveRule> {
+ INSTANCE;
+ @Override
+ public ActiveRule apply(ActiveRule input) {
+ return (ActiveRule) input.clone();
+ }
+ }
}
*/
package org.sonar.api.rules;
-import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.collections.Transformer;
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.builder.ToStringBuilder;
-import org.sonar.api.profiles.RulesProfile;
-
-import javax.annotation.CheckForNull;
-
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
+import javax.annotation.CheckForNull;
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.sonar.api.profiles.RulesProfile;
public class ActiveRule implements Cloneable {
public Object clone() {
final ActiveRule clone = new ActiveRule(getRulesProfile(), getRule(), getSeverity());
clone.setInheritance(getInheritance());
- if (CollectionUtils.isNotEmpty(getActiveRuleParams())) {
- clone.setActiveRuleParams(new ArrayList<ActiveRuleParam>(CollectionUtils.collect(getActiveRuleParams(), new Transformer() {
+ if (activeRuleParams != null && !activeRuleParams.isEmpty()) {
+ clone.setActiveRuleParams(Lists.transform(activeRuleParams, new Function<ActiveRuleParam, ActiveRuleParam>() {
@Override
- public Object transform(Object input) {
- ActiveRuleParam activeRuleParamClone = (ActiveRuleParam) ((ActiveRuleParam) input).clone();
+ public ActiveRuleParam apply(ActiveRuleParam input) {
+ ActiveRuleParam activeRuleParamClone = (ActiveRuleParam) input.clone();
activeRuleParamClone.setActiveRule(clone);
return activeRuleParamClone;
}
- })));
+ }));
}
return clone;
}
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import org.apache.commons.collections.Bag;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.sonar.api.rules.RulePriority;
return format(map, newStringConverter(), newIntegerConverter());
}
- /**
- * @since 1.11
- * @deprecated use Multiset from google collections instead of commons-collections bags
- */
- @Deprecated
- public static String format(Bag bag, int var) {
- StringBuilder sb = new StringBuilder();
- if (bag != null) {
- boolean first = true;
- for (Object obj : bag.uniqueSet()) {
- if (!first) {
- sb.append(PAIR_SEPARATOR);
- }
- sb.append(obj.toString());
- sb.append(FIELD_SEPARATOR);
- sb.append(bag.getCount(obj) + var);
- first = false;
- }
- }
- return sb.toString();
- }
}
public void buildIntegerDistribution() {
RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, new Integer[] {0, 2, 4});
Measure measure = builder
- .add(3.2)
- .add(2.0)
- .add(6.2)
- .build();
+ .add(3.2)
+ .add(2.0)
+ .add(6.2)
+ .build();
assertThat(measure.getData()).isEqualTo("0=0;2=2;4=1");
}
public void buildDoubleDistribution() {
RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, new Double[] {0.0, 2.0, 4.0});
Measure measure = builder
- .add(3.2)
- .add(2.0)
- .add(6.2)
- .build();
+ .add(3.2)
+ .add(2.0)
+ .add(6.2)
+ .build();
assertThat(measure.getData()).isEqualTo("0=0;2=2;4=1");
}
public void valueLesserThanMinimumIsIgnored() {
RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, new Integer[] {0, 2, 4});
Measure measure = builder
- .add(3.2)
- .add(2.0)
- .add(-3.0)
- .build();
+ .add(3.2)
+ .add(2.0)
+ .add(-3.0)
+ .build();
assertThat(measure.getData()).isEqualTo("0=0;2=2;4=0");
}
RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, new Integer[] {0, 2});
builder.clear();
Measure measure = builder
- .add(1)
- .add(measureToAdd)
- .build();
+ .add(1)
+ .add(measureToAdd)
+ .build();
assertThat(measure.getData()).isEqualTo("0=4;2=5");
}
RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, new Integer[] {0, 2, 4});
builder.clear();
Measure measure = builder
- .add(1)
- .add(measureToAdd)
- .build();
+ .add(1)
+ .add(measureToAdd)
+ .build();
assertThat(measure).isNull();
}
RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, new Double[] {0.0, 2.0, 4.0});
builder.clear();
Measure measure = builder
- .add(measureToAdd)
- .build();
+ .add(measureToAdd)
+ .build();
assertThat(measure).isNull();
}
RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION);
builder.clear();
Measure measure = builder
- .add(m1)
- .add(m2)
- .build();
+ .add(m1)
+ .add(m2)
+ .build();
assertThat(measure.getData()).isEqualTo("0.5=3;3.5=7;6.5=10");
}
RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION);
builder.clear();
Measure measure = builder
- .add(m1)
- .add(m2)
- .build();
+ .add(m1)
+ .add(m2)
+ .build();
assertThat(measure.getData()).isEqualTo("0=3;3=7;6=10");
}
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
-import org.apache.commons.collections.bag.TreeBag;
import org.junit.Test;
import org.sonar.api.rules.RulePriority;
import org.sonar.test.TestUtils;
TestUtils.hasOnlyPrivateConstructors(KeyValueFormat.class);
}
- @Test
- public void formatBag() {
- TreeBag bag = new TreeBag();
- bag.add("hello", 1);
- bag.add("key", 3);
- assertThat(KeyValueFormat.format(bag, 0)).isEqualTo("hello=1;key=3");
- }
-
- @Test
- public void formatBagWithVariationHack() {
- TreeBag bag = new TreeBag();
- bag.add("hello", 1);
- bag.add("key", 3);
- assertThat(KeyValueFormat.format(bag, -1)).isEqualTo("hello=0;key=2");
- }
-
}