21

Records, sealed interfaces, and pattern matching

Data-oriented programming in Java 25, with records, sealed hierarchies, and exhaustive pattern-matching switches.

Demo 21Source on GitHub ↗

Records, sealed interfaces, and pattern matching form one toolkit for modeling data and the operations over it. This module models shipping data as sealed hierarchies of records (ParcelKind, ShippingEvent, a nestable RateRule pricing grammar) and keeps operations as external exhaustive pattern-matching switches, including guarded patterns and record deconstruction nested several levels deep, with no accept() method on the data types.

The JUnit 5 tests verify each switch handles every variant correctly and the compact constructors reject invalid data.

Build, run, observe

cd examples/21-records-sealed-patterns
mvn -q test
./demo.sh

The module also runs from the aggregator root with mvn -q -f examples/pom.xml test.