Skip to content

Instantly share code, notes, and snippets.

@nipafx
Last active August 26, 2015 20:03
Show Gist options
  • Save nipafx/230614b4e0a1d11b89dd to your computer and use it in GitHub Desktop.
Save nipafx/230614b4e0a1d11b89dd to your computer and use it in GitHub Desktop.
Null-free Address
public class Address {
// look ma, no comments required
private final String addressLine;
private final String city;
private final Optional<String> postcode;
// nobody has to look at this constructor to check which parameters are
// allowed to be null because of course none are!
public Address(String addressLine, String city, Optional<String> postcode) {
this.addressLine = requireNonNull(addressLine,
"The argument 'addressLine' must not be null.");
this.city = requireNonNull(city,
"The argument 'city' must not be null.");
this.postcode = requireNonNull(postcode,
"The argument 'postcode' must not be null.");
}
// of course methods that might not have a result
// return 'Optional' instead of null
public static Optional<Address> findAddress(String userInput) {
// find the address, returning Optional.empty() if not found
}
// getters are straight forward and can be generated
public String getAddressLine() {
return addressLine;
}
public String getCity() {
return city;
}
// look how the field's type matches the getter's type;
// nice for bean-based code/tools
public Optional<String> getPostcode() {
return postcode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment