Slide 1

Slide 1 text

Explore Java 17 Records, Sealed Classes and Pattern Matching José Paumard @JosePaumard Java Developer Advocate Java Platform Group

Slide 2

Slide 2 text

https://twitter.com/JosePaumard https://github.com/JosePaumard https://www.youtube.com/user/java https://www.youtube.com/user/JPaumard https://www.youtube.com/hashtag/jepcafe https://fr.slideshare.net/jpaumard https://www.pluralsight.com/authors/jose-paumard https://dev.java

Slide 3

Slide 3 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 3 Dev.java Java 8 Java 11

Slide 4

Slide 4 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 4 Records

Slide 5

Slide 5 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 5 https://github.com/JosePaumard/ 2022_DevNexus-lab

Slide 6

Slide 6 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 6 Short answer: a class! Less short answer: a class - That extends the Record class - That is final What is a Record?

Slide 7

Slide 7 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 7 And you can write: I Need to Write a Record! public record User(String name, int age) { } User sarah = new User("Sarah", 23); System.out.println(sarah); $ User[name=Sarah, age=23]

Slide 8

Slide 8 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 8 1) A constructor 2) Accessors: name() and age() 3) The following methods: toString(), equals(), hashCode() What Do you Get with a Record? public record User(String name, int age) { }

Slide 9

Slide 9 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 9 You cannot extend a record A record cannot extend anything You cannot create any instance field in a record You can implement interfaces You can add any instance method You can add any static field or method Restrictions on Records

Slide 10

Slide 10 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 10 name and age are the components of the record Components are both final instance fields And accessors Components of a Record public record User(String name, int age) { }

Slide 11

Slide 11 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 11 More on Components public record User(String name, int age) { } User user = new User("Sarah", 23); Class userClass = User.class; boolean isRecord = userClass.isRecord(); Component[] components = userClass.getRecordComponents();

Slide 12

Slide 12 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 12 More on Components public record User(String name, int age) { } User user = new User("Sarah", 23); RecordComponent nameComponent = components[0]; Class> declaring = nameComponent.getDeclaringRecord(); String name = (String) nameComponent.getAccessor()invoke(user);

Slide 13

Slide 13 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 13 More on Components public record User(@UserName String name, int age) { } @Target(ElementType.RECORD_COMPONENT) @interface UserName {}

Slide 14

Slide 14 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 14 The state of a record is entirely defined by its components - You cannot add more state by extension, or by extending anything - You cannot add any instance field Record State

Slide 15

Slide 15 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 15 The constructor of a record that takes its components is called the canonical constructor Record Building public record User(String name, int age) { public record User(String name, int age) { this.name = name; this.age = age; } }

Slide 16

Slide 16 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 16 Why would you write your own canonical constructor? 1) Defensive copy Record Building public record State(List cities) { public State(List cities) { this.cities = List.copyOf(cities); } }

Slide 17

Slide 17 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 17 In that case, you need to write your own accessor too! Record Building public record State(List cities) { public State(List cities) { this.cities = List.copyOf(cities); } public List cities() { return List.copyOf(cities); } }

Slide 18

Slide 18 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 18 Why would you write your own canonical constructor? 2) Validation Record Building public record Range(int begin, int end) { public Range(int begin, int end) { if (begin > end) { throw new IllegalArgumentException("..."); } this.begin = begin; this.end = end; } }

Slide 19

Slide 19 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 19 Why would you write your own canonical constructor? 2) Validation, with the Compact Constructor Record Building public record Range(int begin, int end) { public Range { if (begin > end) { throw new IllegalArgumentException("..."); } } }

Slide 20

Slide 20 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 20 A record constructor must call the canonical constructor Record Building public record Range(int begin, int end) { public Range(int end) { this.begin = 0; this.end = end; } }

Slide 21

Slide 21 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 21 A record constructor must call the canonical constructor Record Building public record Range(int begin, int end) { public Range(int end) { this(0, end); } }

