Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // And finally a functional example of the find location with non-zero inventory for item example
- public class Functional {
- // @Inject
- private ItemRepository itemRepository;
- // @Inject
- private InventoryLocationRepostitory inventoryLocationRepostitory;
- // Monadic approach, composed behaviors
- // "The monad represents computations with a sequential structure: a monad defines what it means to chain operations together."
- public Function<Optional<String>, Optional<InventoryLocation>> firstAvailableLocation = itemIdOpt ->
- itemIdOpt.flatMap(itemId ->
- itemRepository.findByItemId(itemId)
- .flatMap(item ->
- inventoryLocationRepostitory
- .findByItemId(item)
- .filter(inventoryLocation -> inventoryLocation.getInStock() > 0)
- .findFirst()));
- }
- // 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