Skip to content

Commit

Permalink
Create BiSumCounter to do a sum on 2 metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Jul 15, 2015
1 parent 2d71043 commit 698e995
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 40 deletions.
@@ -0,0 +1,57 @@
/*
* 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;

/**
* This counter can be used to aggregate measure from two metrics
*/
public class BiSumCounter implements Counter<BiSumCounter> {

private final SumCounter sumCounter1;
private final SumCounter sumCounter2;

public BiSumCounter(String metric1, String metric2) {
this.sumCounter1 = new SumCounter(metric1);
this.sumCounter2 = new SumCounter(metric2);
}

@Override
public void aggregate(BiSumCounter counter) {
sumCounter1.aggregate(counter.sumCounter1);
sumCounter2.aggregate(counter.sumCounter2);
}

@Override
public void aggregate(FileAggregateContext context) {
sumCounter1.aggregate(context);
sumCounter2.aggregate(context);
}

public Optional<Integer> getValue1() {
return sumCounter1.getValue();
}

public Optional<Integer> getValue2() {
return sumCounter2.getValue();
}
}
@@ -0,0 +1,66 @@
/*
* 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 org.sonar.server.computation.measure.Measure;

/**
* Simple counter that do the sum of an integer measure
*/
public class SumCounter implements Counter<SumCounter> {

private final String metricKey;

private int value = 0;
private boolean initialized = false;

public SumCounter(String metricKey) {
this.metricKey = metricKey;
}

@Override
public void aggregate(SumCounter counter) {
if (counter.getValue().isPresent()) {
addValue(counter.getValue().get());
}
}

@Override
public void aggregate(FileAggregateContext context) {
Optional<Measure> measureOptional = context.getMeasure(metricKey);
if (measureOptional.isPresent()) {
addValue(measureOptional.get().getIntValue());
}
}

private void addValue(int newValue) {
initialized = true;
value += newValue;
}

public Optional<Integer> getValue() {
if (initialized) {
return Optional.of(value);
}
return Optional.absent();
}
}
Expand Up @@ -26,7 +26,7 @@

import static java.util.Objects.requireNonNull;

public class SumFormula implements Formula<SumFormula.SumCounter> {
public class SumFormula implements Formula<SumCounter> {

private final String metricKey;

Expand All @@ -36,7 +36,7 @@ public SumFormula(String metricKey) {

@Override
public SumCounter createNewCounter() {
return new SumCounter();
return new SumCounter(metricKey);
}

@Override
Expand All @@ -54,36 +54,4 @@ public String[] getOutputMetricKeys() {
return new String[] {metricKey};
}

class SumCounter implements Counter<SumCounter> {

private int value = 0;
private boolean initialized = false;

@Override
public void aggregate(SumCounter counter) {
if (counter.getValue().isPresent()) {
addValue(counter.getValue().get());
}
}

@Override
public void aggregate(FileAggregateContext context) {
Optional<Measure> measureOptional = context.getMeasure(metricKey);
if (measureOptional.isPresent()) {
addValue(measureOptional.get().getIntValue());
}
}

private void addValue(int newValue) {
initialized = true;
value += newValue;
}

public Optional<Integer> getValue() {
if (initialized) {
return Optional.of(value);
}
return Optional.absent();
}
}
}
@@ -0,0 +1,92 @@
/*
* 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 org.junit.Test;
import org.sonar.server.computation.measure.Measure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BiSumCounterTest {

private final static String METRIC_KEY_1 = "metric1";
private final static String METRIC_KEY_2 = "metric2";

FileAggregateContext fileAggregateContext = mock(FileAggregateContext.class);

BiSumCounter sumCounter = new BiSumCounter(METRIC_KEY_1, METRIC_KEY_2);

@Test
public void no_value_when_no_aggregation() {
assertThat(sumCounter.getValue1()).isAbsent();
assertThat(sumCounter.getValue2()).isAbsent();
}

@Test
public void aggregate_from_context() {
when(fileAggregateContext.getMeasure(METRIC_KEY_1)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
when(fileAggregateContext.getMeasure(METRIC_KEY_2)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(20)));

sumCounter.aggregate(fileAggregateContext);

assertThat(sumCounter.getValue1().get()).isEqualTo(10);
assertThat(sumCounter.getValue2().get()).isEqualTo(20);
}

@Test
public void no_value_when_aggregate_from_context_but_no_measure() {
when(fileAggregateContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());

sumCounter.aggregate(fileAggregateContext);

assertThat(sumCounter.getValue1()).isAbsent();
assertThat(sumCounter.getValue2()).isAbsent();
}

@Test
public void aggregate_from_counter() {
when(fileAggregateContext.getMeasure(METRIC_KEY_1)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
when(fileAggregateContext.getMeasure(METRIC_KEY_2)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(20)));
BiSumCounter anotherCounter = new BiSumCounter(METRIC_KEY_1, METRIC_KEY_2);
anotherCounter.aggregate(fileAggregateContext);

sumCounter.aggregate(anotherCounter);

assertThat(sumCounter.getValue1().get()).isEqualTo(10);
assertThat(sumCounter.getValue2().get()).isEqualTo(20);
}

@Test
public void no_value_when_aggregate_from_empty_aggregator() {
BiSumCounter anotherCounter = new BiSumCounter(METRIC_KEY_1, METRIC_KEY_2);

sumCounter.aggregate(anotherCounter);

assertThat(sumCounter.getValue1()).isAbsent();
assertThat(sumCounter.getValue2()).isAbsent();
}

}
@@ -0,0 +1,84 @@
/*
* 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 org.junit.Test;
import org.sonar.server.computation.measure.Measure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class SumCounterTest {

private final static String METRIC_KEY = "metric";

FileAggregateContext fileAggregateContext = mock(FileAggregateContext.class);

SumCounter sumCounter = new SumCounter(METRIC_KEY);

@Test
public void no_value_when_no_aggregation() {
assertThat(sumCounter.getValue()).isAbsent();
}

@Test
public void aggregate_from_context() {
when(fileAggregateContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));

sumCounter.aggregate(fileAggregateContext);

assertThat(sumCounter.getValue().get()).isEqualTo(10);
}

@Test
public void no_value_when_aggregate_from_context_but_no_measure() {
when(fileAggregateContext.getMeasure(anyString())).thenReturn(Optional.<Measure>absent());

sumCounter.aggregate(fileAggregateContext);

assertThat(sumCounter.getValue()).isAbsent();
}

@Test
public void aggregate_from_counter() {
when(fileAggregateContext.getMeasure(METRIC_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create(10)));
SumCounter anotherCounter = new SumCounter(METRIC_KEY);
anotherCounter.aggregate(fileAggregateContext);

sumCounter.aggregate(anotherCounter);

assertThat(sumCounter.getValue().get()).isEqualTo(10);
}

@Test
public void no_value_when_aggregate_from_empty_aggregator() {
SumCounter anotherCounter = new SumCounter(METRIC_KEY);

sumCounter.aggregate(anotherCounter);

assertThat(sumCounter.getValue()).isAbsent();
}

}

0 comments on commit 698e995

Please sign in to comment.