Is there a shortcut to execute something only if its not null?
java
Solution
In Java 8:
static <T> boolean notNull(Supplier<T> getter, Predicate<T> tester) {
T x = getter.get();
return x != null && tester.test(x);
}
if (notNull(something::getThatObject, MyObject::someBooleanFunction)) {
...
}
If this style is new to the readers, one should keep in mind, that full functional programming is a bit nicer.
Problem
I find myself constantly writing this statement ``` MyObject myObject = something.getThatObject(); if( myObject !=null && myObject .someBooleanFunction()){ } ``` in order to prevent a null pointer exception. Is there a shortcut to this in Java? I'm thinking like `myObject..someBooleanFunction()`?