Skip to content

Instantly share code, notes, and snippets.

@adam-arold
Created September 11, 2017 16:31
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 adam-arold/074849246682406ac6faaac78461bae9 to your computer and use it in GitHub Desktop.
Save adam-arold/074849246682406ac6faaac78461bae9 to your computer and use it in GitHub Desktop.
Java user
public class JavaUser {
static class Address {
String city;
}
private final String firstName;
private final String lastName;
private final List<Address> addresses;
/**
* If you want to make sure nothing is `null`
* you have to check everything.
*/
public static String getFirstCity(JavaUser user) {
if(user != null && user.addresses != null && !user.addresses.isEmpty()) {
for(Address address : user.addresses) {
if(address.city != null) {
return address.city;
}
}
}
throw new IllegalArgumentException("This User has no cities!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment