diff --git a/src/main/java/com/pivovarit/gatherers/MoreGatherers.java b/src/main/java/com/pivovarit/gatherers/MoreGatherers.java index 395b2dd..b9db791 100644 --- a/src/main/java/com/pivovarit/gatherers/MoreGatherers.java +++ b/src/main/java/com/pivovarit/gatherers/MoreGatherers.java @@ -124,15 +124,7 @@ class State { } public static Gatherer zipWithIndex(BiFunction mapper) { - Objects.requireNonNull(mapper, "mapper can't be null"); - - return ofSequential( - AtomicLong::new, - ofGreedy((state, element, downstream) -> { - downstream.push(mapper.apply(state.getAndIncrement(), element)); - return true; - }) - ); + return new ZipWithIndexMappingGatherer<>(mapper); } public static Gatherer> zipWithIndex() { diff --git a/src/main/java/com/pivovarit/gatherers/ZipWithIndexMappingGatherer.java b/src/main/java/com/pivovarit/gatherers/ZipWithIndexMappingGatherer.java new file mode 100644 index 0000000..6b747ff --- /dev/null +++ b/src/main/java/com/pivovarit/gatherers/ZipWithIndexMappingGatherer.java @@ -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(BiFunction mapper) + implements Gatherer { + + ZipWithIndexMappingGatherer { + Objects.requireNonNull(mapper, "mapper can't be null"); + } + + @Override + public Supplier initializer() { + return AtomicLong::new; + } + + @Override + public Integrator integrator() { + return Integrator.ofGreedy((state, element, downstream) -> { + downstream.push(mapper.apply(state.getAndIncrement(), element)); + return true; + }); + } +}