forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
calculating Moving averages - review fixes
- Loading branch information
Jagath Kumar
committed
Apr 17, 2024
1 parent
80f0ac6
commit ac32eeb
Showing
11 changed files
with
66 additions
and
14 deletions.
There are no files selected for viewing
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
...thms-miscellaneous-9/src/main/java/com/baeldung/MovingAverageWithStreamBasedApproach.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,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(); | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...ung/ExponentialMovingAverageUnitTest.java → ...ung/ExponentialMovingAverageUnitTest.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
1 change: 0 additions & 1 deletion
1
...ovingAverageByCircularBufferUnitTest.java → ...ovingAverageByCircularBufferUnitTest.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
1 change: 0 additions & 1 deletion
1
...AverageWithApacheCommonsMathUnitTest.java → ...AverageWithApacheCommonsMathUnitTest.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
38 changes: 38 additions & 0 deletions
38
...cellaneous-9/src/test/java/com/baeldung/MovingAverageWithStreamBasedApproachUnitTest.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,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); | ||
} | ||
} |
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
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