On 04/29/2016 11:23 AM, Andrej Golovnin wrote:
The lines 406-415 can be rewritten as:

       otherMods
           .stream()
           .map(finder::find) // After this step you can filter out all
                              // empty Optionals.
                              // But I don't think it is really needed.
           .forEach(
               o -> o.ifPresent(mref -> {
                   map.putIfAbsent(mref.descriptor().name(), ref);
                   mrefs.add(mref);
               }
           );

...even more Stream-y (with JDK 9 changes to Optional):

otherMods
    .stream()
    .flatMap(mod -> finder.find(mod).stream())
    .forEach(mref -> ...);


Yes, it takes some time for people to get used to new tools before they use them optimally. This is a normal learning process. So I wonder whether it is only the method name that is to blame for the observed percentage of wrong or sub-optimal usages of Optional or would it be more-or-less the same with a more descriptive name at this time.

But the argument that getWhenPresent() is easier to spot than get() when reading code is on spot so at least some bugs would get spotted more quickly if the method stood out.

Regards, Peter

Reply via email to