Sub topic 10
Nesting Record Patterns
Assume you know Java classes, records, interfaces, instanceof, switch expressions, and basic generics. This chapter uses record patterns, which became a permanent Java feature in JDK 21.
A record pattern can match a record and extract its components. A nested record pattern does this at more than one level. For example, a Line record can contain two Point records. One pattern can open the line and both points together.
A record inside another record
Consider these records:
record Point(int x, int y) {}
record Line(Point start, Point end) {}A Line has two components. Each component is a Point, and each point has two integer components.
The traditional approach accesses each value through several accessor calls:
static boolean isHorizontal(Line line) {
return line.start().y() == line.end().y();
}A nested record pattern expresses the same structure in a switch:
static boolean isHorizontal(Object value) {
return switch (value) {
case Line(Point(var x1, var y1), Point(var x2, var y2)) ->
y1 == y2;
default -> false;
};
}The outer pattern, Line(...), checks that the value is a Line. Its first nested pattern extracts the start point. Its second nested pattern extracts the end point. The var bindings receive the component values.
The variables x1, y1, x2, and y2 exist only in the right side of that case. The example does not use x1 or x2, but they show the complete shape of the data.
Matching nested data
Nested patterns can also calculate a result from all extracted values:
record Point(int x, int y) {}
record Line(Point start, Point end) {}
public class Geometry {
static double length(Object value) {
return switch (value) {
case Line(Point(var x1, var y1), Point(var x2, var y2)) ->
Math.hypot(x2 - x1, y2 - y1);
default -> 0.0;
};
}
public static void main(String[] args) {
Line line = new Line(new Point(1, 2), new Point(4, 6));
System.out.println(length(line));
}
}The nested pattern matches only when every level matches. The value must be a Line, its start must be a Point, and its end must be a Point.
A nested component that is null does not match a nested record pattern. Therefore, this value selects the default case:
new Line(new Point(1, 2), null)If null is valid in your model, handle it with a separate case or avoid nullable components.
Combining nested patterns with guards
A guard can refine a complete nested match:
static String classify(Object value) {
return switch (value) {
case Line(Point(var x1, var y1), Point(var x2, var y2))
when y1 == y2 -> "horizontal";
case Line(Point(var x1, var y1), Point(var x2, var y2))
when x1 == x2 -> "vertical";
case Line line -> "diagonal or irregular";
default -> "not a line";
};
}The guarded cases must appear before the unguarded Line case. Otherwise, the general Line case would match first and prevent the guarded cases from being reached.
Exercise
What does isHorizontal(new Line(new Point(3, 5), new Point(9, 5))) return when it uses the nested pattern from this chapter?
Show answer
It returns true because both nested points match and their y components are equal.