Files
javademos/recap26/src/Api26.java
T

45 lines
2.5 KiB
Java

import java.lang.management.ManagementFactory;
import java.net.http.HttpClient;
import java.nio.ByteOrder;
import java.util.List;
import java.util.UUID;
/**
* Public API that first appeared in Java 26 and needs no --enable-preview. Each call is asserted, so the
* run is the proof. The list was produced by diffing javap dumps of 25 and 26 (docs/output/50-*).
* Chapter: docs/11-recap-26.md
*/
public class Api26 {
public static void main(String[] args) throws Exception {
// String: Unicode case-folding comparisons, no more toLowerCase(Locale.ROOT) allocations
Check.that("STRASSE".equalsFoldCase("straße"), "\"STRASSE\".equalsFoldCase(\"stra\" + sharp s + \"e\") is true (full case folding)");
Check.that(!"STRASSE".equalsIgnoreCase("straße"), "equalsIgnoreCase says false for the same pair (simple folding only)");
Check.that("apple".compareToFoldCase("BANANA") < 0, "\"apple\".compareToFoldCase(\"BANANA\") < 0");
List<String> names = new java.util.ArrayList<>(List.of("b", "A", "c", "B"));
names.sort(String.UNICODE_CASEFOLD_ORDER);
Check.that(names.get(0).equalsFoldCase("a") && names.get(2).equalsFoldCase("b"), "sorted with String.UNICODE_CASEFOLD_ORDER: " + names);
// UUID: a v7-shaped UUID for a given instant
UUID u = UUID.ofEpochMillis(1_700_000_000_000L);
Check.that(u.version() == 7, "UUID.ofEpochMillis(..).version() == 7");
Check.that(u.toString().startsWith("018bcfe5-6800"), "the first 48 bits are the timestamp: " + u.toString().substring(0, 13) + "-...");
// Process is AutoCloseable now
try (Process p = new ProcessBuilder("true").start()) {
Check.that(p.waitFor() == 0, "try-with-resources on Process (implements Closeable in 26)");
}
// MemoryMXBean: cumulative CPU time spent in GC
long gcCpu = ManagementFactory.getMemoryMXBean().getTotalGcCpuTime();
Check.that(gcCpu >= 0, "MemoryMXBean.getTotalGcCpuTime() exists and is non-negative");
// ByteOrder became an enum
Check.that(ByteOrder.BIG_ENDIAN instanceof Enum<?>, "ByteOrder is an enum: " + java.util.Arrays.toString(ByteOrder.values()));
// HTTP/3 in the client API
HttpClient c = HttpClient.newBuilder().version(HttpClient.Version.HTTP_3).build();
Check.that(c.version() == HttpClient.Version.HTTP_3, "HttpClient.Version.HTTP_3 exists and is accepted by the builder");
Check.that(Character.UnicodeBlock.SIDETIC != null, "Character.UnicodeBlock.SIDETIC exists (Unicode 17)");
}
}