-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathRecordPatternTest.java
91 lines (76 loc) · 2.49 KB
/
RecordPatternTest.java
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
import java.util.*;
/**
* To run: `java RecordPatternTest.java`
*/
public class RecordPatternTest {
public static void main(String[] args) {
instanceofExample();
switchExample();
genericInferenceTest();
}
public static void instanceofExample() {
System.out.println("=== instanceof example ===");
var point = new Point(10, 10);
var coloredPoint = new ColoredPoint(point, "blue");
Object obj = coloredPoint;
// JDK 16
if (obj instanceof ColoredPoint cp) {
System.out.println("obj is a ColoredPoint: " + cp);
}
if (obj instanceof Point p) {
System.out.println("obj is a point: " + p);
} else {
System.out.println("obj is not a point");
}
// JDK 20
if (obj instanceof ColoredPoint(Point(int x, var y), String color)) {
System.out.printf("Point [%d,%d] has color %s%n", x, y, color);
}
}
public static void switchExample() {
System.out.println("=== switch example ===");
var list = new Decorator[] {
new Decorator(new Point(21, 21)),
new Decorator(new ColoredPoint(new Point(42, 42), "red")),
new Decorator("test")
};
for (Decorator d : list) {
switch (d) {
case Decorator(Point(var x, var y)) -> {
System.out.printf("Point [%d,%d]%n", x, y);
}
case Decorator(ColoredPoint(Point(var x, var y), String color)) -> {
System.out.printf("ColoredPoint [%d,%d] with color %s%n", x, y, color);
}
case Decorator(String s) -> {
System.out.println("Decorated string: " + s);
}
// required to be exhaustive
default -> System.out.println("None matched");
}
}
}
public static void genericInferenceTest() {
System.out.println("=== generic type inference example ===");
var point = new Point(42, 42);
var decoratedPoint = new Decorator(new ColoredPoint(point, "RED"));
var anotherDecorated = new Decorator(decoratedPoint);
// JDK 20: type inference in Decorator<T>(Decorator<T>(T))
if (anotherDecorated instanceof Decorator(Decorator(ColoredPoint(Point(int x, int y), String color)))) {
System.out.printf("x=%d, y=%d; color=%s%n%n", x, y, color);
}
}
static void removedSupportToUseRecordPatternInEnhancedForLoop() {
var items = new ColoredPoint[] { new ColoredPoint(new Point(42, 42), "red") };
// support removed in JDK 21
/*
// only works in JDK 20
for (ColoredPoint(Point(var x, var y), String color) : items) {
System.out.printf("Point [%d, %d] has color %s", x, y, color);
}
*/
}
}
record Point(int x, int y) {}
record ColoredPoint(Point p, String color) {}
record Decorator<T>(T t) {}