aboutsummaryrefslogtreecommitdiffstats
path: root/tests/features199/java18/SwitchPatternPreview2OK.java
blob: c17fdc12b567ce871105f38cfbbb90d1cc4cdf52 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
 * Inspired by examples in https://openjdk.java.net/jeps/420
 */
public class SwitchPatternPreview2OK {
  public static void main(String[] args) {
//    constantLabelMustAppearBeforePattern(-1);
//    constantLabelMustAppearBeforePattern(0);
//    constantLabelMustAppearBeforePattern(42);
//    constantLabelMustAppearBeforePattern(-99);
//    constantLabelMustAppearBeforePattern(Integer.valueOf(123));
//    constantLabelMustAppearBeforePattern(null);

    constantLabelMustAppearBeforePatternInteger(-1);
    constantLabelMustAppearBeforePatternInteger(0);
    constantLabelMustAppearBeforePatternInteger(42);
    constantLabelMustAppearBeforePatternInteger(-99);
    constantLabelMustAppearBeforePatternInteger(Integer.valueOf(123));
    constantLabelMustAppearBeforePatternInteger(null);

//    System.out.println(testGenericSealedExhaustive(new B<Integer>()));
  }

  /**
   * According to an example from JEP 420, this should work, but it does not, neither with Javac nor ECJ.
   *
   * See:
   *   https://openjdk.java.net/jeps/420#1b--Dominance-of-pattern-labels
   *   https://bugs.openjdk.java.net/browse/JDK-8273326
   *   https://bugs.eclipse.org/bugs/show_bug.cgi?id=579355
   *
   * TODO: reactivate when implemented or move to preview 3 with Java 19, Eclipse 4.24.
   */
  /*
  static String constantLabelMustAppearBeforePattern(Object o) {
    switch (o) {
      case null -> System.out.println("value unavailable: " + i);
      case -1, 1 -> System.out.println("special case:" + o);
      case Integer i && i > 0 -> System.out.println("positive integer: " + o);
      case Integer i -> System.out.println("other integer: " + o);
      default -> System.out.println("non-integer: " + o);
    }
    return i == null ? "null" : i.toString();
  }
  */

  static String constantLabelMustAppearBeforePatternInteger(Integer i) {
    switch (i) {
      case null -> System.out.println("value unavailable: " + i);
      case -1, 1 -> System.out.println("absolute value 1: " + i);
      case Integer value && value > 0 -> System.out.println("positive integer: " + i);
      default -> System.out.println("other integer: " + i);
    }
    return i == null ? "null" : i.toString();
  }

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

  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 && 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 && 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 && value > 0 -> System.out.println("positive integer: " + i);
    }
  }

  /**
   * According to an example from JEP 420, this should work with preview 2 (Java 18), and it does with Javac,
   * but not with ECJ for Java 18 and 19.
   *
   * See:
   *   https://openjdk.java.net/jeps/420#2--Exhaustiveness-of-switch-expressions-and-statements
   *   https://bugs.eclipse.org/bugs/show_bug.cgi?id=579360
   *   https://github.com/eclipse-jdt/eclipse.jdt.core/issues/587
   */
  /*
  sealed interface I<T> permits A, B {}
  final static class A<X> implements I<String> {}
  final static class B<Y> implements I<Y> {}

  static int testGenericSealedExhaustive(I<Integer> i) {
    return switch (i) {
      // Exhaustive as no A case possible!
      case B<Integer> bi -> 42;
    };
  }
  */
}