Add patterns module: pattern matching from JDK 21 to 27

Type, record and primitive patterns, guards, sealed exhaustiveness, MatchException via separate compilation, and eight captured transcripts.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TF9JWFvJSNm6HVzswzZU5a
This commit is contained in:
Claude
2026-09-24 11:50:40 +00:00
parent 96b25c8fc4
commit bc91b9c8d0
36 changed files with 736 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
public class BoxedToNarrower {
public static void main(String[] args) {
Integer boxed = 300;
if (boxed instanceof byte b) System.out.println("fits a byte: " + b); // Integer to byte is not allowed
}
}
+16
View File
@@ -0,0 +1,16 @@
public class PrimitiveEdges {
public static void main(String[] args) {
double[] ds = {0.0, -0.0, Double.NaN, 16777217.0, 1e10, 3.0};
for (double d : ds) {
System.out.printf("%-12s int? %-5b float? %-5b%n", d, d instanceof int, d instanceof float);
}
long big = 1L << 40;
System.out.println(big + " instanceof int? " + (big instanceof int));
Object o = 42;
if (o instanceof int i) System.out.println("Object holding Integer matches int pattern: " + i);
Object s = (short) 7;
if (s instanceof int i) System.out.println("Short in Object matches int? " + i); else System.out.println("Short in Object does not match int");
int x = 65;
if (x instanceof char c) System.out.println("65 as char: " + c);
}
}
+17
View File
@@ -0,0 +1,17 @@
public class PrimitiveInRecord {
record Reading(double celsius) {}
static String classify(Object o) {
return switch (o) {
case Reading(int whole) -> "whole degrees: " + whole; // double component matched against an int pattern
case Reading(double d) -> "fractional: " + d;
default -> "not a reading";
};
}
public static void main(String[] args) {
System.out.println(classify(new Reading(21.0)));
System.out.println(classify(new Reading(21.5)));
System.out.println(classify("x"));
}
}
+20
View File
@@ -0,0 +1,20 @@
public class PrimitiveInstanceof {
static String describe(int i) {
if (i instanceof byte b) return i + " fits in a byte: " + b;
if (i instanceof short s) return i + " fits in a short: " + s;
return i + " needs an int";
}
static void floats(double d) {
if (d instanceof float f) System.out.println(d + " converts to float " + f + " without loss");
else System.out.println(d + " does NOT convert to float without loss");
}
public static void main(String[] args) {
System.out.println(describe(100));
System.out.println(describe(1000));
System.out.println(describe(100000));
floats(0.5);
floats(0.1);
}
}
+31
View File
@@ -0,0 +1,31 @@
public class PrimitiveSwitch {
static String httpFamily(int status) {
return switch (status) {
case 200 -> "OK";
case int i when i >= 200 && i < 300 -> "other success " + i;
case int i when i >= 400 && i < 500 -> "client error " + i;
case int i -> "something else " + i;
};
}
static String flag(boolean b) {
return switch (b) {
case true -> "yes";
case false -> "no";
};
}
static String big(long n) {
return switch (n) {
case 0L -> "zero";
case 1L -> "one";
case long l -> "many (" + l + ")";
};
}
public static void main(String[] args) {
System.out.println(httpFamily(200) + " | " + httpFamily(204) + " | " + httpFamily(404) + " | " + httpFamily(500));
System.out.println(flag(true) + " " + flag(false));
System.out.println(big(0) + " " + big(1) + " " + big(Long.MAX_VALUE));
}
}