这是
Java 8中可选类型的有效(预期)用法吗?
class Foo {
private final Integer id;
private final String name;
private final String countryCode;
Foo(final Integer id, final String name, final String countryCode) {
this.id = id;
this.name = name;
this. countryCode = countryCode;
}
public String getName() {
return name;
}
public String getCountryCode() {
return countryCode;
}
}
class FooSrv {
private Optional getFoo(final Integer id) {
return id != null && id == 1 ? Optional.of(new Foo(id, "Bar", "US")) : Optional.empty();
}
private boolean isFooValid(final Integer id) {
return getFoo(id)
.filter(f -> "Bar".equals(f.getName()) && "US".equals(f.getCountryCode()))
.map(f -> true)
.orElse(false);
}
}