WEBVTT

00:00.080 --> 00:00.590
Hi everyone.

00:00.590 --> 00:01.190
Welcome back.

00:01.190 --> 00:06.560
In this lecture we are going to learn about how to apply pattern matching on record classes.

00:06.560 --> 00:09.920
That's one of the reason why the name is called Record Patterns.

00:09.920 --> 00:14.060
Let me quickly explain the code that we are going to be exploring in this particular section.

00:14.060 --> 00:17.900
And then we'll go into experiment the record patterns in action.

00:17.900 --> 00:21.410
So the first thing is we have a animal seed interface.

00:21.410 --> 00:25.730
So this interface permits to classes which is cat and dog.

00:25.760 --> 00:29.000
The reason why I used interface is just to show the difference.

00:29.000 --> 00:33.110
You can use sealed with interfaces whenever it makes sense.

00:33.110 --> 00:38.300
It could be an abstract class also over here, but I just went ahead with the decision of using interface

00:38.330 --> 00:38.540
here.

00:38.570 --> 00:43.730
So in this case if you go and take a look at the cat class, this is a record class right.

00:43.730 --> 00:45.230
And then it has two properties.

00:45.230 --> 00:47.030
Number one is name and color.

00:47.030 --> 00:49.880
And then that implements the animal interface.

00:49.880 --> 00:52.250
And it's the same case for the dog one too.

00:52.280 --> 00:56.090
So the dog class also has two properties which is name and color.

00:56.090 --> 00:57.920
And that also implements animal.

00:57.920 --> 00:59.720
So the hierarchy makes sense here.

00:59.720 --> 01:02.480
And then if you open the animal service class.

01:02.480 --> 01:05.150
So in this case you will notice an implementation.

01:05.150 --> 01:09.630
So this is an implementation which is similar to what we saw in the previous lecture.

01:09.630 --> 01:11.730
So in this case this function accepts an animal.

01:11.730 --> 01:15.540
And then we have this enhanced switch to apply the pattern matching.

01:15.540 --> 01:18.210
So in this case if it's a null we return the empty string.

01:18.210 --> 01:21.720
If it's a cat type then we are going to be returning the name.

01:21.720 --> 01:23.100
So that's the name of this function.

01:23.100 --> 01:28.080
This function is going to get the animal type and then return the name for that particular animal type.

01:28.080 --> 01:30.930
So in the case of cat we create a binding variable.

01:30.930 --> 01:35.070
And then we use the binding variable to get the name and return that name.

01:35.070 --> 01:37.320
The same is applicable for the dog too.

01:37.350 --> 01:40.740
So this is very similar to what we saw in the previous lecture.

01:40.740 --> 01:45.120
So now what we are going to do is we are going to apply the record patterns for this one.

01:45.120 --> 01:46.170
So how do we do it.

01:46.200 --> 01:49.020
So in here what I'm going to do I'm going to create another function.

01:49.020 --> 01:51.930
So in this function I'm going to name this one as v two okay.

01:51.960 --> 01:59.220
So since it's a record class we know the record classes are basically data holders are immutable data

01:59.220 --> 01:59.910
containers.

01:59.940 --> 02:00.300
Right.

02:00.330 --> 02:03.930
So in this case what we are doing here is that we are just accessing the name.

02:04.170 --> 02:04.620
Right.

02:04.620 --> 02:06.930
We are not doing anything with the color at all.

02:06.930 --> 02:11.580
So in this case can we apply the pattern matching a level before.

02:11.580 --> 02:18.400
So in this case instead of creating an instance variable why not use the where name here and then where

02:18.430 --> 02:19.240
color here.

02:19.240 --> 02:23.500
So basically the idea is to extract the binding variable a level up.

02:23.530 --> 02:27.130
So in this case you don't have to use the instance variable to call the name.

02:27.130 --> 02:28.780
All you got to do is do this.

02:29.110 --> 02:30.430
That's all we need right.

02:30.430 --> 02:31.570
For the specific logic.

02:31.570 --> 02:33.970
So this is what Record Patterns is about.

02:33.970 --> 02:39.280
It's about taking it to the next level of matching and creating the binding variable in the case label

02:39.280 --> 02:39.640
itself.

02:39.640 --> 02:41.830
So in this case we can do the same thing for this one.

02:41.830 --> 02:44.230
So even here what we need is a name.

02:44.230 --> 02:46.120
Let's go ahead and return the name.

02:46.150 --> 02:46.810
There you go.

02:47.110 --> 02:50.410
So this code still compiles and works fine as expected.

