-
-
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 ZipWithIndexMappingGatherer
- Loading branch information
Showing
2 changed files
with
29 additions
and
9 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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/pivovarit/gatherers/ZipWithIndexMappingGatherer.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,28 @@ | ||
package com.pivovarit.gatherers; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Gatherer; | ||
|
||
record ZipWithIndexMappingGatherer<T, R>(BiFunction<Long, ? super T, ? extends R> mapper) | ||
implements Gatherer<T, AtomicLong, R> { | ||
|
||
ZipWithIndexMappingGatherer { | ||
Objects.requireNonNull(mapper, "mapper can't be null"); | ||
} | ||
|
||
@Override | ||
public Supplier<AtomicLong> initializer() { | ||
return AtomicLong::new; | ||
} | ||
|
||
@Override | ||
public Integrator<AtomicLong, T, R> integrator() { | ||
return Integrator.ofGreedy((state, element, downstream) -> { | ||
downstream.push(mapper.apply(state.getAndIncrement(), element)); | ||
return true; | ||
}); | ||
} | ||
} |