1 /**
   2  * @test /nodynamiccopyright/
   3  * @bug 8206986
   4  * @compile/fail/ref=MultipleLabelsExpression-old.out -source 9 -Xlint:-options -XDrawDiagnostics MultipleLabelsExpression.java
   5  * @compile --enable-preview -source 12 MultipleLabelsExpression.java
   6  * @run main/othervm --enable-preview MultipleLabelsExpression
   7  */
   8 
   9 import java.util.Objects;
  10 import java.util.function.Function;
  11 
  12 public class MultipleLabelsExpression {
  13     public static void main(String... args) {
  14         new MultipleLabelsExpression().run();
  15     }
  16 
  17     private void run() {
  18         runTest(this::expression1);
  19     }
  20 
  21     private void runTest(Function<T, String> print) {
  22         check(T.A,  print, "A");
  23         check(T.B,  print, "B-C");
  24         check(T.C,  print, "B-C");
  25         check(T.D,  print, "D");
  26         check(T.E,  print, "other");
  27     }
  28 
  29     private String expression1(T t) {
  30         return switch (t) {
  31             case A -> "A";
  32             case B, C -> { break "B-C"; }
  33             case D -> "D";
  34             default -> "other";
  35         };
  36     }
  37 
  38     private void check(T t, Function<T, String> print, String expected) {
  39         String result = print.apply(t);
  40         if (!Objects.equals(result, expected)) {
  41             throw new AssertionError("Unexpected result: " + result);
  42         }
  43     }
  44 
  45     enum T {
  46         A, B, C, D, E;
  47     }
  48 }