Guest User

Untitled

a guest
Apr 25th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. // And finally a functional example of the find location with non-zero inventory for item example
  2. public class Functional {
  3.  
  4. // @Inject
  5. private ItemRepository itemRepository;
  6. // @Inject
  7. private InventoryLocationRepostitory inventoryLocationRepostitory;
  8.  
  9. // Monadic approach, composed behaviors
  10. // "The monad represents computations with a sequential structure: a monad defines what it means to chain operations together."
  11. public Function<Optional<String>, Optional<InventoryLocation>> firstAvailableLocation = itemIdOpt ->
  12. itemIdOpt.flatMap(itemId ->
  13. itemRepository.findByItemId(itemId)
  14. .flatMap(item ->
  15. inventoryLocationRepostitory
  16. .findByItemId(item)
  17. .filter(inventoryLocation -> inventoryLocation.getInStock() > 0)
  18. .findFirst()));
  19. }
  20. // a few less lines, no null checks, the cruft is basically gone. Didn't bother to unwrap the optiona, we used it!
Add Comment
Please, Sign In to add comment