Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create BiSumCounter to do a sum on 2 metrics
- Loading branch information
1 parent
2d71043
commit 698e995
Showing
6 changed files
with
307 additions
and
40 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
server/sonar-server/src/main/java/org/sonar/server/computation/formula/BiSumCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
server/sonar-server/src/main/java/org/sonar/server/computation/formula/SumCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
server/sonar-server/src/test/java/org/sonar/server/computation/formula/BiSumCounterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
server/sonar-server/src/test/java/org/sonar/server/computation/formula/SumCounterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
Oops, something went wrong.