-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordPatterns.java
More file actions
43 lines (32 loc) · 1.24 KB
/
RecordPatterns.java
File metadata and controls
43 lines (32 loc) · 1.24 KB
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
import static java.lang.String.format;
public class RecordPatterns {
public static void main(String[] args) {
var p1 = new Point(1.0, 2.0);
var p2 = new Point(5.0, 6.0);
var l = new Line(p1, p2);
instanceOfExample(p1);
instanceOfExample(l);
switchExample(p1);
switchExample(l);
}
public record Point(double x, double y) {
}
public record Line(Point start, Point end) {
}
static void instanceOfExample(Object o) {
if (o instanceof Point(double x, double y)) {
System.out.printf("I have a point at %.2f,%.2f%n", x, y);
}
if (o instanceof Line(Point(double x1, double y1), Point(double x2, double y2))) {
System.out.printf("I have a line from %.2f,%.2f to %.2f,%.2f%n", x1, y1, x2, y2);
}
}
static void switchExample(Object o) {
switch (o) {
case Point(double x, double y) -> System.out.printf("I have a point at %.2f,%.2f%n", x, y);
case Line(Point(double x1, double y1), Point(double x2, double y2)) ->
System.out.printf("I have a line from %.2f,%.2f to %.2f,%.2f%n", x1, y1, x2, y2);
default -> System.out.println("no idea");
}
}
}