-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternMatchingSwitch.java
More file actions
137 lines (120 loc) · 4.01 KB
/
Copy pathPatternMatchingSwitch.java
File metadata and controls
137 lines (120 loc) · 4.01 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package java18.patternmatching;
/**
* Java 18 Pattern Matching for switch (Second Preview) Demonstrates pattern matching with switch expressions
* NOTE: Requires --enable-preview flag for compilation and execution Compile: javac --enable-preview --release 18 PatternMatchingSwitch.java Run: java --enable-preview PatternMatchingSwitch
*/
public class PatternMatchingSwitch
{
public static void main(String[] args)
{
System.out.println("=== Java 18 Pattern Matching for switch (Second Preview) ===\n");
// 1. Basic pattern matching
System.out.println("1. Basic Pattern Matching");
System.out.println("-------------------------");
Object obj1 = "Hello";
String result1 = switch (obj1)
{
case String s -> "String: " + s;
case Integer i -> "Integer: " + i;
case null -> "Null";
default -> "Unknown type";
};
System.out.println("Result: " + result1);
// 2. With sealed classes (exhaustive matching)
System.out.println("\n2. Pattern Matching with Sealed Classes");
System.out.println("----------------------------------------");
Shape circle = new Circle(5.0);
Shape rectangle = new Rectangle(10.0, 20.0);
Shape triangle = new Triangle(8.0, 6.0);
System.out.println("Circle area: " + calculateArea(circle));
System.out.println("Rectangle area: " + calculateArea(rectangle));
System.out.println("Triangle area: " + calculateArea(triangle));
// 3. Guarded patterns
System.out.println("\n3. Guarded Patterns");
System.out.println("------------------");
Object obj2 = "Hello World";
String result2 = switch (obj2)
{
case String s when s.length() > 10 -> "Long string: " + s;
case String s -> "Short string: " + s;
case Integer i when i > 100 -> "Large number: " + i;
case Integer i -> "Small number: " + i;
default -> "Unknown";
};
System.out.println("Result: " + result2);
// 4. Multiple types
System.out.println("\n4. Processing Multiple Types");
System.out.println("----------------------------");
processObject("Hello");
processObject(42);
processObject(3.14);
processObject(true);
processObject(null);
// 5. Comparison with traditional approach
System.out.println("\n5. Comparison: Traditional vs Pattern Matching");
System.out.println("---------------------------------------------");
Object value = "Hello";
// Traditional way (before Java 17)
String traditional;
if (value instanceof String)
{
String s = (String) value; // Manual cast
traditional = "String: " + s;
}
else if (value instanceof Integer)
{
Integer i = (Integer) value; // Manual cast
traditional = "Integer: " + i;
}
else
{
traditional = "Unknown";
}
System.out.println("Traditional: " + traditional);
// Pattern matching way (Java 18)
String patternMatching = switch (value)
{
case String s -> "String: " + s; // Automatic cast
case Integer i -> "Integer: " + i; // Automatic cast
default -> "Unknown";
};
System.out.println("Pattern matching: " + patternMatching);
}
private static double calculateArea(Shape shape)
{
// Exhaustive pattern matching with sealed classes
// No default case needed - compiler knows all possible types
return switch (shape)
{
case Circle c -> Math.PI * c.radius() * c.radius();
case Rectangle r -> r.width() * r.height();
case Triangle t -> 0.5 * t.base() * t.height();
};
}
private static void processObject(Object obj)
{
String result = switch (obj)
{
case String str -> "String: " + str;
case Integer num -> "Integer: " + num;
case Double num -> "Double: " + num;
case Boolean bool -> "Boolean: " + bool;
case null -> "Null value";
default -> "Other type: " + obj.getClass().getSimpleName();
};
System.out.println(" " + result);
}
// Sealed interface for exhaustive pattern matching
public sealed interface Shape permits Circle, Rectangle, Triangle
{
}
public record Circle(double radius) implements Shape
{
}
public record Rectangle(double width, double height) implements Shape
{
}
public record Triangle(double base, double height) implements Shape
{
}
}