Skip to content

Commit

Permalink
Misc Cleanups (#66)
Browse files Browse the repository at this point in the history
+ Nullability annotations in Gatherers4j.java
+ Minimum windowSize is 1 in a lot of places, not zero.
+ Add constructor to GroupingByGatherer and handle null mapping function to make nullability checks happier
+ Parameterize tests where multiple independent assertions were taking place
  • Loading branch information
tginsberg authored Dec 27, 2024
1 parent 789f9c6 commit 7f26f56
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class BigDecimalMovingProductGatherer<INPUT extends @Nullable Object>
final Function<INPUT, @Nullable BigDecimal> mappingFunction,
final int windowSize) {
super(mappingFunction);
if (windowSize <= 0) {
throw new IllegalArgumentException("Window size must be positive");
if (windowSize <= 1) {
throw new IllegalArgumentException("Window size must be greater than 1");
}
this.windowSize = windowSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class BigDecimalMovingSumGatherer<INPUT extends @Nullable Object>
final Function<INPUT, @Nullable BigDecimal> mappingFunction,
final int windowSize) {
super(mappingFunction);
if (windowSize <= 0) {
throw new IllegalArgumentException("Window size must be positive");
if (windowSize <= 1) {
throw new IllegalArgumentException("Window size must be greater than 1");
}
this.windowSize = windowSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public final class BigDecimalSimpleMovingAverageGatherer<INPUT extends @Nullable
final int windowSize
) {
super(mappingFunction);
if (windowSize <= 0) {
throw new IllegalArgumentException("Window size must be positive");
if (windowSize <= 1) {
throw new IllegalArgumentException("Window size must be greater than 1");
}
this.windowSize = windowSize;
}
Expand Down
150 changes: 99 additions & 51 deletions src/main/java/com/ginsberg/gatherers4j/Gatherers4j.java

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions src/main/java/com/ginsberg/gatherers4j/GroupingByGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
public class GroupingByGatherer<INPUT extends @Nullable Object> implements
Gatherer<INPUT, GroupingByGatherer.State<INPUT>, List<INPUT>> {

private final Function<INPUT, Object> mappingFunction;
private final @Nullable Function<INPUT, Object> mappingFunction;

GroupingByGatherer(Function<INPUT, @Nullable Object> mappingFunction) {
GroupingByGatherer() {
mappingFunction = null;
}

GroupingByGatherer(Function<@Nullable INPUT, @Nullable Object> mappingFunction) {
mustNotBeNull(mappingFunction, "Mapping function must not be null");
this.mappingFunction = mappingFunction;
}
Expand All @@ -46,7 +50,7 @@ public Supplier<State<INPUT>> initializer() {
@Override
public Integrator<State<INPUT>, INPUT, List<INPUT>> integrator() {
return Integrator.ofGreedy((state, element, downstream) -> {
final Object thisMatch = mappingFunction.apply(element);
final Object thisMatch = mappingFunction == null ? element: mappingFunction.apply(element);
if (state.working == null) {
state.working = new ArrayList<>();
state.working.add(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.ginsberg.gatherers4j;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.math.BigDecimal;
import java.util.List;
Expand Down Expand Up @@ -172,14 +174,11 @@ void treatNullAsOne() {
);
}

@Test
void windowSizeMustBePositive() {
assertThatThrownBy(() ->
Stream.of(BigDecimal.ONE).gather(Gatherers4j.movingProduct(0))
).isExactlyInstanceOf(IllegalArgumentException.class);

@ParameterizedTest(name = "windowSize of {0}")
@ValueSource(ints = {-1, 0, 1})
void windowSizeMustBeGreaterThanOne(final int windowSize) {
assertThatThrownBy(() ->
Stream.of(BigDecimal.ONE).gather(Gatherers4j.movingProduct(-1))
Stream.of(BigDecimal.ONE).gather(Gatherers4j.movingProduct(windowSize))
).isExactlyInstanceOf(IllegalArgumentException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.ginsberg.gatherers4j;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.math.BigDecimal;
import java.util.List;
Expand Down Expand Up @@ -172,14 +174,13 @@ void treatNullAsOne() {
);
}

@Test
void windowSizeMustBePositive() {
assertThatThrownBy(() ->
Stream.of(BigDecimal.ONE).gather(Gatherers4j.movingSum(0))
).isExactlyInstanceOf(IllegalArgumentException.class);

@ParameterizedTest(name = "windowSize of {0}")
@ValueSource(ints = {-1, 0, 1})
void windowSizeMustBeGreaterThanOne(final int windowSize) {
assertThatThrownBy(() ->
Stream.of(BigDecimal.ONE).gather(Gatherers4j.movingSum(-1))
Stream.of(BigDecimal.ONE).gather(Gatherers4j.movingSum(windowSize))
).isExactlyInstanceOf(IllegalArgumentException.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.ginsberg.gatherers4j;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.math.BigDecimal;
import java.math.MathContext;
Expand Down Expand Up @@ -187,14 +189,11 @@ void treatNullAsZero() {
);
}

@Test
void windowSizeMustBePositive() {
assertThatThrownBy(() ->
Stream.of(BigDecimal.ONE).gather(Gatherers4j.simpleMovingAverage(0))
).isExactlyInstanceOf(IllegalArgumentException.class);

@ParameterizedTest(name = "windowSize of {0}")
@ValueSource(ints = {-1, 0, 1})
void windowSizeMustBeGreaterThanOne(final int windowSize) {
assertThatThrownBy(() ->
Stream.of(BigDecimal.ONE).gather(Gatherers4j.simpleMovingAverage(-1))
Stream.of(BigDecimal.ONE).gather(Gatherers4j.simpleMovingAverage(windowSize))
).isExactlyInstanceOf(IllegalArgumentException.class);
}

Expand Down

0 comments on commit 7f26f56

Please sign in to comment.