blob: 26c1cf755d2ce254fc20f6c5f908d99e1dd47b1c (
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
|
import java.util.List;
/**
* Inspired by examples in https://openjdk.java.net/jeps/406
*/
public class SwitchPatternOK {
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)));
}
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("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 && (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";
};
}
}
|