I'm currently facing a code readability issue. The problem is the following:
Have three objects
// initialization skipped, all of three could be null as result of their initalization
Object obj1;
Object obj2;
Object obj3;
I want to create two booleans from them as follows:
// all are null
boolean bool1 = (obj1 == null && obj2 == null && obj3 == null);
// any of them is null
boolean bool2 = (obj1 == null || obj2 == null || obj3 == null);
I know guava provides built-in predicates like isNull
and notNull
.
Is there a way to implement a custom predicate that fulfills those two booleans? (assuming that .apply(..)
function would take 3 parameters then)
I'm not sure what you want, but the answer is most probably: Yes, but it makes little sense.
You can use
FluentIterable<Object> it =
FluentIterable.from(Lists.newArrayList(obj1, obj2, obj3));
boolean allNull = it.allMatch(Predicates.isNull());
boolean anyNull = it.anyMatch(Predicates.isNull());
but be assured that it's both much less readable and much slower than doing it the normal way.