I have an interface and its parameter can be null
.
int count(@Nullable F filter);
To avoid null
parameters and null-checks, common advice is to use the method overloading:
int count();
int count(@NonNull F filter);
In some cases, I don't like this approach, because it forces clients to make this kind of job:
F f = getFilter();
int count = f != null ? count(f) : count();
It much more convenient for client to use:
count(getFilter())
Optionals, at the first glance, can solve this problem but Optional
should not be used for parameters https://rules.sonarsource.com/java/RSPEC-3553
So my question is: can I use Supplier
instead?
// service code:
int count(Supplier<F> filter) {
Optional.ofNullable(filter.get()).map(f -> count(f)).orElseGet(() -> count());
};
//client code:
count(() -> getFilter())
Or even
int count(Supplier<Optional<F>> filter)
Idea doesn't show any warnings.
I personally like my code after that, but isn't it cheating? Why it's better than Optional
?
EDIT: Thanks for comments. I agree, in my case Supplier
is just a sophisticated @Nullable
approach.
I will return to:
int count();
int count(@NonNull F filter);
/////
getFilter().map(f -> count(f)).orElseGet(() -> count());