import java.util.function.*; public class SwitchExpressionsJava12 { void multiLabel(int[] arr) { int z = switch (arr.length % 5) { case 0, 4, 3, 2, 1 -> 0; default -> throw new IllegalStateException("Unexpected value"); }; } static void fooBar(int k) { String s = switch (k) { case 1, 2 -> "foo"; default -> "bar"; }; if(s.equals("baz")) {} } enum X {A, B, C}; static void testEnum(X x) { int i = switch(x) { case A -> 1; case B -> 2; }; // default and case C is missing: we assume that IncompatibleClassChangeError is still thrown if (i == 0) {} int i1 = switch(x) { case A -> 1; case B -> 2; case C -> 3; }; if (i1 == 0) {} // exhaustive } static void testEnumAndCatch(X x) { int i1 = 0; try { i1 = switch(x) { case A -> 1; case B -> 2; case C -> 3; }; } catch (IncompatibleClassChangeError ex) { if (i1 == 0) {} if (x == X.A) {} if (x == X.B) {} if (x == X.C) {} } } static void testBoxed(int i) { Integer j = switch(i) { case 1 -> 2; case 2 -> i+3; case 3 -> i; default -> 4; }; if (j > 1) {} if (j < 6) {} if (j < 5) {} } static void testBoxedThrow(int i) { Integer j = switch(i) { case 1 -> 2; case 2 -> i+3; case 3 -> i; default -> throw new IllegalArgumentException(); }; if (j > 1) {} if (j < 6) {} if (j < 5) {} if (j == 5 && i != 2) {} if (i > 3) {} if (i < 1) {} } static void testIncomplete(Integer i) { if (i == null) { System.out.println(switch (i)); } else { int x = switch(i) { case 1 -> case 2 -> { if (i == 1) {} throw new IllegalArgumentException(); } default -> 1; }; if (x != 1) { // the only case where we don't know the returned value is i == 1, so the warning is logical here if (i == 1) {} } } } static void testSimpleBreak(int i) { int j = switch(i) { case 1 -> { System.out.println("Hello"); yield 10; } case 2 -> { i = i+1; yield 20; } case 3 -> { yield 33-i; } default -> 0; }; if (j == 20 && i == 3) {} if (j == 30 && i == 3) {} if (j == 10 && i == 1) {} if (i == 2) {} if (j == 0 && (i < 1 || i > 3)) {} } static void testSwitchInInlinedLambda(int i) { int x = ((IntSupplier)() -> { int j = switch (i) { case 1 -> 2; case 2 -> 3; case 3 -> { System.out.println("hello"); yield 1; } default -> 4; }; if (j < 2) {} if (j == 4 && i == 2) {} if (i == 3 && j == 1) {} return 0; }).getAsInt(); if (x == 1 && i == 3) {} } static void testSwitchWithInnerFinally(int i) { int j = switch (i) { case 1 -> 2; case 2 -> 3; case 3 -> { try { System.out.println("hello"); yield 1; // never happens } finally { yield 2; } } default -> 4; }; if (j < 2) {} if (j == 4 && i == 2) {} } static int get(int x) { return x; } void testStatementInsideExpressionInsideBlockLambda(int x, int y) { int i = ((IntSupplier)() -> { System.out.println(); return switch(x) { case 1 -> { switch (y) { default -> get(x); } yield 5; } default -> 10; }; }).getAsInt(); if (i != 10) {} } void testSwitchWithCatch(int x) { int i = switch(x) { case 1, 2: try { if (x == 1) throw new IllegalArgumentException(); } catch (IllegalArgumentException ex) { yield 100; } case 3: yield 200; default: yield 300; }; if (i == 100 && x == 1) {} if (i == 200 && (x == 2 || x == 3)) {} } int i; void testSwitchIncompleteBreak(X e) { int z; z = switch (e) { case A: i = 1; yield 2; case B: i = 2; yield 3; case C: i = 3; break; default: { i = 10; yield 10;/* todo one two */ } }; System.out.println("i = " + i); } }