I have a situation in my java code, where I must to do a lot of null checks for string parameters:
someService.someUpdateFunc(Optional.ofNullable(personPhone.getName()).orElse(""),
Optional.ofNullable(personPhone.getNumber()).orElse(""),
Optional.ofNullable(personPhone.getDescription()).orElse("")
);
I know, that there is the special function in Google Guava, so it's possible to write code like that:
someService.someUpdateFunc(nullToEmpty(personPhone.getName()),
nullToEmpty(personPhone.getNumber()),
nullToEmpty(personPhone.getDescription())
);
But I like pure OOP, so that I try to reduce using of static methods in my code. Is there a special decorator/wrapper class for standard String in some library or framework, which could incapsulate this behavior? Something like:
class StrictString {
private final String origin;
public StrictString(String origin) {
this.origin = origin;
}
public String asString() {
return Optional.ofNullable(origin).orElse("");
}
}
Thanks a lot for helping.