@Test public void testSafe() { A a = new A(null); assertNull(a.safe(A::getB).safe(B::getC)); a = new A(new B(new C("v"))); assertEquals("v", a.safe(A::getB).safe(B::getC).safe(C::getValue)); assertEquals("v", LangUtil.safe(a, A::getB, B::getC, C::getValue)); }
public static <T,R> R safe(T obj, Function<T,R> getter) { return safeOrElse(obj, getter, null); }
LangUtil { public static <T,R> R safe(T obj, Function<T,R> getter) { return safeOrElse(obj, getter, null); } }
LangUtil { public static <T,R> R safe(T obj, Function<T,R> getter) { return safeOrElse(obj, getter, null); }  }
LangUtil { public static <T,R> R safe(T obj, Function<T,R> getter) { return safeOrElse(obj, getter, null); }  static R safeOrElse(T obj, Function<T,R> getter, R def); static R safe(T obj, Function<T,R> getter); static R2 safeOrElse(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, R2 def); static R2 safe(T obj, Function<T,R1> getter1, Function<R1,R2> getter2); static R3 safeOrElse(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, R3 def); static R3 safe(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3); static R4 safeOrElse(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, Function<R3,R4> getter4, R4 def); static R4 safe(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, Function<R3,R4> getter4); static R5 safeOrElse(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, Function<R3,R4> getter4, Function<R4,R5> getter5, R5 def); static R5 safe(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, Function<R3,R4> getter4, Function<R4,R5> getter5); static void safeConsume(T obj, Consumer<T> action); static void doIf(boolean condition, Runnable action); static void doIfElse(boolean condition, Runnable ifAction, Runnable elseAction); static T orElse(T value, T fullBack); static T orElse(T value, Supplier<T> fullBack); }
LangUtil { public static <T,R> R safe(T obj, Function<T,R> getter) { return safeOrElse(obj, getter, null); }  static R safeOrElse(T obj, Function<T,R> getter, R def); static R safe(T obj, Function<T,R> getter); static R2 safeOrElse(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, R2 def); static R2 safe(T obj, Function<T,R1> getter1, Function<R1,R2> getter2); static R3 safeOrElse(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, R3 def); static R3 safe(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3); static R4 safeOrElse(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, Function<R3,R4> getter4, R4 def); static R4 safe(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, Function<R3,R4> getter4); static R5 safeOrElse(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, Function<R3,R4> getter4, Function<R4,R5> getter5, R5 def); static R5 safe(T obj, Function<T,R1> getter1, Function<R1,R2> getter2, Function<R2,R3> getter3, Function<R3,R4> getter4, Function<R4,R5> getter5); static void safeConsume(T obj, Consumer<T> action); static void doIf(boolean condition, Runnable action); static void doIfElse(boolean condition, Runnable ifAction, Runnable elseAction); static T orElse(T value, T fullBack); static T orElse(T value, Supplier<T> fullBack);  }