-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternMatchingSwitch.java
More file actions
103 lines (92 loc) · 3.29 KB
/
Copy pathPatternMatchingSwitch.java
File metadata and controls
103 lines (92 loc) · 3.29 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
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
package Phase9_ModernJavaAndModules.ModernJava;
/**
* Pattern Matching for switch (Java 21)
* -------------------------------------
* Java 21 finalises a powerful upgrade to switch: you can now match on the
* TYPE of the value (and combine with guards) inside switch labels - no more
* long chains of `if (x instanceof T) ... else if (...)`.
* <p>
*
* Three Building Blocks
* ---------------------
* 1. Type Pattern in a case label:
* case Integer i -> ...
* <p>
*
* 2. Guarded Pattern with `when`:
* case Integer i when i > 0 -> "positive int"
* <p>
*
* 3. null Handling - traditionally switch threw NullPointerException on null.
* With pattern matching you can match null with an explicit case:
* case null -> "missing"
* <p>
*
* Exhaustiveness
* --------------
* The compiler tracks the set of possible types and either you cover all of
* them or you add a `default`. For SEALED types it can verify exhaustiveness
* without any default at all.
* <p>
*
* Old vs New
* ----------
* // Old: if/else with instanceof
* String describe(Object o) {
* if (o instanceof Integer i) return "int " + i;
* else if (o instanceof String s) return "str of length " + s.length();
* else if (o == null) return "null";
* else return "other";
* }
* <p>
*
* // New: pattern matching switch
* String describe(Object o) {
* return switch (o) {
* case Integer i -> "int " + i;
* case String s -> "str of length " + s.length();
* case null -> "null";
* default -> "other";
* };
* }
*/
public class PatternMatchingSwitch {
public static void main(String[] args) {
Object[] samples = {
42,
"Hello",
3.14,
-5,
"",
null,
new java.util.ArrayList<>(java.util.List.of("a", "b"))
};
for (Object o : samples) {
System.out.println(o + " -> " + describe(o));
}
// OUTPUT
// 42 -> positive Integer: 42
// Hello -> non-empty String of length 5
// 3.14 -> Double: 3.14
// -5 -> non-positive Integer: -5
// -> empty String
// null -> null reference
// [a, b] -> some other Object: ArrayList
}
/** Classify any Object using a pattern-matching switch expression. */
static String describe(Object o) {
return switch (o) {
// Guarded patterns - the `when` clause adds a condition
case Integer i when i > 0 -> "positive Integer: " + i;
case Integer i -> "non-positive Integer: " + i;
// Type patterns combined with extra logic
case String s when s.isEmpty() -> "empty String";
case String s -> "non-empty String of length " + s.length();
case Double d -> "Double: " + d;
// Explicit null branch - no NullPointerException
case null -> "null reference";
// default catches everything else
default -> "some other Object: " + o.getClass().getSimpleName();
};
}
}