-
-
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.
Introduce dedicated ZipIteratorGatherer (#35)
- Loading branch information
Showing
2 changed files
with
34 additions
and
17 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/pivovarit/gatherers/ZipIteratorGatherer.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,33 @@ | ||
package com.pivovarit.gatherers; | ||
|
||
import java.util.Iterator; | ||
import java.util.Objects; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Gatherer; | ||
|
||
record ZipIteratorGatherer<T1, T2, R>(Iterator<T2> iterator, BiFunction<? super T1, ? super T2, ? extends R> mapper) | ||
implements Gatherer<T1, Iterator<T2>, R> { | ||
|
||
ZipIteratorGatherer { | ||
Objects.requireNonNull(mapper, "mapper can't be null"); | ||
Objects.requireNonNull(iterator, "iterator can't be null"); | ||
} | ||
|
||
@Override | ||
public Supplier<Iterator<T2>> initializer() { | ||
return () -> iterator; | ||
} | ||
|
||
@Override | ||
public Integrator<Iterator<T2>, T1, R> integrator() { | ||
return (state, element, downstream) -> { | ||
if (state.hasNext()) { | ||
downstream.push(mapper.apply(element, state.next())); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
} | ||
} |