WEBVTT

00:00.110 --> 00:00.650
Hi everyone.

00:00.650 --> 00:01.220
Welcome back.

00:01.220 --> 00:06.140
In this lecture we are going to code and learn about the next type of pattern matching called guarded

00:06.140 --> 00:06.770
patterns.

00:06.770 --> 00:10.670
So guarded patterns is an advanced level to the record patterns.

00:10.670 --> 00:13.940
So in this case what we are going to do is we are going to use the same example.

00:13.940 --> 00:18.140
And then we are going to implement the guarded patterns using pattern matching.

00:18.140 --> 00:19.850
So in this case there you go.

00:19.850 --> 00:25.820
So I'm going to name this function as retrieve name using guarded pattern.

00:25.820 --> 00:31.700
So guarded pattern is a kind of a pattern where you can apply checks on the binding variable that we

00:31.700 --> 00:32.690
have over here.

00:33.410 --> 00:39.590
Which here means we don't just rely on the pattern matching on the type and the variable on top of it.

00:39.620 --> 00:41.630
We can apply one another check.

00:41.630 --> 00:47.570
So in this case what we can do is in here the value name can still be null right.

00:47.570 --> 00:51.350
So with this implementation we can still return a null value right.

00:51.380 --> 00:54.200
Which is not desirable as per this functionality.

00:54.200 --> 00:56.840
So in this case what we can do is we can do a when block.

00:56.840 --> 01:05.790
And then we can call when name equal to equal to null return empty string Okay.

01:06.090 --> 01:08.850
Else you return the actual name.

01:08.880 --> 01:13.620
So in this case, we don't need this check anymore because the null is handled on the above case label.

01:13.620 --> 01:17.040
And then what we are going to do we are going to return the name over here.

01:17.070 --> 01:17.640
There we go.

01:17.640 --> 01:24.150
So this is a guarded pattern where we are actually applying the case label for type matching.

01:24.150 --> 01:28.890
On top of it you apply one another check to fulfill your business functionality.

01:28.890 --> 01:31.800
So this specific implementation is a guarded pattern.

01:31.800 --> 01:33.960
This is supported from Java 21.

01:33.960 --> 01:37.110
So in this case what we will do we'll go ahead and write a test over here.

01:37.410 --> 01:39.120
So you go over here.

01:39.120 --> 01:42.780
So rest of the input is going to be behaving as expected.

01:42.780 --> 01:47.700
So in this case if I pass a name as null instead of kitty what would be the response.

01:47.730 --> 01:47.940
Right.

01:47.970 --> 01:52.140
So in this case let's go ahead and write a test case for the new function that we created.

01:52.140 --> 01:52.890
There you go.

01:52.890 --> 01:59.280
And then what I'm going to do I'm going to pass the animal value as instead of this I'm going to be

01:59.280 --> 02:01.290
passing the null over here.

02:01.320 --> 02:03.390
So if I pass a null value over here.

02:03.390 --> 02:05.700
So this is going to give me a empty string.

02:05.700 --> 02:06.060
Right.

02:06.060 --> 02:07.170
So in this case.

02:07.170 --> 02:10.240
So I'm not going to use the method source for this particular test.

02:10.240 --> 02:13.480
I'm going to be passing the empty string over here.

02:13.540 --> 02:13.870
Okay.

02:13.900 --> 02:16.540
So this can be a regular test function.

02:18.550 --> 02:18.850
There you go.

02:18.850 --> 02:20.230
I don't think we need this one.

02:20.290 --> 02:20.770
There we go.

02:20.770 --> 02:23.830
So now what we are going to do we are going to run this test.

02:23.830 --> 02:29.950
So the change we made is we added a null check for the actual record binding variable.

02:29.950 --> 02:33.520
So if this value is null then we are going to be returning the empty string.

02:33.520 --> 02:34.720
So in this case we are passing.

02:34.750 --> 02:38.560
So in this case we are passing the name of the cat to be null.

02:38.560 --> 02:41.530
So we expect the empty string to be the response.

02:41.530 --> 02:46.210
So the name is going to be this one.

02:46.210 --> 02:50.890
And the expected result is going to be empty string I don't think these are needed.

02:50.890 --> 02:52.330
We can go ahead and remove that.

02:52.360 --> 02:52.840
There you go.

02:52.840 --> 02:53.740
So now what we will do.

02:53.770 --> 02:56.440
We'll go ahead and execute this test case.

02:58.270 --> 02:58.630
There you go.

02:58.660 --> 03:00.430
Our test case passed as expected.

03:00.430 --> 03:05.440
So this is how you implement a guarded pattern on record classes.

03:05.470 --> 03:10.660
I hope you all have a pretty good idea about how to implement guarded patterns on record classes.

03:10.660 --> 03:11.980
This marks the end of this lecture.

03:11.980 --> 03:13.060
Thank you for watching.
