@Test public void build_integer_distribution() { RangeDistributionBuilder builder = new RangeDistributionBuilder(new Integer[] {0, 2, 4}); String data = builder .add(3.2) .add(2.0) .add(6.2) .build(); assertThat(data).isEqualTo("0=0;2=2;4=1"); }
/** * 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 * <br> * The returned distribution will be invalidated in case the given value does not use the same bottom limits * * @param data the data to add to the current one */ public RangeDistributionBuilder add(String data) { Map<Double, Double> map = KeyValueFormat.parse(data, KeyValueFormat.newDoubleConverter(), KeyValueFormat.newDoubleConverter()); if (bottomLimits == null) { Number[] limits = map.keySet().toArray(new Number[map.size()]); 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; }
/** * Increments an entry * * @param value the value to use to pick the entry to increment * @param count the number by which to increment */ public RangeDistributionBuilder add(Number value, int count) { if (greaterOrEqualsThan(value, bottomLimits[0])) { addValue(value, count); isEmpty = false; } return this; }
/** * Increments an entry by 1 * * @param value the value to use to pick the entry to increment */ public RangeDistributionBuilder add(Number value) { return add(value, 1); }
@Test public void aggregate_empty_distribution() { RangeDistributionBuilder builder = new RangeDistributionBuilder(); String distribution = builder.build(); assertThat(distribution).isEmpty(); }
public Optional<String> getValue() { if (initialized) { return Optional.ofNullable(distribution.build()); } return Optional.empty(); } }
@Test public void is_empty_is_true_when_no_data_on_distribution_with_limits() { RangeDistributionBuilder builder = new RangeDistributionBuilder(new Integer[] {4, 2, 0}); assertThat(builder.isEmpty()).isTrue(); }
public FileMeasures(Number[] limitsComplexityFunctions, Number[] filesDistributionBottomLimits) { functionNumber = 0; classNumber = 0; statementNumber = 0; classComplexity = 0; functionComplexity = 0; functionComplexityDistribution = new RangeDistributionBuilder(limitsComplexityFunctions); fileComplexityDistribution = new RangeDistributionBuilder(filesDistributionBottomLimits); }
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; }
@Override public void aggregate(DistributionCounter counter) { Optional<String> value = counter.getValue(); if (value.isPresent()) { initialized = true; distribution.add(value.get()); } }
public Optional<String> getValue() { if (initialized) { return Optional.fromNullable(distribution.build()); } return Optional.absent(); } }
@Test public void is_empty_is_true_when_no_data() { RangeDistributionBuilder builder = new RangeDistributionBuilder(); assertThat(builder.isEmpty()).isTrue(); }
public FileMeasures(Number[] limitsComplexityFunctions, Number[] filesDistributionBottomLimits) { functionNumber = 0; classNumber = 0; statementNumber = 0; classComplexity = 0; functionComplexity = 0; functionComplexityDistribution = new RangeDistributionBuilder(limitsComplexityFunctions); fileComplexityDistribution = new RangeDistributionBuilder(filesDistributionBottomLimits); }
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; }
@Test public void value_lesser_than_minimum_is_ignored() { RangeDistributionBuilder builder = new RangeDistributionBuilder(new Integer[] {0, 2, 4}); String data = builder .add(3.2) .add(2.0) .add(-3.0) .build(); assertThat(data).isEqualTo("0=0;2=2;4=0"); }
@Override public void initialize(CounterInitializationContext context) { Optional<Measure> measureOptional = context.getMeasure(metricKey); String data = measureOptional.isPresent() ? measureOptional.get().getData() : null; if (data != null) { initialized = true; distribution.add(data); } }
@Test public void test() { String filename = "metrics/lines_of_code.php"; PhpFile file = FileTestUtils.getFile(new File("src/test/resources/"+filename)); FileLinesContext fileLinesContext = mock(FileLinesContext.class); FileMeasures fileMeasures = new MetricsVisitor().getFileMeasures(file, parse(filename), fileLinesContext); assertThat(fileMeasures.getFileComplexity()).isEqualTo(1); assertThat(fileMeasures.getFileCognitiveComplexity()).isEqualTo(0); assertThat(fileMeasures.getClassComplexity()).isEqualTo(1); assertThat(fileMeasures.getFunctionComplexity()).isEqualTo(1); assertThat(fileMeasures.getFileComplexityDistribution().build()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0"); assertThat(fileMeasures.getFunctionComplexityDistribution().build()).isEqualTo("1=1;2=0;4=0;6=0;8=0;10=0;12=0"); assertThat(fileMeasures.getFunctionNumber()).isEqualTo(1); assertThat(fileMeasures.getStatementNumber()).isEqualTo(2); assertThat(fileMeasures.getClassNumber()).isEqualTo(1); assertThat(fileMeasures.getLinesOfCodeNumber()).isEqualTo(7); assertThat(fileMeasures.getCommentLinesNumber()).isEqualTo(5); verify(fileLinesContext).setIntValue(CoreMetrics.EXECUTABLE_LINES_DATA_KEY, 21, 1); }
/** * 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 * <br> * The returned distribution will be invalidated in case the given value does not use the same bottom limits * * @param data the data to add to the current one */ public RangeDistributionBuilder add(String data) { Map<Double, Double> map = KeyValueFormat.parse(data, KeyValueFormat.newDoubleConverter(), KeyValueFormat.newDoubleConverter()); if (bottomLimits == null) { Number[] limits = map.keySet().toArray(new Number[map.size()]); 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; }
/** * Increments an entry * * @param value the value to use to pick the entry to increment * @param count the number by which to increment */ public RangeDistributionBuilder add(Number value, int count) { if (greaterOrEqualsThan(value, bottomLimits[0])) { addValue(value, count); isEmpty = false; } return this; }
@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(); assertThat(data).isEqualTo("0=3;3=7;6=10"); }