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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
import java.util.List;
import java.util.Locale;
/**
* Inspired by examples in https://openjdk.org/jeps/427
*/
public class SwitchPatternPreview3OK {
public static void main(String[] args) {
System.out.println(formatterPatternSwitch(null));
System.out.println(formatterPatternSwitch(123));
System.out.println(formatterPatternSwitch(999L));
System.out.println(formatterPatternSwitch(12.34));
System.out.println(formatterPatternSwitch("foo"));
System.out.println(formatterPatternSwitch(List.of(123, "foo", 999L, 12.34)));
System.out.println(testCircle(new Rectangle()));
System.out.println(testCircle(new Circle(5)));
System.out.println(testCircle(new Circle(6)));
System.out.println(testSealedCoverage(new A()));
System.out.println(testSealedCoverage(new B()));
System.out.println(testSealedCoverage(new C(5)));
// 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>()));
}
static String formatterPatternSwitch(Object o) {
return switch (o) {
case null -> "null";
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format(Locale.ENGLISH, "double %f", d);
case String s -> String.format("String %s", s);
default -> o.toString();
};
}
static class Shape {}
static class Rectangle extends Shape {}
static class Circle extends Shape {
private final double radius;
public Circle(double radius) { this.radius = radius; }
double calculateArea() { return Math.PI * radius * radius; }
}
static String testCircle(Shape s) {
return switch (s) {
case Circle c when (c.calculateArea() > 100) -> "Large circle";
case Circle c -> "Small circle";
default -> "Non-circle";
};
}
sealed interface S permits A, B, C {}
final static class A implements S {}
final static class B implements S {}
static record C(int i) implements S {} // Implicitly final
static String testSealedCoverage(S s) {
return switch (s) {
case A a -> "Sealed sub-class A";
case B b -> "Sealed sub-class B";
case C c -> "Sealed sub-record C";
};
}
/**
* 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 when 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 when 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 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);
}
}
/**
* 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
*
* TODO: reactivate when implemented or move to preview 4 with Java 20.
*/
/*
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;
};
}
*/
}
|