I have a method findUrl()
that take a User
and a Permission
as parameters and queries the Database and implements some logic to find the URLs accessible to this User
in comma separated string.
I want to develop a null-safe version of this method taking same
User
and Permission
as parameters and then returning an Optional
which will be
empty if at least one of the values passed to it is empty or findUrl
method return null
. I wrote the below code but it seems like a null check based implementation and I don't see much benefit of using Optional to make code more concise.
public Optional<String> nullSafeFindUrl(User user, Permissions permissions) {
if ((Optional.ofNullable(user)).isPresent() && (Optional.ofNullable(permissions)).isPresent()) {
return Optional.ofNullable(findUrl(user.get(), permissions.get()));
} else {
return Optional.empty();
}
}
Is there a better way to do it to make code more readable ?