Skip to content

Commit

Permalink
Introduce dedicated ZipWithIndexGatherer (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Oct 14, 2024
1 parent 3dddab3 commit 48e7898
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/main/java/com/pivovarit/gatherers/MoreGatherers.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,6 @@ class State {
}

public static <T> Gatherer<T, ?, Map.Entry<Long, T>> zipWithIndex() {
return ofSequential(
AtomicLong::new,
ofGreedy((state, element, downstream) -> {
downstream.push(Map.entry(state.getAndIncrement(), element));
return true;
})
);
return new ZipWithIndexGatherer<>();
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/pivovarit/gatherers/ZipWithIndexGatherer.java
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;
});
}
}

0 comments on commit 48e7898

Please sign in to comment.