Cześć,
powiedzmy, że mam funkcję:
public static String getCode(Foo first, Supplier<Foo> second)
interface Foo {
String getCode();
}
chciałbym, żeby przechodziła test:
expect:
getCode(new FooImpl(first), { new FooImpl(second) }) == result
where:
first | second || result
'A' | 'B' || 'A'
null | 'B' || 'B'
null | null || 'default'
Jak byście napisali to w Javie 8? Próbowałem z ifami, ale robi się to mało czytelne i nie podoba mi się.
Optional.or
doszedł niestety w Java 9, ale chciałbym uzyskać coś podobnego:
public static String getCode(Foo first, Supplier<Foo> second) {
return ofNullable(first.getCode())
.or(() -> ofNullable(second.get().getCode()))
.orElse(DEFAULT);
}
Może znacie jakieś Utilsy (apache, guava), które mają już coś takiego?