-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add windowSliding() Gatherer with customizable step parameter (#45)
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 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
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
70 changes: 70 additions & 0 deletions
70
src/main/java/com/pivovarit/gatherers/WindowSlidingGatherer.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,70 @@ | ||
package com.pivovarit.gatherers; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Gatherer; | ||
|
||
record WindowSlidingGatherer<T>(int windowSize, int step) | ||
implements Gatherer<T, WindowSlidingGatherer.SlidingWindow, List<T>> { | ||
WindowSlidingGatherer { | ||
if (windowSize < 1) { | ||
throw new IllegalArgumentException("'windowSize' must be greater than zero"); | ||
} | ||
|
||
if (step < 0) { | ||
throw new IllegalArgumentException("'step' must be greater than or equal to zero"); | ||
} | ||
|
||
if (step > windowSize) { | ||
throw new IllegalArgumentException("'step' must be less than or equal to 'windowSize'"); | ||
} | ||
} | ||
|
||
@Override | ||
public Supplier<WindowSlidingGatherer.SlidingWindow> initializer() { | ||
return WindowSlidingGatherer.SlidingWindow::new; | ||
} | ||
|
||
@Override | ||
public Integrator<WindowSlidingGatherer.SlidingWindow, T, List<T>> integrator() { | ||
return Integrator.ofGreedy((state, e, downstream) -> state.integrate(e, downstream)); | ||
} | ||
|
||
@Override | ||
public BiConsumer<WindowSlidingGatherer.SlidingWindow, Downstream<? super List<T>>> finisher() { | ||
return SlidingWindow::finish; | ||
} | ||
|
||
class SlidingWindow { | ||
Object[] window = new Object[windowSize]; | ||
int at = 0; | ||
boolean firstWindow = true; | ||
|
||
boolean integrate(T element, Downstream<? super List<T>> downstream) { | ||
window[at++] = element; | ||
if (at < windowSize) { | ||
return true; | ||
} else { | ||
final var oldWindow = window; | ||
final var newWindow = new Object[windowSize]; | ||
System.arraycopy(oldWindow, step, newWindow, 0, windowSize - step); | ||
window = newWindow; | ||
at -= step; | ||
firstWindow = false; | ||
return downstream.push((List<T>) Arrays.asList(oldWindow)); | ||
} | ||
} | ||
|
||
void finish(Downstream<? super List<T>> downstream) { | ||
if (firstWindow && at > 0 && !downstream.isRejecting()) { | ||
var lastWindow = new Object[at]; | ||
System.arraycopy(window, 0, lastWindow, 0, at); | ||
window = null; | ||
at = 0; | ||
downstream.push((List<T>) Arrays.asList(lastWindow)); | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/com/pivovarit/gatherers/blackbox/WindowSlidingTest.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,44 @@ | ||
package com.pivovarit.gatherers.blackbox; | ||
|
||
import com.pivovarit.gatherers.MoreGatherers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static java.util.List.of; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class WindowSlidingTest { | ||
|
||
@Test | ||
void shouldRejectInvalidWindowSize() { | ||
assertThatThrownBy(() -> MoreGatherers.windowSliding(0, 1)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("'windowSize' must be greater than zero"); | ||
} | ||
|
||
@Test | ||
void shouldRejectInvalidStep() { | ||
assertThatThrownBy(() -> MoreGatherers.windowSliding(1, -1)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("'step' must be greater than or equal to zero"); | ||
} | ||
|
||
@Test | ||
void shouldWindowSlidingEmpty() { | ||
assertThat(Stream.empty().gather(MoreGatherers.windowSliding(2, 1))).isEmpty(); | ||
} | ||
|
||
@Test | ||
void shouldWindowSlidingWithStep1() { | ||
assertThat(Stream.of(1, 2, 3, 4, 5).gather(MoreGatherers.windowSliding(2, 1))) | ||
.containsExactly(of(1, 2), of(2, 3), of(3, 4), of(4, 5)); | ||
} | ||
|
||
@Test | ||
void shouldWindowSlidingWithStep2() { | ||
assertThat(Stream.of(1, 2, 3, 4, 5).gather(MoreGatherers.windowSliding(2, 2))) | ||
.containsExactly(of(1, 2), of(3, 4)); | ||
} | ||
} |