diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-08-27 16:05:18 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-08-31 09:52:02 +0200 |
commit | 48835a873353be8fdd48ca57313f6a74ddaf15f7 (patch) | |
tree | 4b6a16d3fd0b8822dc18d831b5ec027bf6962507 /server | |
parent | a2a6a50c88c58a0af3daa52e39e76d362078f547 (diff) | |
download | sonarqube-48835a873353be8fdd48ca57313f6a74ddaf15f7.tar.gz sonarqube-48835a873353be8fdd48ca57313f6a74ddaf15f7.zip |
SONAR-6730 Move RangeDistrubtionBuilder to API and allow constructor to take bottom limits
Diffstat (limited to 'server')
3 files changed, 2 insertions, 296 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/DistributionFormula.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/DistributionFormula.java index cdeca703326..a2b3b4745b0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/DistributionFormula.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/DistributionFormula.java @@ -21,6 +21,7 @@ package org.sonar.server.computation.formula; import com.google.common.base.Optional; +import org.sonar.api.ce.measure.RangeDistributionBuilder; import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.CrawlerDepthLimit; import org.sonar.server.computation.measure.Measure; @@ -81,7 +82,7 @@ public class DistributionFormula implements Formula<DistributionFormula.Distribu public Optional<String> getValue() { if (initialized) { - return distribution.build(); + return Optional.fromNullable(distribution.build()); } return Optional.absent(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/RangeDistributionBuilder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/RangeDistributionBuilder.java deleted file mode 100644 index a78418ace64..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/RangeDistributionBuilder.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.server.computation.formula; - -import com.google.common.base.Optional; -import com.google.common.collect.Multiset; -import com.google.common.collect.TreeMultiset; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import org.sonar.api.utils.KeyValueFormat; - -/** - * Utility to build a distribution based on defined ranges - * <p/> - * <p>An example of usage : you wish to record the percentage of lines of code that belong to method - * with pre-defined ranges of complexity.</p> - * - */ -public class RangeDistributionBuilder { - - private Multiset<Number> distributionSet; - private boolean isEmpty = true; - private Number[] bottomLimits; - private boolean isValid = true; - - /** - * Adds an existing Distribution to the current one. - * It will create the entries if they don't exist. - * Can be used to add the values of children resources for example - * <p/> - * Since 2.2, the distribution returned will be invalidated in case the - * measure given does not use the same bottom limits - * - * @param data the data to add to the current one - * @return the current object - */ - public RangeDistributionBuilder add(String data) { - Map<Double, Double> map = KeyValueFormat.parse(data, KeyValueFormat.newDoubleConverter(), KeyValueFormat.newDoubleConverter()); - Number[] limits = map.keySet().toArray(new Number[map.size()]); - if (bottomLimits == null) { - init(limits); - - } else if (!areSameLimits(bottomLimits, map.keySet())) { - isValid = false; - } - - if (isValid) { - for (Map.Entry<Double, Double> entry : map.entrySet()) { - addLimitCount(entry.getKey(), entry.getValue().intValue()); - } - } - return this; - } - - 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(); - distributionSet = TreeMultiset.create(NumberComparator.INSTANCE); - } - - private void changeDoublesToInts() { - boolean onlyInts = true; - for (Number bottomLimit : bottomLimits) { - if (NumberComparator.INSTANCE.compare(bottomLimit.intValue(), bottomLimit.doubleValue()) != 0) { - onlyInts = false; - } - } - if (onlyInts) { - for (int i = 0; i < bottomLimits.length; i++) { - bottomLimits[i] = bottomLimits[i].intValue(); - } - } - } - - private static boolean areSameLimits(Number[] bottomLimits, Set<Double> limits) { - if (limits.size() == bottomLimits.length) { - for (Number l : bottomLimits) { - if (!limits.contains(l.doubleValue())) { - return false; - } - } - return true; - } - return false; - } - - private RangeDistributionBuilder addLimitCount(Number limit, int count) { - for (Number bottomLimit : bottomLimits) { - if (NumberComparator.INSTANCE.compare(bottomLimit.doubleValue(), limit.doubleValue()) == 0) { - addValue(limit, count); - isEmpty = false; - return this; - } - } - isValid = false; - return this; - } - - private void addValue(Number value, int count) { - for (int i = bottomLimits.length - 1; i >= 0; i--) { - if (greaterOrEqualsThan(value, bottomLimits[i])) { - this.distributionSet.add(bottomLimits[i], count); - return; - } - } - } - - /** - * @return whether the current object is empty or not - */ - public boolean isEmpty() { - return isEmpty; - } - - /** - * Used to build a measure from the current object - * - * @return the built measure - */ - public Optional<String> build() { - if (isValid) { - return Optional.of(KeyValueFormat.format(toMap())); - } - return Optional.absent(); - } - - private Map<Number, Integer> toMap() { - if (bottomLimits == null || bottomLimits.length == 0) { - return Collections.emptyMap(); - } - Map<Number, Integer> map = new TreeMap<>(); - for (int i = 0; i < bottomLimits.length; i++) { - Number value = bottomLimits[i]; - map.put(value, distributionSet.count(value)); - } - return map; - } - - private static boolean greaterOrEqualsThan(Number n1, Number n2) { - return NumberComparator.INSTANCE.compare(n1, n2) >= 0; - } - - private enum NumberComparator implements Comparator<Number> { - INSTANCE; - - @Override - public int compare(Number n1, Number n2) { - return ((Double) n1.doubleValue()).compareTo(n2.doubleValue()); - } - } - -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/RangeDistributionBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/RangeDistributionBuilderTest.java deleted file mode 100644 index ea8dfbc7900..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/RangeDistributionBuilderTest.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.server.computation.formula; - -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class RangeDistributionBuilderTest { - - @Test - public void build_integer_distribution() { - RangeDistributionBuilder builder = new RangeDistributionBuilder(); - String data = builder - .add("0=0;2=2;4=1") - .add("0=1;2=2;4=2") - .build().get(); - - assertThat(data).isEqualTo("0=1;2=4;4=3"); - } - - @Test - public void build_double_distribution() { - RangeDistributionBuilder builder = new RangeDistributionBuilder(); - String data = builder - .add("0.5=0;1.9=2;4.5=1") - .add("0.5=1;1.9=3;4.5=1") - .build().get(); - - assertThat(data).isEqualTo("0.5=1;1.9=5;4.5=2"); - } - - @Test - public void add_distribution_measure_with_identical_limits() { - RangeDistributionBuilder builder = new RangeDistributionBuilder(); - String data = builder - .add("0=1;2=0") - .add("0=3;2=5") - .build().get(); - - assertThat(data).isEqualTo("0=4;2=5"); - } - - @Test - public void add_distribution_measure_with_different_int_limits() { - RangeDistributionBuilder builder = new RangeDistributionBuilder(); - - assertThat(builder - .add("0=1") - .add("0=3;2=5") - .build().isPresent()).isFalse(); - } - - @Test - public void add_distribution_measure_with_different_double_limits() { - RangeDistributionBuilder builder = new RangeDistributionBuilder(); - - assertThat(builder - .add("0.0=3;3.0=5") - .add("0.0=3;3.0=5;6.0=9") - .build().isPresent()).isFalse(); - } - - @Test - public void init_limits_at_the_first_add() { - RangeDistributionBuilder builder = new RangeDistributionBuilder(); - String data = builder - .add("0.5=3;3.5=5;6.5=9") - .add("0.5=0;3.5=2;6.5=1") - .build().get(); - - assertThat(data).isEqualTo("0.5=3;3.5=7;6.5=10"); - } - - @Test - public void keep_int_ranges_when_merging_distributions() { - RangeDistributionBuilder builder = new RangeDistributionBuilder(); - String data = builder - .add("0=3;3=5;6=9") - .add("0=0;3=2;6=1") - .build().get(); - - assertThat(data).isEqualTo("0=3;3=7;6=10"); - } - - @Test - public void return_empty_string_when_empty_data() { - RangeDistributionBuilder builder = new RangeDistributionBuilder(); - - assertThat(builder.isEmpty()).isTrue(); - assertThat(builder.build().get()).isEmpty(); - } - - @Test - public void aggregate_empty_distribution() { - RangeDistributionBuilder builder = new RangeDistributionBuilder(); - String distribution = builder.build().get(); - assertThat(distribution).isEmpty(); - } - -} |