Slide 22

Slide 22 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 22 You cannot build a record without calling its canonical constructor Deserialization calls the canonical constructor of a record

Slide 23

Slide 23 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 23 Deserialization calls the canonical constructor of a record Records are a perfect choice for Data Transport Objects Record Serialization

Slide 24

Slide 24 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 24 Sealed Types

Slide 25

Slide 25 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 25 Short answer: type (interface, class, abstract class)! Less short answer: a type - That declares its subtypes - That lives in the same package or module as them What is a Sealed Type?

Slide 26

Slide 26 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 26 A sealed type must declare its extensions Syntax public sealed interface Shape permits Square, Rectangle, WeirdShape { }

Slide 27

Slide 27 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 27 An extension of a sealed type must be: - final - or sealed - or non-sealed Syntax

Slide 28

Slide 28 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 28 An extension of a sealed type must be: - final Syntax public record Square(int edge) implements Shape { }

Slide 29

Slide 29 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 29 An extension of a sealed type must be: - sealed Syntax public sealed class Rectangle permits ExtendedRectangle { }

Slide 30

Slide 30 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 30 An extension of a sealed type must be: - non-sealed Syntax public non-sealed abstract class WeirdShape { }

Slide 31

Slide 31 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 31 Pattern Matching

Slide 32

Slide 32 text

?Record and Array Pattern Matching? Record Sealed Classes Switch Expression Constant Dynamic Inner Classes private in VM Nestmates Pattern Matching for instanceof 11 14 16 17 Switch on Types 19

Slide 33

Slide 33 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 33 In reference to the Amber Chronicles by Roger Zelazny Project Amber

Slide 34

Slide 34 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 34 A pattern is a combination of a match predicate that determines if the pattern matches a target, along with a set of pattern variables that are conditionally extracted if the pattern matches the target. Java 16 : Pattern Match for IntanceOf Pattern Matching for Java Gavin Bierman and Brian Goetz, September 2018 https://github.com/openjdk/amber-docs/blob/master/site/design-notes/pattern-matching-for-java.md

Slide 35

