Skip to content

Commit ec22afe

Browse files
committed
Java 12 emulation
1 parent a5e649d commit ec22afe

File tree

8 files changed

+240
-0
lines changed

8 files changed

+240
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,10 @@ public String repeat(int count) {
791791
return asNativeString().repeat(count);
792792
}
793793

794+
public <R> R transform(Function<? super String,? extends R> f) {
795+
return f.apply(this);
796+
}
797+
794798
private int getLeadingWhitespaceLength() {
795799
int length = length();
796800
for (int i = 0; i < length; i++) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
import java.util.LinkedHashMap;
2828
import java.util.List;
2929
import java.util.LongSummaryStatistics;
30+
import java.util.AbstractMap;
3031
import java.util.Map;
3132
import java.util.Objects;
3233
import java.util.Optional;
3334
import java.util.Set;
3435
import java.util.StringJoiner;
3536
import java.util.function.BiConsumer;
3637
import java.util.function.BinaryOperator;
38+
import java.util.function.BiFunction;
3739
import java.util.function.Function;
3840
import java.util.function.Predicate;
3941
import java.util.function.Supplier;
@@ -71,6 +73,32 @@ public static <T, A, R, RR> Collector<T, A, RR> collectingAndThen(
7173
downstream.finisher().andThen(finisher));
7274
}
7375

76+
public static <T,R1,R2,R> Collector<T,?,R> teeing(Collector<? super T,?,R1> downstream1,
77+
Collector<? super T,?,R2> downstream2,
78+
BiFunction<? super R1,? super R2,R> merger) {
79+
return teeing2(downstream1, downstream2, merger);
80+
}
81+
82+
private static <T,R1,R2,R,X,Y> Collector<T,?,R> teeing2(Collector<? super T,X,R1> downstream1,
83+
Collector<? super T,Y,R2> downstream2,
84+
BiFunction<? super R1,? super R2,R> merger) {
85+
return Collector.of(
86+
() -> new AbstractMap.SimpleEntry<>(downstream1.supplier().get(), downstream2.supplier().get()),
87+
(a,b) -> {
88+
downstream1.accumulator().accept(a.getKey(), b);
89+
downstream2.accumulator().accept(a.getValue(), b);
90+
},
91+
(a,b)-> {
92+
X part = downstream1.combiner().apply(a.getKey(), b.getKey());
93+
Y part2 = downstream2.combiner().apply(a.getValue(), b.getValue());
94+
return new AbstractMap.SimpleEntry<>(part, part2);
95+
},
96+
(e) -> merger.apply(downstream1.finisher().apply(e.getKey()),
97+
downstream2.finisher().apply(e.getValue()))
98+
);
99+
}
100+
101+
74102
public static <T> Collector<T,?,Long> counting() {
75103
// Using Long::sum here fails in JDT
76104
return reducing(0L, item -> 1L, (a, b) -> (Long) a.longValue() + b.longValue());

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ public boolean tryAdvance(Consumer<? super T> action) {
246246
return StreamSupport.stream(spliterator, false);
247247
}
248248

249+
default List<T> toList() {
250+
return Collections.unmodifiableList(this.collect(Collectors.toList()));
251+
}
252+
249253
Stream<T> filter(Predicate<? super T> predicate);
250254

251255
Optional<T> findAny();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.emultest.java11.lang;
17+
18+
import com.google.gwt.emultest.java.util.EmulTestBase;
19+
20+
import java.util.Arrays;
21+
import java.util.stream.Collectors;
22+
23+
/**
24+
* Tests for java.lang.String Java 12 API emulation.
25+
*/
26+
public class StringTest extends EmulTestBase {
27+
28+
public void testTransform() {
29+
assertEquals(3, hideFromCompiler("foo").transform(String::length));
30+
}
31+
32+
public void testIndent() {
33+
assertEquals(" x", hideFromCompiler("x").indent(2));
34+
assertEquals("x", hideFromCompiler(" x").indent(-2));
35+
assertEquals(" x\n y", hideFromCompiler("x\ny").indent(2));
36+
assertEquals(" x\r\n y", hideFromCompiler("x\r\ny").indent(2));
37+
assertEquals(" x\r y", hideFromCompiler("x\ry").indent(2));
38+
assertEquals("x\ny", hideFromCompiler(" x\n y").indent(-2));
39+
assertEquals("x\r\ny", hideFromCompiler(" x\r\n y").indent(-2));
40+
assertEquals("x\ry", hideFromCompiler(" x\r y").indent(-2));
41+
}
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+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2024 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.emultest.java12.util.stream;
17+
18+
import com.google.gwt.emultest.java.util.EmulTestBase;
19+
20+
import java.util.Arrays;
21+
import java.util.stream.Collectors;
22+
23+
/**
24+
* Tests for java.lang.String Java 12 API emulation.
25+
*/
26+
public class CollectorsTest extends EmulTestBase {
27+
28+
public void testTeeing() {
29+
assertEquals(4, Stram.of(2, 4, 6).teeing(Collectors.summingToDouble(n -> n),
30+
Collectors.counting(), (sum, count) -> sum / count));
31+
}
32+
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2023 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.emultest;
17+
18+
import com.google.gwt.emultest.java12.lang.StringTest;
19+
20+
import org.junit.runner.RunWith;
21+
import org.junit.runners.Suite;
22+
23+
/** Test JRE emulations. */
24+
@RunWith(Suite.class)
25+
@Suite.SuiteClasses({
26+
StringTest.class,
27+
})
28+
public class EmulJava12Suite {
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2024 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.emultest.java12.lang;
17+
18+
import com.google.gwt.dev.util.arg.SourceLevel;
19+
import com.google.gwt.emultest.java.util.EmulTestBase;
20+
import com.google.gwt.junit.JUnitShell;
21+
22+
/**
23+
* Tests for java.lang.String Java 12 API emulation.
24+
*/
25+
public class StringTest extends EmulTestBase {
26+
27+
@Override
28+
public void runTest() throws Throwable {
29+
// Only run these tests if -sourceLevel 17 (or greater) is enabled.
30+
if (isGwtSourceLevel17()) {
31+
super.runTest();
32+
}
33+
}
34+
35+
public void testTransform() {
36+
assertFalse(isGwtSourceLevel17());
37+
}
38+
39+
public void testIndent() {
40+
assertFalse(isGwtSourceLevel17());
41+
}
42+
43+
private boolean isGwtSourceLevel17() {
44+
return JUnitShell.getCompilerOptions().getSourceLevel().compareTo(SourceLevel.JAVA17) >= 0;
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2024 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.emultest.java12.util.stream;
17+
18+
import com.google.gwt.dev.util.arg.SourceLevel;
19+
import com.google.gwt.emultest.java.util.EmulTestBase;
20+
import com.google.gwt.junit.JUnitShell;
21+
22+
import java.util.Arrays;
23+
import java.util.stream.Collectors;
24+
25+
/**
26+
* Tests for java.lang.String Java 12 API emulation.
27+
*/
28+
public class CollectorsTest extends EmulTestBase {
29+
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+
38+
public void testTeeing() {
39+
assertFalse(isGwtSourceLevel17());
40+
}
41+
42+
private boolean isGwtSourceLevel17() {
43+
return JUnitShell.getCompilerOptions().getSourceLevel().compareTo(SourceLevel.JAVA17) >= 0;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)