02:50.410 --> 02:51.490
So what we will do.

02:51.490 --> 02:53.290
So there is a test case already available.

02:53.290 --> 02:53.890
There you go.

02:53.920 --> 02:55.210
Let me put this in the right.

02:56.020 --> 02:57.340
And in here what we are going to do.

02:57.370 --> 03:00.760
We are going to learn or write the test case for this function.

03:00.760 --> 03:02.800
So we have a retrieve name function here.

03:02.800 --> 03:04.300
And we have a test case for this one.

03:04.300 --> 03:07.900
And the test case is basically we are passing the input from this input function.

03:07.900 --> 03:11.050
So in the case of this one the name of the cat is going to be kitty.

03:11.080 --> 03:12.250
And the color is black.

03:12.250 --> 03:14.740
And the expected name is going to be kitty.

03:14.770 --> 03:15.100
Right.

03:15.130 --> 03:18.670
So because that's what this function returns, this function returns the name.

03:18.670 --> 03:21.190
And then we are performing the assertion over here.

03:21.190 --> 03:25.530
So we pass the animal and the expected value is going to be Kitty for this one.

03:25.530 --> 03:28.470
And we are asserting over here and similar for Scooby.

03:28.500 --> 03:30.720
The expected name is going to be Scooby.

03:30.720 --> 03:32.520
That's going to be returned from this function.

03:32.520 --> 03:37.440
So similar to this, what we are going to do, we are going to be writing the test case for the new

03:37.440 --> 03:40.650
function that we created using the record patterns approach.

03:40.650 --> 03:44.700
So in this case we can actually copy this one and then put it over here.

03:44.730 --> 03:48.930
And then in here we are going to be calling the retrieve name v2.

03:48.960 --> 03:54.240
So once you execute this function and if you get a green check mark, that means our code is working

03:54.240 --> 03:55.110
as expected.

03:55.140 --> 03:55.740
There you go.

03:55.770 --> 03:57.990
The code is working as expected.

03:57.990 --> 04:00.930
So we have three conditions that are being passed as an input.

04:00.960 --> 04:02.610
Number one is a cat type.

04:02.640 --> 04:05.610
Number two is a dog type, number three is a null type.

04:05.610 --> 04:09.270
And another thing that you notice here is that we don't have the default block here.

04:09.300 --> 04:16.080
The reason why we don't have a default block is because the animal has only two types of implementations.

04:16.080 --> 04:21.420
Number one is cat and dog, and we have the case label present for those two types.

04:21.420 --> 04:25.290
That's one of the reason the compiler isn't complaining about handling a default block.

04:25.290 --> 04:27.690
Since it's a record class, you don't need that.

04:27.690 --> 04:30.130
We covered that as part of the enhanced switch.

04:30.130 --> 04:34.630
If you have questions on this one, you can very well go back and watch the lecture that covers this

04:34.630 --> 04:37.000
exhaustive nature of the enhanced switch.

04:37.030 --> 04:41.650
Another thing that I want to call out is that the case null block I have it on the top right.

04:41.680 --> 04:43.390
It doesn't matter where you put it.

04:43.420 --> 04:45.070
You can just put the bottom two.

04:45.100 --> 04:47.320
The null will be handled as expected.

04:47.350 --> 04:49.960
Now let's go ahead and execute the test case one more time.

04:53.110 --> 04:53.590
There you go.

04:53.590 --> 04:55.210
We got the green check mark.

04:55.240 --> 04:56.470
As expected you see.

04:56.500 --> 04:56.800
Right.

04:56.830 --> 04:58.660
The null is handled as expected.

04:58.660 --> 05:04.090
So this is all about pattern matching on record classes which is also called record patterns.

05:04.090 --> 05:08.770
So in here we have this color binding variable created but it is not used right.

05:08.770 --> 05:12.280
There is an enhancement that's coming in the future part of the Java release.

05:12.280 --> 05:15.220
To not have this just to have something like this.

05:15.250 --> 05:20.860
So anytime you are going to not use a variable, you can have an underscore that is equivalent to a

05:20.860 --> 05:26.980
placeholder, not the actual variable, but it is not ready in Java 21 yet because that's the time I

05:27.010 --> 05:28.330
recorded this particular lecture.

05:28.330 --> 05:33.850
So for now you just need to deal with this redundant binding variable when you are doing record patterns.

05:33.850 --> 05:35.380
So this marks the end of this lecture.

05:35.380 --> 05:36.520
Thank you for watching.
