Skip to content

Commit 55d86e0

Browse files
committed
More emulation
1 parent b780d3f commit 55d86e0

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

user/super/com/google/gwt/emul/java/util/stream/IntStream.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ default IntStream.Builder add(int t) {
6161
IntStream build();
6262
}
6363

64+
/**
65+
* See <a
66+
* href="https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/IntStream.IntMapMultiConsumer.html">
67+
* the official Java API doc</a> for details.
68+
*/
69+
public interface IntMapMultiConsumer {
70+
accept(int value, IntConsumer ic);
71+
}
72+
6473
static Builder builder() {
6574
return new Builder() {
6675
private int[] items = new int[0];
@@ -366,5 +375,14 @@ public boolean tryAdvance(IntConsumer action) {
366375
return StreamSupport.intStream(spliterator, false);
367376
}
368377

378+
default IntStream mapMulti(IntMapMultiConsumer mapper) {
379+
return flatMapToInt(element -> {
380+
List<Integer> buffer = new ArrayList<>();
381+
IntConsumer consumer = buffer::add;
382+
mapper.accept(element, consumer);
383+
return buffer.stream().mapToInt(n -> n);
384+
});
385+
}
386+
369387
int[] toArray();
370388
}

user/super/com/google/gwt/emul/java/util/stream/Stream.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
import static javaemul.internal.InternalPreconditions.checkState;
1919

20+
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.Collections;
23+
import java.util.List;
2224
import java.util.Comparator;
2325
import java.util.Optional;
2426
import java.util.Spliterator;
@@ -28,8 +30,11 @@
2830
import java.util.function.BiFunction;
2931
import java.util.function.BinaryOperator;
3032
import java.util.function.Consumer;
33+
import java.util.function.DoubleConsumer;
3134
import java.util.function.Function;
35+
import java.util.function.IntConsumer;
3236
import java.util.function.IntFunction;
37+
import java.util.function.LongConsumer;
3338
import java.util.function.Predicate;
3439
import java.util.function.Supplier;
3540
import java.util.function.ToDoubleFunction;
@@ -327,6 +332,41 @@ public boolean tryAdvance(Consumer<? super T> action) {
327332
return StreamSupport.stream(spliterator, false);
328333
}
329334

335+
default public <R> Stream<R> mapMulti(BiConsumer<T, Consumer<? super R>> mapper) {
336+
return flatMap(element -> {
337+
List<R> buffer = new ArrayList<>();
338+
mapper.accept(element, buffer::add);
339+
return buffer.stream();
340+
});
341+
}
342+
343+
default DoubleStream mapMultiToDouble(BiConsumer<? super T, ? super DoubleConsumer> mapper) {
344+
return flatMapToDouble(element -> {
345+
List<Double> buffer = new ArrayList<>();
346+
DoubleConsumer consumer = buffer::add;
347+
mapper.accept(element, consumer);
348+
return buffer.stream().mapToDouble(n -> n);
349+
});
350+
}
351+
352+
default IntStream mapMultiToInt(BiConsumer<? super T, ? super IntConsumer> mapper) {
353+
return flatMapToInt(element -> {
354+
List<Integer> buffer = new ArrayList<>();
355+
IntConsumer consumer = buffer::add;
356+
mapper.accept(element, consumer);
357+
return buffer.stream().mapToInt(n -> n);
358+
});
359+
}
360+
361+
default LongStream mapMultiToLong(BiConsumer<? super T, ? super LongConsumer> mapper) {
362+
return flatMapToLong(element -> {
363+
List<Long> buffer = new ArrayList<>();
364+
LongConsumer consumer = buffer::add;
365+
mapper.accept(element, consumer);
366+
return buffer.stream().mapToLong(n -> n);
367+
});
368+
}
369+
330370
Object[] toArray();
331371

332372
<A> A[] toArray(IntFunction<A[]> generator);

0 commit comments

Comments
 (0)