Skip to content

Commit 67baefa

Browse files
committed
forEachLazily(IntStream, IntConsumer)
1 parent 2cd6277 commit 67baefa

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

mug/src/main/java/com/google/mu/util/stream/Iteration.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.util.concurrent.atomic.AtomicBoolean;
2525
import java.util.function.BiConsumer;
2626
import java.util.function.Consumer;
27+
import java.util.function.IntConsumer;
2728
import java.util.function.Supplier;
29+
import java.util.stream.IntStream;
2830
import java.util.stream.Stream;
2931

3032
/**
@@ -267,6 +269,16 @@ public final <V> Iteration<T> forEachLazily(Stream<V> stream, Consumer<? super V
267269
return forEachLazily(stream.spliterator(), consumer);
268270
}
269271

272+
/**
273+
* Applies {@code consumer} lazily on each int element from {@code stream}.
274+
* An element is only iterated when consumed by the result stream.
275+
*
276+
* @since 9.6
277+
*/
278+
public final <V> Iteration<T> forEachLazily(IntStream stream, IntConsumer consumer) {
279+
return forEachLazily(stream.spliterator(), consumer);
280+
}
281+
270282
/**
271283
* Applies {@code consumer} lazily on each element from {@code spliterator}.
272284
* An element is only iterated when consumed by the result stream.
@@ -285,6 +297,24 @@ public final <V> Iteration<T> forEachLazily(
285297
}));
286298
}
287299

300+
/**
301+
* Applies {@code consumer} lazily on each int element from {@code spliterator}.
302+
* An element is only iterated when consumed by the result stream.
303+
*
304+
* @since 9.6
305+
*/
306+
public final <V> Iteration<T> forEachLazily(
307+
Spliterator.OfInt spliterator, IntConsumer consumer) {
308+
requireNonNull(spliterator);
309+
requireNonNull(consumer);
310+
return lazily(
311+
() -> spliterator.tryAdvance(
312+
(int i) -> {
313+
consumer.accept(i);
314+
forEachLazily(spliterator, consumer);
315+
}));
316+
}
317+
288318
/**
289319
* Applies {@code consumer} lazily on each entry from {@code map}.
290320
* An entry is only iterated when consumed by the result stream.

mug/src/test/java/com/google/mu/util/stream/IterationTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

2121
import java.util.concurrent.atomic.AtomicInteger;
22+
import java.util.stream.IntStream;
2223
import java.util.stream.Stream;
2324

2425
import org.junit.Test;
@@ -42,6 +43,13 @@ public class IterationTest {
4243
.inOrder();
4344
}
4445

46+
@Test public void emit_lazyIntElements() {
47+
Iteration<Integer> iteration = new Iteration<>();
48+
assertThat(iteration.forEachLazily(IntStream.of(1, 2, 3), iteration::emit).iterate())
49+
.containsExactly(1, 2, 3)
50+
.inOrder();
51+
}
52+
4553
@Test public void emit_lazyEntries() {
4654
Iteration<String> iteration = new Iteration<>();
4755
assertThat(iteration.forEachLazily(

mug/src/test/java/com/google/mu/util/stream/PermutationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void next(List<T> buffer, int i) {
4646
}
4747
lazily(() -> next(buffer, i + 1));
4848
forEachLazily(
49-
IntStream.range(i + 1, buffer.size()).boxed(),
49+
IntStream.range(i + 1, buffer.size()),
5050
j -> {
5151
swap(buffer, i, j);
5252
next(buffer, i + 1);

0 commit comments

Comments
 (0)