Hi everyone.
Welcome back.
In this lecture we are going to code and learn about the next type of pattern matching called guarded
patterns.
So guarded patterns is an advanced level to the record patterns.
So in this case what we are going to do is we are going to use the same example.
And then we are going to implement the guarded patterns using pattern matching.
So in this case there you go.
So I'm going to name this function as retrieve name using guarded pattern.
So guarded pattern is a kind of a pattern where you can apply checks on the binding variable that we
have over here.
Which here means we don't just rely on the pattern matching on the type and the variable on top of it.
We can apply one another check.
So in this case what we can do is in here the value name can still be null right.
So with this implementation we can still return a null value right.
Which is not desirable as per this functionality.
So in this case what we can do is we can do a when block.
And then we can call when name equal to equal to null return empty string Okay.
Else you return the actual name.
So in this case, we don't need this check anymore because the null is handled on the above case label.
And then what we are going to do we are going to return the name over here.
There we go.
So this is a guarded pattern where we are actually applying the case label for type matching.
On top of it you apply one another check to fulfill your business functionality.
So this specific implementation is a guarded pattern.
This is supported from Java 21.
So in this case what we will do we'll go ahead and write a test over here.
So you go over here.
So rest of the input is going to be behaving as expected.
So in this case if I pass a name as null instead of kitty what would be the response.
Right.
So in this case let's go ahead and write a test case for the new function that we created.
There you go.
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
passing the null over here.
So if I pass a null value over here.
So this is going to give me a empty string.
Right.
So in this case.
So I'm not going to use the method source for this particular test.
I'm going to be passing the empty string over here.
Okay.
So this can be a regular test function.
There you go.
I don't think we need this one.
There we go.
So now what we are going to do we are going to run this test.
So the change we made is we added a null check for the actual record binding variable.
So if this value is null then we are going to be returning the empty string.
So in this case we are passing.
So in this case we are passing the name of the cat to be null.
So we expect the empty string to be the response.
So the name is going to be this one.
And the expected result is going to be empty string I don't think these are needed.
We can go ahead and remove that.
There you go.
So now what we will do.
We'll go ahead and execute this test case.
There you go.
Our test case passed as expected.
So this is how you implement a guarded pattern on record classes.
I hope you all have a pretty good idea about how to implement guarded patterns on record classes.
This marks the end of this lecture.
Thank you for watching.