-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathLanguageFeatures.java
More file actions
57 lines (49 loc) · 1.39 KB
/
Copy pathLanguageFeatures.java
File metadata and controls
57 lines (49 loc) · 1.39 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
package java25;
import module java.base;
/// Exercises language features that require Java 22 through Java 25 source support.
public class LanguageFeatures {
/// Java 22 unnamed variables and unnamed record patterns.
public static int unnamedVariablesAndPatterns(Object input) {
int seen = 0;
for (String _ : List.of("alpha", "beta")) {
seen++;
}
try {
Integer.parseInt("not-a-number");
} catch (NumberFormatException _) {
seen++;
}
if (input instanceof Range(int start, int _)) {
return start + seen;
}
return seen;
}
/// Java 24/25 primitive patterns in switch.
public static String primitivePatterns(int value) {
return switch (value) {
case byte _ -> "byte range";
case int i when i > 0 -> "positive int";
case int _ -> "int";
};
}
/// Java 25 flexible constructor bodies.
public static final class Child extends Parent {
public Child(String input) {
String normalized = Objects.requireNonNull(input).strip();
if (normalized.isEmpty()) {
throw new IllegalArgumentException("blank");
}
super(normalized);
}
}
public static sealed class Parent permits Child {
private final String value;
protected Parent(String value) {
this.value = value;
}
public String value() {
return value;
}
}
public record Range(int start, int end) {}
}