-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathRecordPatternsTest.java
88 lines (74 loc) · 2.47 KB
/
RecordPatternsTest.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
/**
* Run: `java --enable-preview --source 19 RecordPatternsTest.java`
*/
public class RecordPatternsTest {
public static void main(String[] args) {
simpleExampleOfDeconstruct();
var p1 = new Point(10, 10);
var p2 = new ColoredPoint(new Point(10, 10), Color.GREEN);
System.out.println("Has collision: " + checkCollisionIfs(p1, p2));
System.out.println("Has collision: " + checkCollisionSwitches(p1, p2));
}
static void simpleExampleOfDeconstruct() {
record ComputedPoint (int x, int y) {
// the accessor method is used is the deconstruction
public int y() {
return this.y + 10;
}
}
Object point = new ComputedPoint(42, 42);
// `int x` will receive the result of implitcy accessor `Point.x()`
// `int y` will receive the result of explicity accesor `Point.y()`
if (point instanceof ComputedPoint(int x, var y)) {
System.out.printf("Deconstruction: x=%d, y=%d%n%n", x, y);
} else {
System.out.println("Not a instance of ComputedPoint");
}
}
static boolean checkCollisionIfs(Object p1, Object p2) {
if (p1 instanceof Point(int x1, int y1) && p2 instanceof Point(int x2, int y2)) {
return x1 == x2 && y1 == y2;
}
if (p1 instanceof Point(int x1, int y1) && p2 instanceof ColoredPoint(Point(int x2, int y2), Color c)) {
return x1 == x2 && y1 == y2;
}
if (p1 instanceof ColoredPoint(Point(int x1, int y1), Color c) && p2 instanceof Point(int x2, int y2)) {
return x1 == x2 && y1 == y2;
}
if (p1 instanceof ColoredPoint(Point(int x1, int y1), Color c1)
&& p2 instanceof ColoredPoint(Point(int x2, int y2), Color c2)) {
return x1 == x2 && y1 == y2;
}
throw new IllegalArgumentException("Invalid type");
}
static boolean checkCollisionSwitches(Object p1, Object p2) {
int x1, y1, x2, y2;
// Record pattern on switch should be exhaustive (careful with generics on record)
switch (p1) {
case Point(int px1, int py1) -> {
x1 = px1;
y1 = py1;
}
case ColoredPoint(Point(int px1, int py1), Color c) -> {
x1 = px1;
y1 = py1;
}
case null, default -> throw new IllegalArgumentException("Invalid type");
}
switch (p2) {
case Point(int px2, int py2) -> {
x2 = px2;
y2 = py2;
}
case ColoredPoint(Point(int px2, int py2), Color c) -> {
x2 = px2;
y2 = py2;
}
case null, default -> throw new IllegalArgumentException("Invalid type");
}
return x1 == x2 && y1 == y2;
}
}
record Point(int x, int y) {}
enum Color { RED, GREEN, BLUE }
record ColoredPoint(Point p, Color c) {}