aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/pom.xml4
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/CountDistributionBuilder.java29
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/MultisetDistributionFormat.java50
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/RangeDistributionBuilder.java54
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java19
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java24
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java22
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/RangeDistributionBuilderTest.java52
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java17
9 files changed, 133 insertions, 138 deletions
diff --git a/sonar-plugin-api/pom.xml b/sonar-plugin-api/pom.xml
index bde39416b19..53510bfaf9a 100644
--- a/sonar-plugin-api/pom.xml
+++ b/sonar-plugin-api/pom.xml
@@ -36,10 +36,6 @@
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- </dependency>
- <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CountDistributionBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CountDistributionBuilder.java
index d85ea3c4cd6..379d1e866db 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CountDistributionBuilder.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CountDistributionBuilder.java
@@ -19,9 +19,9 @@
*/
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;
@@ -36,9 +36,8 @@ import org.sonar.api.utils.SonarException;
*/
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
@@ -46,8 +45,10 @@ public class CountDistributionBuilder implements MeasureBuilder {
* @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;
}
/**
@@ -62,8 +63,8 @@ public class CountDistributionBuilder implements MeasureBuilder {
addZero(value);
} else {
- if (this.countBag.add(value, count)) {
- //hack
+ if (this.countBag.add(value, count) == 0) {
+ // hack
this.countBag.add(value, 1);
}
}
@@ -152,16 +153,8 @@ public class CountDistributionBuilder implements MeasureBuilder {
*/
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;
- }
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/MultisetDistributionFormat.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MultisetDistributionFormat.java
new file mode 100644
index 00000000000..de931c3286d
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MultisetDistributionFormat.java
@@ -0,0 +1,50 @@
+/*
+ * 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();
+ }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/RangeDistributionBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/RangeDistributionBuilder.java
index 592788a18a6..5c20bb25dcc 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/RangeDistributionBuilder.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/RangeDistributionBuilder.java
@@ -19,18 +19,19 @@
*/
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
@@ -44,10 +45,11 @@ import org.sonar.api.utils.SonarException;
@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;
/**
@@ -58,17 +60,23 @@ public class RangeDistributionBuilder implements MeasureBuilder {
* @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() {
@@ -85,10 +93,6 @@ public class RangeDistributionBuilder implements MeasureBuilder {
}
}
- public RangeDistributionBuilder(Metric<String> metric) {
- this.metric = metric;
- }
-
/**
* Gives the bottom limits of ranges used
*
@@ -115,9 +119,9 @@ public class RangeDistributionBuilder implements MeasureBuilder {
* @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;
@@ -126,7 +130,7 @@ public class RangeDistributionBuilder implements MeasureBuilder {
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;
}
@@ -189,9 +193,7 @@ public class RangeDistributionBuilder implements MeasureBuilder {
}
private void doClear() {
- if (countBag != null) {
- countBag.clear();
- }
+ countBag.clear();
if (bottomLimits != null) {
Collections.addAll(countBag, bottomLimits);
}
@@ -223,15 +225,14 @@ public class RangeDistributionBuilder implements MeasureBuilder {
*/
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];
@@ -244,11 +245,4 @@ public class RangeDistributionBuilder implements MeasureBuilder {
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;
- }
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java
index 72263f21ec7..ef6a3fdfbba 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java
@@ -19,14 +19,14 @@
*/
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;
@@ -340,12 +340,7 @@ public class RulesProfile implements Cloneable {
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;
}
@@ -375,4 +370,12 @@ public class RulesProfile implements Cloneable {
return input.getRule().equals(rule);
}
}
+
+ private enum CloneFunction implements Function<ActiveRule, ActiveRule> {
+ INSTANCE;
+ @Override
+ public ActiveRule apply(ActiveRule input) {
+ return (ActiveRule) input.clone();
+ }
+ }
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java
index 69b3231ce61..80f219d61d9 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java
@@ -19,17 +19,15 @@
*/
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 {
@@ -289,15 +287,15 @@ 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;
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java
index cc21a93c8be..5eb2737f108 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java
@@ -27,7 +27,6 @@ import java.util.LinkedHashMap;
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;
@@ -440,25 +439,4 @@ public final class KeyValueFormat {
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();
- }
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/RangeDistributionBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/RangeDistributionBuilderTest.java
index 3b2e248214e..4d9bd1afed3 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/RangeDistributionBuilderTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/RangeDistributionBuilderTest.java
@@ -43,10 +43,10 @@ public class RangeDistributionBuilderTest {
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");
}
@@ -55,10 +55,10 @@ public class RangeDistributionBuilderTest {
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");
}
@@ -67,10 +67,10 @@ public class RangeDistributionBuilderTest {
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");
}
@@ -83,9 +83,9 @@ public class RangeDistributionBuilderTest {
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");
}
@@ -98,9 +98,9 @@ public class RangeDistributionBuilderTest {
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();
}
@@ -113,8 +113,8 @@ public class RangeDistributionBuilderTest {
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();
}
@@ -130,9 +130,9 @@ public class RangeDistributionBuilderTest {
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");
}
@@ -148,9 +148,9 @@ public class RangeDistributionBuilderTest {
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");
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java
index a7fe6965824..c7d3e529d53 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java
@@ -26,7 +26,6 @@ import java.util.Date;
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;
@@ -236,20 +235,4 @@ public class KeyValueFormatTest {
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");
- }
-
}