Skip to content

Commit 033deb4

Browse files
committed
More emulation
1 parent b780d3f commit 033deb4

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

user/super/com/google/gwt/emul/java/lang/String.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Spliterators;
3333
import java.util.StringJoiner;
3434
import java.util.function.Consumer;
35+
import java.util.function.Function;
3536
import java.util.stream.Stream;
3637
import java.util.stream.StreamSupport;
3738
import javaemul.internal.ArrayHelper;
@@ -815,6 +816,20 @@ private int getTrailingWhitespaceLength() {
815816
return length;
816817
}
817818

819+
public String indent(int spaces) {
820+
if (spaces == 0) {
821+
return this;
822+
}
823+
if (spaces > 0) {
824+
return " ".repeat(spaces) + this;
825+
}
826+
int prefixLength = 0;
827+
while (prefixLength < length() && charAt(prefixLength) == ' ') {
828+
prefixLength++;
829+
}
830+
return substring(prefixLength);
831+
}
832+
818833
private class LinesSpliterator extends Spliterators.AbstractSpliterator<String> {
819834
private int nextIndex = 0;
820835
private int rPosition = -1;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

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

20+
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.Comparator;
2223
import java.util.IntSummaryStatistics;
24+
import java.util.List;
2325
import java.util.OptionalDouble;
2426
import java.util.OptionalInt;
2527
import java.util.PrimitiveIterator;
@@ -61,6 +63,15 @@ default IntStream.Builder add(int t) {
6163
IntStream build();
6264
}
6365

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

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

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);

user/test/com/google/gwt/emultest/EmulSuite.gwt.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<source path='java9' />
2121
<source path='java10' />
2222
<source path='java11' />
23+
<source path='java12' />
2324

2425
<define-configuration-property name="someConfigurationProperty" is-multi-valued='false'/>
2526
<set-configuration-property name="someConfigurationProperty" value="conf"/>

user/test/com/google/gwt/emultest/java12/util/stream/CollectorsTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
import com.google.gwt.emultest.java.util.EmulTestBase;
2020
import com.google.gwt.junit.JUnitShell;
2121

22-
import java.util.Arrays;
23-
import java.util.stream.Collectors;
24-
2522
/**
2623
* Tests for java.lang.String Java 12 API emulation.
2724
*/

0 commit comments

Comments
 (0)