Last active
May 13, 2018 13:02
-
-
Save softarn/52f4a899f0627f5b1f059095545ec09a to your computer and use it in GitHub Desktop.
Blog - Optional.orElse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public T orElse(T other) | |
public T orElseGet(Supplier<? extends T> other) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Optional<Inventory> findInventoryByUser(User user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Inventory getInventory(User user) { | |
Optional inventory = findInventoryByUser(User user); | |
if (inventory.isPresent()) { | |
return inventory.get(); | |
} else { | |
throw new NotFoundException(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Inventory getInventory(User user) { | |
return findInventoryByUser(userUuid) | |
.orElseGet(() -> createInventory(user)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Inventory getInventory(User user) { | |
return findInventoryByUser(User user) | |
.orElseThrow(() -> new NotFoundException()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Inventory getInventory(User user) { | |
return findInventoryByUser(userUuid) | |
.orElse(createInventory(user)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment