[jdk19] Add support for new guard pattern

This commit is contained in:
Rawi01 committed 2022-08-26 00:53:21 +02:00
1 parent 646723f6d3
commit 957adfae2e
4 files changed
+77 -1

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
// version 19:
public class Switch19 {
String switchPatternMatching(Object o) {
return switch (o) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
default -> o.toString();
};
}
String switchNull(Object o) {
return switch (o) {
case null, default -> "?";
};
}
String switchGuardPattern(Object o) {
return switch (o) {
case String s when s.length() > 1 -> s;
case String s -> s;
default -> o.toString();
};
}
String switchParenthesizedPattern(Object o) {
return switch (o) {
case (String s) -> s;
default -> o.toString();
};
}
}