Javaにnull合体演算子が無いのでイケてないなあと思いつつ
Util関数を用意する次第
名称をcoalesce
として大体SQLの関数と同じ感じで使えるかなと
public final class Utils {
@SafeVarargs
public static <T> T coalesce(T... value) {
for (T v : value) {
if (v != null) {
return v;
}
}
return null;
}
}
public static void main() {
String foo = null;
String bar = null;
String hoge = Utils.coalesce(foo, bar, "huga");
System.out.println(hoge);
}
でも正直なところC#のような??
演算子が使いたいのです。