Created
September 11, 2017 16:31
-
-
Save adam-arold/074849246682406ac6faaac78461bae9 to your computer and use it in GitHub Desktop.
Java user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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