Skip to content

Commit

Permalink
calculating Moving averages - review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagath Kumar committed Apr 17, 2024
1 parent 80f0ac6 commit ac32eeb
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.baeldung;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class ExponentialMovingAverageUnitTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.baeldung;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MovingAverageByCircularBufferUnitTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.baeldung;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MovingAverageWithApacheCommonsMathUnitTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions algorithms-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<module>algorithms-searching</module>
<module>algorithms-sorting</module>
<module>algorithms-sorting-2</module>
<module>algorithms-miscellaneous-9</module>
</modules>

<properties>
Expand Down
1 change: 0 additions & 1 deletion core-java-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit ac32eeb

Please sign in to comment.