aboutsummaryrefslogtreecommitdiffstats
path: root/tests/features1920/java20/SwitchPatternPreview4Error.java
blob: ba38e739599d30ec1d9712af1cc2fc2312796e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * This used to work up to preview 3 (Java 19), but no longer in preview 4 (Java 20).
 * Now, the default case seems to need to be the last one in the list.
 */
public class SwitchPatternPreview4Error {
  static void defaultCanAppearBeforePattern(Integer i) {
    switch (i) {
      case null -> System.out.println("value unavailable: " + i);
      case -1, 1 -> System.out.println("absolute value 1: " + i);
      default -> System.out.println("other integer: " + i);
      case Integer value when value > 0 -> System.out.println("positive integer: " + i);
    }
  }

  static void defaultCanAppearBeforeNull(Integer i) {
    switch (i) {
      case -1, 1 -> System.out.println("absolute value 1: " + i);
      default -> System.out.println("other integer: " + i);
      case null -> System.out.println("value unavailable: " + i);
      case Integer value when value > 0 -> System.out.println("positive integer: " + i);
    }
  }

  static void defaultCanAppearBeforeConstantLabel(Integer i) {
    switch (i) {
      case null -> System.out.println("value unavailable: " + i);
      default -> System.out.println("other integer: " + i);
      case -1, 1 -> System.out.println("absolute value 1: " + i);
      case Integer value when value > 0 -> System.out.println("positive integer: " + i);
    }
  }
}