Slide 35 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 35 String name = the type of the pattern String = the name of the pattern name = the binding variable o = target operand Type Pattern if (o instanceof String name) { // some code }

Slide 36

Slide 36 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 36 Type Pattern if (o instanceof String name) { Sytem.out.println(name.toUpperCase()); } if (o instanceof String name && name.length() > 0) { Sytem.out.println(name.toUpperCase()); }

Slide 37

Slide 37 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 37 Type Pattern if (o instanceof String name) { Sytem.out.println(name.toUpperCase()); } if (!(o instanceof String name)) { // you cant use name here! } Sytem.out.println(name.toUpperCase());

Slide 38

Slide 38 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 38 Pattern Matching for Switch

Slide 39

Slide 39 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 39 Pattern Matching for Switch interface Shape {} record Square(int edge) implements Shape {} record Circle(int radius) implements Shape {}

Slide 40

Slide 40 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 40 Pattern Matching for Switch double area(Shape shape) { return switch (shape) { case Square square -> square.edge()*square.edge(); case Circle circle -> Math.PI*circle.radius()* circle.radius(); default -> 0; } }

Slide 41

Slide 41 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 41 Switch on a Sealed Type sealed interface Shape permits Square, Circle {} record Square(int edge) implements Shape {} record Circle(int radius) implements Shape {}

Slide 42

Slide 42 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 42 Switch on a Sealed Type double area(Shape shape) { return switch (shape) { case Square square -> square.edge()*square.edge(); case Circle circle -> Math.PI*circle.radius()* circle.radius(); // default -> 0; // Not needed! } }

Slide 43

Slide 43 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 43 The compiler can: - check if a type is not present and raise an error - raise a warning if an uneeded default is present Switch on a Sealed Type

Slide 44

Slide 44 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 44 Guarded Pattern (prev. 17 and 18) double area(Shape shape) { return switch (shape) { case Square square && square.edge() == 0 -> 0; case Square square -> square.edge()*square.edge(); case Circle circle -> Math.PI*circle.radius()* circle.radius(); } }

Slide 45

Slide 45 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 45 Guarded Pattern (prev. 19) double area(Shape shape) { return switch (shape) { case Square square when square.edge() == 0 -> 0; case Square square -> square.edge()*square.edge(); case Circle circle -> Math.PI*circle.radius()* circle.radius(); } }

Slide 46

Slide 46 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 46 (19 prev) Switch and Record Pattern Matching double area(Shape shape) { return switch (shape) { case Square(int edge) when edge == 0 -> 0; case Square(int edge) -> edge*edge; case Circle(int radius) -> Math.PI*radius*radius; } }

Slide 47

Slide 47 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 47 Record Pattern Matching (prev Java 19)

Slide 48

Slide 48 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 48 Record Pattern Matching record User(String name, int age) {} if (o instanceof User user)) { String name = user.name(); int age = user.age(); }

Slide 49

Slide 49 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 49 name and age are initialized by calling the record accessors So defensive copy is possible Record Pattern Matching record User(String name, int age) {} if (o instanceof User(String name, int age))) { // use name and age }

Slide 50

Slide 50 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 50 Record Pattern Matching record User(String name, int age) {} if (o instanceof User(String name, int age) user)) { // use name, age and user }

Slide 51

Slide 51 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 51 Array Pattern Matching

Slide 52

Slide 52 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 52 Array Pattern Matching if (o instanceof String[] array && array.length() >= 2) { // do something with array[0] and array[1] }

Slide 53

Slide 53 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 53 Array Pattern Matching if (o instanceof String[] {s1, s2}) { // do something with s1 and s2 }

Slide 54

Slide 54 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 54 Nesting Patterns + Type Inference if (o instanceof Circle[] {Circle(var r1), Circle(var r2)}) { // do something with r1 and r2 } record Circle(int radius) {}

Slide 55

Slide 55 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 55 Nesting Patterns + Type Inference if (o instanceof Circle[] { Circle(Point(int x1, int y1), _), Circle(Point(int x2, int y2), _)}) { // do something with x1, y1, x2, y2 } record Circle(Point center, int radius) {} record Point(int x, int y) {}

Slide 56

Slide 56 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 56 Nesting Patterns + Type Inference List points = ...; for (Point point: points) { int x = point.x(); int y = point.x(); // do something with x and y } record Point(int x, int y) {}

Slide 57

Slide 57 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 57 Nesting Patterns + Type Inference List points = ...; for (Point(int x, int y): points) { // do something with x and y } record Point(int x, int y) {}

Slide 58

Slide 58 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 58 The Future of Pattern Matching

Slide 59

Slide 59 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 59 Nested Patterns, needed binding variables, type inference Syntaxic Sugars if (shape instanceof Circle(var center, _) && center instanceof Point(int x, int y)) { // center and radius are binding variables } if (shape instanceof Circle(Point(int x, int y), _)) { // center and radius are binding variables }

Slide 60

Slide 60 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 60 The deconstruction uses the canonical constructor of a record What about: - factory methods? Deconstruction

Slide 61

Slide 61 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 61 Deconstruction Using Factory Methods interface Shape { static Circle circle(double radius) { return new Circle(radius); } static Square square(double edge) { return new Square(edge); } } record Circle(double radius) {} record Square(double edge) {}

Slide 62

Slide 62 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 62 Then this code could become possible: Deconstruction Using Factory Methods double area = switch(shape) { case Shape.circle(double radius) -> Math.PI*radius*radius; case Shape.square(double edge) -> edge*edge; }

Slide 63

Slide 63 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 63 With factory methods Deconstruction Using Factory Methods if (opt instanceof Optional.of(var max)) { // max is a binding variable }

Slide 64

Slide 64 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 64 With factory methods Deconstruction Using Factory Methods if (s instanceof String.format("%s is %d years old", String name, int age)) { // name and age are binding variables }

Slide 65

Slide 65 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 65 Map deconstruction This is an extended form of Pattern Matching where you check the value of a binding variable Deconstruction Using Factory Methods if (map instanceof Map.withMapping("name", var name) && map instanceof Map.withMapping("email", var email)) { // name and email are binding variables }

Slide 66

Slide 66 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 66 Pattern combination More Examples if (map instanceof Map.withMapping("name", var name) __AND Map.withMapping("email", var email)) { // name and email are binding variables } __AND = pattern combination

Slide 67

Slide 67 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 67 More Examples { "firstName": "John", "lastName": "Smith", "age": 25, "address" : { "street": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" } } if (json instanceof stringKey("firstName", var firstName) __AND stringKey("lastName", var lastName) __AND intKey("age", var age) __AND objectKey("address", stringKey("street", var street) __AND stringKey("city", var city) __AND stringKey("state", var state) )) { // firstName, lastName, age, // street, city, state, ... // are binding variables }

Slide 68

Slide 68 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 68 If Java Embraces « Map Literals » Map map = { "firstName": "John", "lastName": "Smith", "age": "25" } if (map instanceof { "firstName": var firstName, "lastName": var lastName, "age": Integer.toString(var age) }) { // firstName, lastName, age // are binding variables // age is a deconstructed int }

Slide 69

Slide 69 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 69 The deconstruction uses the canonical constructor of a record What about: - factory methods? - classes that are not records? Deconstruction

Slide 70

Slide 70 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 70 What About Your POJOs? public class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public deconstructor(int x, int y) { x = this.x; y = this.y; } } This is the output of the deconstructor It allows both: - overloading - defensive copy

Slide 71

Slide 71 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 71 Match can check if an expression matches a pattern Just as if checks for a boolean expression Introducing Match

Slide 72

Slide 72 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 72 Pattern with Match record Point(int x, int y) {} record Circle(Point center, int radius) implements Shape {} Circle circle = ...; match Circle(var center, var radius) = circle; // center and radius are binding variables

Slide 73

Slide 73 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 73 Pattern with Match record Point(int x, int y) {} record Circle(Point center, int radius) implements Shape {} Circle circle = ...; match Circle(var center, var radius) = circle else throw new IllegalStateException("Not a circle");

Slide 74

Slide 74 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 74 Pattern with Match record Point(int x, int y) {} record Circle(Point center, int radius) implements Shape {} Circle circle = ...; match Circle(var center, var radius) = circle else { center = new Point(0, 0); // this is called radius = 0; // an anonymous matcher }

Slide 75

Slide 75 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 75 You can use match with more than one pattern… … or use nested patterns Pattern with Match Shape shape = ...; match Rectangle(var p1, var p2) = shape, Point(var x0, var y0) = p1, Point(var x1, var y2) = p2;

Slide 76

Slide 76 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 76 • Constant Patterns: checks the operand with a constant value • Type Patterns: checks if the operand has the right type, casts it, and creates a binding variable Patterns at a Glance

Slide 77

Slide 77 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 77 • Patterns + Deconstruction: checks the operand type, casts it, bind the component to binding variables • Patterns + Method: uses a factory method or a deconstructor • Patterns + Var: infers the right type, and creates the binding variable • Pattern + _: infers the right type, but does not create the binding variable Patterns at a Glance

Slide 78

Slide 78 text

5/13/2022 Copyright © 2021, Oracle and/or its affiliates | 78 Where are we? • Pattern Matching for instanceof (16) • Pattern Matching for Switch (prev 17, 18, 19) • Record Pattern Matching (19) • Array Pattern Matching • Match • Literals Patterns at a Glance