I was just wondering if the following is the wrong use case for Optional. It probably is, because it looks nasty.
(It is regarding calling "Legacy" code that returns nulls.)
private static class Person {
private Address address;
private String name;
}
private static class Address {
private Integer housenr;
private String houseletter;
private String street;
}
public String getAddress(Person person) {
return Optional.ofNullable(person.getAddress())
.map(x -> Optional.ofNullable(x.getHousenr()).map(y -> y + " ").orElse("") +
Optional.ofNullable(x.getHouseletter()).map(y -> y + " ").orElse("") +
Optional.ofNullable(x.getStreet()).orElse(""))
.orElse("<unknown>");
}
// unit test if string representation of an address is properly generated
assertThat(getAddress(charlesBabbage))
.isEqualTo("4 l Regentstreet");
I should probably put the code in a method of the Person class and the Address class, so it doesn't bother me so much.
Or should I do it "the old way":
public String getAddress(Person person) {
if (person.getAddress() == null) {
return "<unknown>";
}
StringBuilder builder = new StringBuilder();
if (person.getAddress().getHousenr() != null) {
builder.append(person.getAddress().getHousenr() + " ");
}
if (person.getAddress().getHouseletter() != null) {
builder.append(person.getAddress().getHouseletter() + " ");
}
if (person.getAddress().getStreet() != null) {
builder.append(person.getAddress().getStreet() + " ");
}
return builder.toString();
}
Bear in mind that this is just an example. More fields can be added like affix., postoffice box, town/city, municipality, state, country (not to mention foreign addresses) exacerbating the problem.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…