Skip to content

Instantly share code, notes, and snippets.

@rupertbates
Created November 28, 2014 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rupertbates/7d2bbf7cea695ee002d6 to your computer and use it in GitHub Desktop.
Save rupertbates/7d2bbf7cea695ee002d6 to your computer and use it in GitHub Desktop.
//Checking for null through an object graph quickly becomes unwieldy
if (article.getContributor() != null &&
article.getContributor().getImage() != null &&
article.getContributor().getImage().getSize() != null) {
Size size = article.getContributor().getImage().getSize();
setContributorImageSize(size.getWidth(), size.getHeight());
}
//Chaining multiple optional values using bind and map
article.getContributor()
.bind(Contributor::getImage)
.map(DisplayImage::getSize)
.map(size -> setContributorImageSize(size.getWidth(), size.getHeight()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment