calculating Moving averages - review fixes

This commit is contained in:
Jagath Kumar 2024-04-08 17:03:26 +05:30
parent 80f0ac6b10
commit ac32eebb53
11 changed files with 66 additions and 14 deletions

View File

@ -3,19 +3,18 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-calculating-moving-averages</artifactId>
<name>core-java-calculating-moving-averages</name>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<groupId>com.baeldung</groupId>
<artifactId>algorithms-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>algorithms-miscellaneous-9</artifactId>
<properties>
<commons-math3.version>3.6.1</commons-math3.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>

View File

@ -0,0 +1,18 @@
package com.baeldung;
import java.util.stream.DoubleStream;
public class MovingAverageWithStreamBasedApproach {
private int windowSize;
public MovingAverageWithStreamBasedApproach(int windowSize) {
this.windowSize = windowSize;
}
public double calculateAverage(double[] data) {
return DoubleStream.of(data)
.skip(Math.max(0, data.length - windowSize))
.limit(Math.min(data.length, windowSize))
.summaryStatistics()
.getAverage();
}
}

View File

@ -1,8 +1,7 @@
package com.baeldung;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class ExponentialMovingAverageUnitTest {

View File

@ -1,7 +1,6 @@
package com.baeldung;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MovingAverageByCircularBufferUnitTest {

View File

@ -1,7 +1,6 @@
package com.baeldung;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MovingAverageWithApacheCommonsMathUnitTest {

View File

@ -0,0 +1,38 @@
package com.baeldung;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.*;
public class MovingAverageWithStreamBasedApproachUnitTest {
@Test
public void whenEmptyDataIsPassed_shouldReturnZero() {
double[] data = {};
int windowSize = 3;
double expectedAverage = 0;
MovingAverageWithStreamBasedApproach calculator = new MovingAverageWithStreamBasedApproach(windowSize);
double actualAverage = calculator.calculateAverage(data);
assertEquals(expectedAverage, actualAverage);
}
@Test
public void whenValidDataIsPassed_shouldReturnCorrectAverage() {
double[] data = {10, 20, 30, 40, 50};
int windowSize = 3;
double expectedAverage = 40;
MovingAverageWithStreamBasedApproach calculator = new MovingAverageWithStreamBasedApproach(windowSize);
double actualAverage = calculator.calculateAverage(data);
assertEquals(expectedAverage, actualAverage);
}
@Test
public void whenValidDataIsPassedWithLongerWindowSize_shouldReturnCorrectAverage() {
double[] data = {10, 20, 30, 40, 50};
int windowSize = 5;
double expectedAverage = 30;
MovingAverageWithStreamBasedApproach calculator = new MovingAverageWithStreamBasedApproach(windowSize);
double actualAverage = calculator.calculateAverage(data);
assertEquals(expectedAverage, actualAverage);
}
}

View File

@ -26,6 +26,7 @@
<module>algorithms-searching</module>
<module>algorithms-sorting</module>
<module>algorithms-sorting-2</module>
<module>algorithms-miscellaneous-9</module>
</modules>
<properties>

View File

@ -224,7 +224,6 @@
<module>java-rmi</module>
<module>java-spi</module>
<module>java-websocket</module>
<module>core-java-calculating-moving-averages</module>
</modules>
<dependencies>