Skip to content

Commit

Permalink
Introduce dedicated SamplingGatherer (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Oct 14, 2024
1 parent bb2798a commit 0722ab1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T2>)`
- `MoreGatherers.zip(Iterator<T2>, BiFunction<T1,T2>)`
- `MoreGatherers.zip(Stream<T2>)`
Expand Down
18 changes: 2 additions & 16 deletions src/main/java/com/pivovarit/gatherers/MoreGatherers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,21 +23,8 @@ private MoreGatherers() {
return new LastGatherer<>(n);
}

public static <T> Gatherer<T, ?, T> 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 <T> Gatherer<T, ?, T> sampling(int n) {
return new SamplingGatherer<>(n);
}

public static <T, U> Gatherer<T, ?, T> distinctByKeepLast(Function<? super T, ? extends U> keyExtractor) {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/pivovarit/gatherers/SamplingGatherer.java
Original file line number Diff line number Diff line change
@@ -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<T>(int n) implements Gatherer<T, AtomicLong, T> {

SamplingGatherer {
if (n <= 0) {
throw new IllegalArgumentException("sampling frequency can't be lower than 1");
}
}

@Override
public Supplier<AtomicLong> initializer() {
return AtomicLong::new;
}

@Override
public Integrator<AtomicLong, T, T> integrator() {
return Integrator.ofGreedy((state, element, downstream) -> {
if (state.getAndIncrement() % n == 0) {
downstream.push(element);
}
return true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 0722ab1

Please sign in to comment.