0

I have the following in my code:

Optional<Foo> optionalFoo = fooDAO.findByName(name);
Foo foo;
if(!optionalFoo.isPresent()) {
    foo = fooDAO.addFoo(name);
} else {
    foo = optionalFoo.get();
}

IntelliJ popped up a hint that said can be replaced with single expression in functional style. I tried replacing this block with:

Optional<Foo> optionalFoo = fooDAO.findByName(name);
Foo foo = optionalFoo.orElse(fooDAO.addFoo(name));

This caused tests to fail, as fooDAO.addFoo(name) is called regardless of whether or not the Optional is empty, and the logic within addFoo should not be run if the Foo already exists.

Is it possible to rewrite the original block functionally without calling fooDAO.addFoo(name) unless it's necessary?

Mike S
  • 1,451
  • 1
  • 16
  • 34
  • @Leviand I believe it's calling `fooDAO.addFoo(name)` in order to get its return value, which is what is passed to `orElse` – Mike S Aug 28 '18 at 15:13

3 Answers3

3

You're looking for orElseGet, which accepts a Supplier that is only invoked when the value is not present:

Foo foo = fooDAO.findByName(name)
    .orElseGet(() -> fooDAO.addFoo(name));

I think that name needs to be effectively final for this to work.

Zircon
  • 4,677
  • 15
  • 32
3

Use Optional.orElseGet with a lambda expression:

Optional<Foo> optionalFoo = fooDAO.findByName(name);
Foo foo = optionalFoo.orElseGet(()->fooDAO.addFoo(name));
robymus
  • 31
  • 1
1

orElse take a value as an argument, so the function you passed always executed.

To create foo if necessary , you should use orElseGet:

Foo foo = fooDAO.findByName(name).orElseGet(() -> fooDAO.addFoo(name));
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51