Skip to content

Instantly share code, notes, and snippets.

@leopku
Last active September 16, 2019 12:42
Show Gist options
  • Save leopku/7ecd658f2260030182251755eea8ed14 to your computer and use it in GitHub Desktop.
Save leopku/7ecd658f2260030182251755eea8ed14 to your computer and use it in GitHub Desktop.
Check a deep nest object properties in an elegant(Functional Programming) way.
Optional.ofNullable(product).map(
Product::getLatestVersion
).map(
ProductVersion::getProductData
).map(
ProductData::getTradeItem
).map(
TradeItemType::getInformationProviderOfTradeItem
).map(
PartyInRoleType::getGln
).orElse(null);
int entryListSize = io.vavr.control.Option.of(dataResponse)
.map(Response::data)
.map(FetchEntriesOfFeedQuery.Data::feed)
.map(FetchEntriesOfFeedQuery.Feed::entries)
.map(List::size)
.getOrElse(0);
@leopku
Copy link
Author

leopku commented Sep 16, 2019

Note: Lambda needs the API of Android >= 24.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment