I'm wondering if there is a shorter version for checking if any field of my ProfileDto is blank.
Upon searching the internet, I only found questions about how to check if a field is null or if all fields are null which is something totally different.
For context, if blank, it should take the respective field of the user object (which is just a call to the database). If notBlank, then it should take the ProfileDto field
private void setEmptyFieldsForUpdatedUser(User user, ProfileDto profileDto) {
String newFirstName = profileDto.getFirstName();
String newLastName = profileDto.getLastName();
String newEmailAdres = profileDto.getEmail();
String oldPassword = profileDto.getPassword();
String newPassword = profileDto.getNewPassword();
if (newFirstName == null) {
profileDto.setFirstName(user.getFirstName());
}
if (newLastName == null) {
profileDto.setLastName(user.getLastName());
}
if (newEmailAdres == null) {
profileDto.setEmail(user.getEmail());
}
}
This ProfileDto gives a JSON object. Which can have null-values. If it is null, I want to set the value with the previous user field which is in my database.
My database user has the following properties:
firstname: dj
lastname : test
email : dj@mail.com
password : qwerty
Input example:
{
"firstName": "freeman",
"lastName": null,
"email": null
"password": null,
"newPassword" : null
}
My output should become:
{
"firstName": "freeman",
"lastName": "test",
"email": "dj@mail.com",
"password": "qwerty"
}
Obviously, we can see that if I have 20 more variables that I need a lot of if's so I was wondering if there was a better way.