Skip to content

Commit d0e547d

Browse files
committed
More emulation
1 parent b780d3f commit d0e547d

File tree

18 files changed

+315
-68
lines changed

18 files changed

+315
-68
lines changed

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

Lines changed: 43 additions & 6 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,42 @@ private int getTrailingWhitespaceLength() {
815816
return length;
816817
}
817818

819+
public String indent(int spaces) {
820+
if (spaces == 0) {
821+
return this;
822+
}
823+
int nPosition = -1;
824+
int rPosition = -1;
825+
int nextIndex = 0;
826+
StringBuilder ret = new StringBuilder();
827+
String spaceString = spaces > 0 ? " ".repeat(spaces) : "";
828+
while (nextIndex < length()) {
829+
if (rPosition < nextIndex) {
830+
rPosition = cappedIndexOf('\r', nextIndex);
831+
}
832+
if (nPosition < nextIndex) {
833+
nPosition = cappedIndexOf('\n', nextIndex);
834+
}
835+
int lineEnd = Math.min(nPosition, rPosition) + 1;
836+
if (nPosition == rPosition + 1) {
837+
lineEnd++;
838+
}
839+
String line = substring(nextIndex, Math.min(lineEnd, length()));
840+
nextIndex = lineEnd;
841+
if (spaces > 0) {
842+
ret.append(spaceString).append(line);
843+
} else {
844+
int prefixLength = 0;
845+
while (prefixLength < line.length() && line.charAt(prefixLength) == ' '
846+
&& prefixLength < -spaces) {
847+
prefixLength++;
848+
}
849+
ret.append(line.substring(prefixLength));
850+
}
851+
}
852+
return ret.toString();
853+
}
854+
818855
private class LinesSpliterator extends Spliterators.AbstractSpliterator<String> {
819856
private int nextIndex = 0;
820857
private int rPosition = -1;
@@ -830,10 +867,10 @@ public boolean tryAdvance(Consumer<? super String> action) {
830867
return false;
831868
}
832869
if (rPosition < nextIndex) {
833-
rPosition = cappedIndexOf('\r');
870+
rPosition = cappedIndexOf('\r', nextIndex);
834871
}
835872
if (nPosition < nextIndex) {
836-
nPosition = cappedIndexOf('\n');
873+
nPosition = cappedIndexOf('\n', nextIndex);
837874
}
838875
int lineEnd = Math.min(nPosition, rPosition);
839876
action.accept(substring(nextIndex, lineEnd));
@@ -843,11 +880,11 @@ public boolean tryAdvance(Consumer<? super String> action) {
843880
}
844881
return nextIndex < length();
845882
}
883+
}
846884

847-
private int cappedIndexOf(char c) {
848-
int index = indexOf(c, nextIndex);
849-
return index == -1 ? length() : index;
850-
}
885+
private int cappedIndexOf(char c, int nextIndex) {
886+
int index = indexOf(c, nextIndex);
887+
return index == -1 ? length() : index;
851888
}
852889

853890
@JsType(isNative = true, name = "String", namespace = "<window>")

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

Lines changed: 19 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.DoubleSummaryStatistics;
23+
import java.util.List;
2224
import java.util.OptionalDouble;
2325
import java.util.PrimitiveIterator;
2426
import java.util.Spliterator;
@@ -58,6 +60,15 @@ default DoubleStream.Builder add(double t) {
5860
DoubleStream build();
5961
}
6062

63+
/**
64+
* See <a
65+
* href="https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/DoubleStream.DoubleMapMultiConsumer.html">
66+
* the official Java API doc</a> for details.
67+
*/
68+
interface DoubleMapMultiConsumer {
69+
void accept(double value, DoubleConsumer consumer);
70+
}
71+
6172
static Builder builder() {
6273
return new Builder() {
6374
private double[] items = new double[0];
@@ -317,5 +328,13 @@ public boolean tryAdvance(DoubleConsumer action) {
317328
return StreamSupport.doubleStream(spliterator, false);
318329
}
319330

331+
default DoubleStream mapMulti(DoubleStream.DoubleMapMultiConsumer mapper) {
332+
return flatMap(element -> {
333+
List<Double> buffer = new ArrayList<>();
334+
mapper.accept(element, (DoubleConsumer) buffer::add);
335+
return buffer.stream().mapToDouble(n -> n);
336+
});
337+
}
338+
320339
double[] toArray();
321340
}

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

Lines changed: 19 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+
interface IntMapMultiConsumer {
72+
void accept(int value, IntConsumer consumer);
73+
}
74+
6475
static Builder builder() {
6576
return new Builder() {
6677
private int[] items = new int[0];
@@ -366,5 +377,13 @@ 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+
mapper.accept(element, (IntConsumer) buffer::add);
384+
return buffer.stream().mapToInt(n -> n);
385+
});
386+
}
387+
369388
int[] toArray();
370389
}

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

Lines changed: 19 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.Comparator;
23+
import java.util.List;
2224
import java.util.LongSummaryStatistics;
2325
import java.util.OptionalDouble;
2426
import java.util.OptionalLong;
@@ -61,6 +63,15 @@ default LongStream.Builder add(long t) {
6163
LongStream build();
6264
}
6365

