|
9 | 9 | import java.security.MessageDigest; |
10 | 10 | import java.security.NoSuchAlgorithmException; |
11 | 11 | import java.text.NumberFormat; |
| 12 | +import java.util.AbstractMap; |
| 13 | +import java.util.List; |
12 | 14 | import java.util.Locale; |
| 15 | +import java.util.concurrent.CompletableFuture; |
| 16 | +import java.util.stream.Collectors; |
13 | 17 |
|
14 | | -/** |
15 | | - * Java 12 (March 2019) |
16 | | - */ |
| 18 | +/// Java 12™ (March 2019) |
| 19 | +/// [JDK 12](https://openjdk.org/projects/jdk/12) |
| 20 | +/// |
| 21 | +/// - STANDARD FEATURES: |
| 22 | +/// - 230: Microbenchmark Suite |
| 23 | +/// - 334: JVM Constants API |
| 24 | +/// - 340: One AArch64 Port, Not Two |
| 25 | +/// - 341: Default CDS Archives (`-Xshare:dump`) |
| 26 | +/// - 344: Abortable Mixed Collections for G1 |
| 27 | +/// - 346: Promptly Return Unused Committed Memory from G1 (`-XX:SoftMaxHeapSize`) |
| 28 | +/// |
| 29 | +/// - PREVIEW & INCUBATOR: |
| 30 | +/// - 189: Shenandoah: A Low-Pause-Time Garbage Collector "Experimental" (`-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC`) |
| 31 | +/// - 325: Switch Expressions (Preview) |
17 | 32 | public class Java12 { |
18 | 33 |
|
19 | 34 | @Test |
@@ -60,4 +75,29 @@ public void testCompactNumberFormat() { |
60 | 75 | Assertions.assertEquals("1.02M", shortNumberFormat.format(1_020_000)); |
61 | 76 | } |
62 | 77 |
|
| 78 | + @Test |
| 79 | + public void testTeeingCollector() { |
| 80 | + List<String> words = List.of("one", "two", "three"); |
| 81 | + |
| 82 | + AbstractMap.SimpleEntry<Integer, Long> pair = words.stream().collect(Collectors.teeing( |
| 83 | + Collectors.summingInt(String::length), |
| 84 | + Collectors.counting(), |
| 85 | + AbstractMap.SimpleEntry::new) |
| 86 | + ); |
| 87 | + |
| 88 | + Assertions.assertEquals(11, pair.getKey()); |
| 89 | + Assertions.assertEquals(3, pair.getValue()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testCompletableExceptionallyCompose() { |
| 94 | + var errorThrowingPlanA = CompletableFuture.supplyAsync(() -> 1 / 0); |
| 95 | + var noErrorPlanB = CompletableFuture.supplyAsync(() -> 0); |
| 96 | + |
| 97 | + errorThrowingPlanA.exceptionallyComposeAsync(ex -> { |
| 98 | + Assertions.assertInstanceOf(ArithmeticException.class, ex); |
| 99 | + return noErrorPlanB; |
| 100 | + }).thenAccept(result -> Assertions.assertEquals(0, result)); |
| 101 | + } |
| 102 | + |
63 | 103 | } |
0 commit comments