Hi everyone.
Welcome back.
In this lecture we are going to learn about how to apply pattern matching on record classes.
That's one of the reason why the name is called Record Patterns.
Let me quickly explain the code that we are going to be exploring in this particular section.
And then we'll go into experiment the record patterns in action.
So the first thing is we have a animal seed interface.
So this interface permits to classes which is cat and dog.
The reason why I used interface is just to show the difference.
You can use sealed with interfaces whenever it makes sense.
It could be an abstract class also over here, but I just went ahead with the decision of using interface
here.
So in this case if you go and take a look at the cat class, this is a record class right.
And then it has two properties.
Number one is name and color.
And then that implements the animal interface.
And it's the same case for the dog one too.
So the dog class also has two properties which is name and color.
And that also implements animal.
So the hierarchy makes sense here.
And then if you open the animal service class.
So in this case you will notice an implementation.
So this is an implementation which is similar to what we saw in the previous lecture.
So in this case this function accepts an animal.
And then we have this enhanced switch to apply the pattern matching.
So in this case if it's a null we return the empty string.
If it's a cat type then we are going to be returning the name.
So that's the name of this function.
This function is going to get the animal type and then return the name for that particular animal type.
So in the case of cat we create a binding variable.
And then we use the binding variable to get the name and return that name.
The same is applicable for the dog too.
So this is very similar to what we saw in the previous lecture.
So now what we are going to do is we are going to apply the record patterns for this one.
So how do we do it.
So in here what I'm going to do I'm going to create another function.
So in this function I'm going to name this one as v two okay.
So since it's a record class we know the record classes are basically data holders are immutable data
containers.
Right.
So in this case what we are doing here is that we are just accessing the name.
Right.
We are not doing anything with the color at all.
So in this case can we apply the pattern matching a level before.
So in this case instead of creating an instance variable why not use the where name here and then where
color here.
So basically the idea is to extract the binding variable a level up.
So in this case you don't have to use the instance variable to call the name.
All you got to do is do this.
That's all we need right.
For the specific logic.
So this is what Record Patterns is about.
It's about taking it to the next level of matching and creating the binding variable in the case label
itself.
So in this case we can do the same thing for this one.
So even here what we need is a name.
Let's go ahead and return the name.
There you go.
So this code still compiles and works fine as expected.
So what we will do.
So there is a test case already available.
There you go.
Let me put this in the right.
And in here what we are going to do.
We are going to learn or write the test case for this function.
So we have a retrieve name function here.
And we have a test case for this one.
And the test case is basically we are passing the input from this input function.
So in the case of this one the name of the cat is going to be kitty.
And the color is black.
And the expected name is going to be kitty.
Right.
So because that's what this function returns, this function returns the name.
And then we are performing the assertion over here.
So we pass the animal and the expected value is going to be Kitty for this one.
And we are asserting over here and similar for Scooby.
The expected name is going to be Scooby.
That's going to be returned from this function.
So similar to this, what we are going to do, we are going to be writing the test case for the new
function that we created using the record patterns approach.
So in this case we can actually copy this one and then put it over here.
And then in here we are going to be calling the retrieve name v2.
So once you execute this function and if you get a green check mark, that means our code is working
as expected.
There you go.
The code is working as expected.
So we have three conditions that are being passed as an input.
Number one is a cat type.
Number two is a dog type, number three is a null type.
And another thing that you notice here is that we don't have the default block here.
The reason why we don't have a default block is because the animal has only two types of implementations.
Number one is cat and dog, and we have the case label present for those two types.
That's one of the reason the compiler isn't complaining about handling a default block.
Since it's a record class, you don't need that.
We covered that as part of the enhanced switch.
If you have questions on this one, you can very well go back and watch the lecture that covers this
exhaustive nature of the enhanced switch.
Another thing that I want to call out is that the case null block I have it on the top right.
It doesn't matter where you put it.
You can just put the bottom two.
The null will be handled as expected.
Now let's go ahead and execute the test case one more time.
There you go.
We got the green check mark.
As expected you see.
Right.
The null is handled as expected.
So this is all about pattern matching on record classes which is also called record patterns.
So in here we have this color binding variable created but it is not used right.
There is an enhancement that's coming in the future part of the Java release.
To not have this just to have something like this.
So anytime you are going to not use a variable, you can have an underscore that is equivalent to a
placeholder, not the actual variable, but it is not ready in Java 21 yet because that's the time I
recorded this particular lecture.
So for now you just need to deal with this redundant binding variable when you are doing record patterns.
So this marks the end of this lecture.
Thank you for watching.