class Test {
enum X{A, B}
record R(int x, int y) {}
record R1(int z) {}
void test(Object obj) {
switch (obj) {
case Integer _, String _ -> System.out.println("string or int");
case R(_, _), R1 _ -> System.out.println("R or R1");
default -> System.out.println("other");
}
}
void testRepeat(Object obj) {
switch (obj) {
case Integer _, String _ -> System.out.println("string or int");
case Double _, Integer _ -> System.out.println("double or int");
default -> System.out.println("other");
}
}
void testEnum(Object obj) {
switch (obj) {
case X.A, String _ -> System.out.println("string or int");
case Integer _, X.B -> System.out.println("string or int");
default -> System.out.println("other");
}
}
void test2(Object obj) {
switch (obj) {
case Integer x, String _ -> System.out.println("string or int");
case R(_, var i), R1 _ -> System.out.println("R or R1");
default -> System.out.println("other");
}
}
void testGuards(Object obj) {
switch (obj) {
case Integer _ when ((Integer)obj) > 0,
String _ when !((String)obj).isEmpty() -> System.out.println("Positive integer or non-empty string");
default -> System.out.println("other");
}
}
void testFallthrough(Object obj) {
switch (obj) {
case Integer _:
case String _:
System.out.println("Number or string");
break;
case Double _:
case Float f:
System.out.println("double or float");
break;
default:
System.out.println("other");
}
}
record RR1() {}
record RR2() {}
void testNoVars(Object obj) {
switch(obj) {
case RR1(), RR2() -> {}
default -> {}
}
}
}