66+
/**
67+
* See <a
68+
* href="https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/stream/LongStream.LongMapMultiConsumer.html">
69+
* the official Java API doc</a> for details.
70+
*/
71+
interface LongMapMultiConsumer {
72+
void accept(long value, LongConsumer consumer);
73+
}
74+
6475
static Builder builder() {
6576
return new Builder() {
6677
private long[] items = new long[0];
@@ -364,5 +375,13 @@ public boolean tryAdvance(LongConsumer action) {
364375
return StreamSupport.longStream(spliterator, false);
365376
}
366377

378+
default LongStream mapMulti(LongStream.LongMapMultiConsumer mapper) {
379+
return flatMap(element -> {
380+
List<Long> buffer = new ArrayList<>();
381+
mapper.accept(element, (LongConsumer) buffer::add);
382+
return buffer.stream().mapToLong(n -> n);
383+
});
384+
}
385+
367386
long[] toArray();
368387
}

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

Lines changed: 37 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.Collections;
2223
import java.util.Comparator;
24+
import java.util.List;
2325
import java.util.Optional;
2426
import java.util.Spliterator;
2527
import java.util.Spliterators;
@@ -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,38 @@ public boolean tryAdvance(Consumer<? super T> action) {
327332
return StreamSupport.stream(spliterator, false);
328333
}
329334

335+
default <R> Stream<R> mapMulti(BiConsumer<T, ? super Consumer<R>> mapper) {
336+
return flatMap(element -> {
337+
List<R> buffer = new ArrayList<>();
338+
mapper.accept(element, (Consumer<R>) 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+
mapper.accept(element, (DoubleConsumer) buffer::add);
347+
return buffer.stream().mapToDouble(n -> n);
348+
});
349+
}
350+
351+
default IntStream mapMultiToInt(BiConsumer<? super T, ? super IntConsumer> mapper) {
352+
return flatMapToInt(element -> {
353+
List<Integer> buffer = new ArrayList<>();
354+
mapper.accept(element, (IntConsumer) buffer::add);
355+
return buffer.stream().mapToInt(n -> n);
356+
});
357+
}
358+
359+
default LongStream mapMultiToLong(BiConsumer<? super T, ? super LongConsumer> mapper) {
360+
return flatMapToLong(element -> {
361+
List<Long> buffer = new ArrayList<>();
362+
mapper.accept(element, (LongConsumer) buffer::add);
363+
return buffer.stream().mapToLong(n -> n);
364+
});
365+
}
366+
330367
Object[] toArray();
331368

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

user/test-super/com/google/gwt/dev/jjs/super/com/google/gwt/emultest/java12/lang/StringTest.java renamed to user/test-super/com/google/gwt/emultest/super/com/google/gwt/emultest/java17/lang/StringTest.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@
1313
* License for the specific language governing permissions and limitations under
1414
* the License.
1515
*/
16-
package com.google.gwt.emultest.java11.lang;
16+
package com.google.gwt.emultest.java17.lang;
1717

1818
import com.google.gwt.emultest.java.util.EmulTestBase;
1919

20-
import java.util.Arrays;
21-
import java.util.stream.Collectors;
22-
2320
/**
2421
* Tests for java.lang.String Java 12 API emulation.
2522
*/
2623
public class StringTest extends EmulTestBase {
2724

2825
public void testTransform() {
29-
assertEquals(3, hideFromCompiler("foo").transform(String::length));
26+
int stringLength = hideFromCompiler("foo").transform(String::length);
27+
assertEquals(3, stringLength);
3028
}
3129

3230
public void testIndent() {
@@ -39,12 +37,4 @@ public void testIndent() {
3937
assertEquals("x\r\ny", hideFromCompiler(" x\r\n y").indent(-2));
4038
assertEquals("x\ry", hideFromCompiler(" x\r y").indent(-2));
4139
}
42-
43-
private <T> T hideFromCompiler(T value) {
44-
if (Math.random() < -1) {
45-
// Can never happen, but fools the compiler enough not to optimize this call.
46-
fail();
47-
}
48-
return value;
49-
}
5040
}

user/test/com/google/gwt/emultest/java12/util/stream/CollectorsTest.java renamed to user/test-super/com/google/gwt/emultest/super/com/google/gwt/emultest/java17/util/stream/CollectorsTest.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,25 @@
1313
* License for the specific language governing permissions and limitations under
1414
* the License.
1515
*/
16-
package com.google.gwt.emultest.java12.util.stream;
16+
package com.google.gwt.emultest.java17.util.stream;
1717

18-
import com.google.gwt.dev.util.arg.SourceLevel;
1918
import com.google.gwt.emultest.java.util.EmulTestBase;
20-
import com.google.gwt.junit.JUnitShell;
2119

22-
import java.util.Arrays;
20+
import java.util.stream.Collector;
2321
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
2423

2524
/**
2625
* Tests for java.lang.String Java 12 API emulation.
2726
*/
2827
public class CollectorsTest extends EmulTestBase {
2928

30-
@Override
31-
public void runTest() throws Throwable {
32-
// Only run these tests if -sourceLevel 17 (or greater) is enabled.
33-
if (isGwtSourceLevel17()) {
34-
super.runTest();
35-
}
36-
}
37-
3829
public void testTeeing() {
39-
assertFalse(isGwtSourceLevel17());
40-
}
41-
42-
private boolean isGwtSourceLevel17() {
43-
return JUnitShell.getCompilerOptions().getSourceLevel().compareTo(SourceLevel.JAVA17) >= 0;
30+
Collector<Double, ?, Double> teeing = Collectors.teeing(
31+
Collectors.reducing(Double::sum),
32+
Collectors.counting(),
33+
(sum, count) -> sum.orElse(0.0) / count);
34+
assertEquals(4.0, Stream.of(2.0, 4.0, 6.0).collect(teeing), 0.01);
4435
}
4536

4637
}

0 commit comments

Comments
 (0)