From 0722ab14b52ff5db103f551b50dc164ca17f7993 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 14 Oct 2024 09:23:10 +0200 Subject: [PATCH] Introduce dedicated SamplingGatherer (#31) --- README.md | 2 +- .../pivovarit/gatherers/MoreGatherers.java | 18 ++---------- .../pivovarit/gatherers/SamplingGatherer.java | 29 +++++++++++++++++++ .../{SampleTest.java => SamplingTest.java} | 14 ++++----- 4 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/pivovarit/gatherers/SamplingGatherer.java rename src/test/java/com/pivovarit/gatherers/blackbox/{SampleTest.java => SamplingTest.java} (55%) diff --git a/README.md b/README.md index a94181c..0f168ec 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Whenever possible, the library follows Project Reactor's naming conventions. Provided `Gatherers`: - `MoreGatherers.last(int)` -- `MoreGatherers.sample(int)` +- `MoreGatherers.sampling(int)` - `MoreGatherers.zip(Iterator)` - `MoreGatherers.zip(Iterator, BiFunction)` - `MoreGatherers.zip(Stream)` diff --git a/src/main/java/com/pivovarit/gatherers/MoreGatherers.java b/src/main/java/com/pivovarit/gatherers/MoreGatherers.java index e159744..37d495f 100644 --- a/src/main/java/com/pivovarit/gatherers/MoreGatherers.java +++ b/src/main/java/com/pivovarit/gatherers/MoreGatherers.java @@ -9,7 +9,6 @@ import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Gatherer; -import java.util.stream.Gatherer.Integrator; import java.util.stream.Stream; import static java.util.stream.Gatherer.Integrator.ofGreedy; @@ -24,21 +23,8 @@ private MoreGatherers() { return new LastGatherer<>(n); } - public static Gatherer sample(int n) { - if (n <= 0) { - throw new IllegalArgumentException("sample size can't be lower than 1"); - } - return n == 1 - ? noop() - : Gatherer.ofSequential( - () -> new AtomicLong(), - Integrator.ofGreedy((state, element, downstream) -> { - if (state.getAndIncrement() % n == 0) { - downstream.push(element); - } - return true; - } - )); + public static Gatherer sampling(int n) { + return new SamplingGatherer<>(n); } public static Gatherer distinctByKeepLast(Function keyExtractor) { diff --git a/src/main/java/com/pivovarit/gatherers/SamplingGatherer.java b/src/main/java/com/pivovarit/gatherers/SamplingGatherer.java new file mode 100644 index 0000000..250cc47 --- /dev/null +++ b/src/main/java/com/pivovarit/gatherers/SamplingGatherer.java @@ -0,0 +1,29 @@ +package com.pivovarit.gatherers; + +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Supplier; +import java.util.stream.Gatherer; + +record SamplingGatherer(int n) implements Gatherer { + + SamplingGatherer { + if (n <= 0) { + throw new IllegalArgumentException("sampling frequency can't be lower than 1"); + } + } + + @Override + public Supplier initializer() { + return AtomicLong::new; + } + + @Override + public Integrator integrator() { + return Integrator.ofGreedy((state, element, downstream) -> { + if (state.getAndIncrement() % n == 0) { + downstream.push(element); + } + return true; + }); + } +} diff --git a/src/test/java/com/pivovarit/gatherers/blackbox/SampleTest.java b/src/test/java/com/pivovarit/gatherers/blackbox/SamplingTest.java similarity index 55% rename from src/test/java/com/pivovarit/gatherers/blackbox/SampleTest.java rename to src/test/java/com/pivovarit/gatherers/blackbox/SamplingTest.java index 876fb0e..328286e 100644 --- a/src/test/java/com/pivovarit/gatherers/blackbox/SampleTest.java +++ b/src/test/java/com/pivovarit/gatherers/blackbox/SamplingTest.java @@ -4,31 +4,31 @@ import java.util.stream.Stream; -import static com.pivovarit.gatherers.MoreGatherers.sample; +import static com.pivovarit.gatherers.MoreGatherers.sampling; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -class SampleTest { +class SamplingTest { @Test void shouldRejectInvalidSampleSize() { - assertThatThrownBy(() -> sample(0)) + assertThatThrownBy(() -> sampling(0)) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("sample size can't be lower than 1"); + .hasMessage("sampling frequency can't be lower than 1"); } @Test void shouldSampleEmpty() throws Exception { - assertThat(Stream.empty().gather(sample(42))).isEmpty(); + assertThat(Stream.empty().gather(sampling(42))).isEmpty(); } @Test void shouldSampleEvery() { - assertThat(Stream.of(1,2,3).gather(sample(1))).containsExactly(1,2,3); + assertThat(Stream.of(1,2,3).gather(sampling(1))).containsExactly(1,2,3); } @Test void shouldSampleEveryOther() { - assertThat(Stream.of(1,2,3).gather(sample(2))).containsExactly(1,3); + assertThat(Stream.of(1,2,3).gather(sampling(2))).containsExactly(1,3); } }