-
-
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 ZipWithIndexGatherer (#33)
- Loading branch information
Showing
2 changed files
with
23 additions
and
7 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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/pivovarit/gatherers/ZipWithIndexGatherer.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,22 @@ | ||
package com.pivovarit.gatherers; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Gatherer; | ||
|
||
record ZipWithIndexGatherer<T>() implements Gatherer<T, AtomicLong, Map.Entry<Long, T>> { | ||
|
||
@Override | ||
public Supplier<AtomicLong> initializer() { | ||
return AtomicLong::new; | ||
} | ||
|
||
@Override | ||
public Integrator<AtomicLong, T, Map.Entry<Long, T>> integrator() { | ||
return Integrator.ofGreedy((state, element, downstream) -> { | ||
downstream.push(Map.entry(state.getAndIncrement(), element)); | ||
return true; | ||
}); | ||
} | ||
} |