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:
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user