Skip to content

Commit

Permalink
Handle null mapping function in maxBy and minBy (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
tginsberg authored Oct 13, 2024
1 parent b58699c commit 00fae6b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/ginsberg/gatherers4j/Gatherers4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public static <INPUT> LastGatherer<INPUT> last(final int count) {
public static <INPUT, MAPPED extends Comparable<MAPPED>> MinMaxGatherer<INPUT, MAPPED> maxBy(
final Function<INPUT, MAPPED> function
) {
mustNotBeNull(function, "Mapping function must not be null");
return new MinMaxGatherer<>(true, function);
}

Expand All @@ -131,6 +132,7 @@ public static <INPUT, MAPPED extends Comparable<MAPPED>> MinMaxGatherer<INPUT, M
public static <INPUT, MAPPED extends Comparable<MAPPED>> MinMaxGatherer<INPUT, MAPPED> minBy(
final Function<INPUT, MAPPED> function
) {
mustNotBeNull(function, "Mapping function must not be null");
return new MinMaxGatherer<>(false, function);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/ginsberg/gatherers4j/MinMaxGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public BiConsumer<State<INPUT, MAPPED>, Downstream<? super INPUT>> finisher() {
if (state.bestSoFar != null) {
downstream.push(state.bestSoFar);
}
// TODO: What happens if we have no best?
};
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/ginsberg/gatherers4j/MinMaxGathererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class MinMaxGathererTest {

Expand Down Expand Up @@ -71,6 +72,13 @@ void firstValueSelectedWhenMultipleExist() {
assertThat(output).containsExactly(new TestObject("A", 1));
}

@Test
void mappingFunctionMustNotBeNull() {
assertThatThrownBy(() ->
Stream.empty().gather(Gatherers4j.maxBy(null))
).isInstanceOf(IllegalArgumentException.class);
}

@Test
void maxValue() {
// Arrange
Expand Down Expand Up @@ -132,6 +140,13 @@ void firstValueSelectedWhenMultipleExist() {
assertThat(output).containsExactly(new TestObject("A", 1));
}

@Test
void mappingFunctionMustNotBeNull() {
assertThatThrownBy(() ->
Stream.empty().gather(Gatherers4j.minBy(null))
).isInstanceOf(IllegalArgumentException.class);
}

@Test
void minValue() {
// Arrange
Expand Down

0 comments on commit 00fae6b

Please sign in to comment.