Skip to content

Instantly share code, notes, and snippets.

@struberg
Created January 30, 2017 12:38
Show Gist options
  • Save struberg/5d1182c230c357a2ab59135cd30a84a6 to your computer and use it in GitHub Desktop.
Save struberg/5d1182c230c357a2ab59135cd30a84a6 to your computer and use it in GitHub Desktop.
public Optional<String> getDings() {
String dings = ...;
return Optional.ofNullable(dings);
}
public void usageWithOptional(Optional<String> val) {
String x = val.orElse("defaultValue");
or
String x = val.get() // and boom if missing instead of Validate.notNull
}
public void usageWithoutOptional(String val) {... }
...
public void callIt() {
Optional<String> myval = getDings();
usageWithOptional(myval);
usageWithoutOptional(myval.orElse( "bla") or myval.get() ); ??? hell, I don't like that
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment