Easy to implement in vanilla Java 8:
/**
* Check that <b>at least one</b> of the provided objects is not null
*/
@SafeVarargs
public static <T>
boolean anyNotNull(T... objs)
{
return anySatisfy(Objects::nonNull, objs);
}
/**
* Check that <b>at least one</b> of the provided objects satisfies predicate
*/
@SafeVarargs
public static <T>
boolean anySatisfy(Predicate<T> predicate, T... objs)
{
return Arrays.stream(objs).anyMatch(predicate);
}
/**
* Check that <b>all</b> of the provided objects satisfies predicate
*/
@SafeVarargs
public static <T>
boolean allSatisfy(Predicate<T> predicate, T... objs)
{
return Arrays.stream(objs).allMatch(predicate);
}
//... other methods are left as an exercise for the reader
(See what and why @SaveVarargs)
Usage:
// all are null
final boolean allNulls1 = !anyNotNull(obj1, obj2, obj3);
final boolean allNulls2 = allSatisfy(Objects::isNull, obj1, obj2, obj3);
// any of them is null
final boolean hasNull = anySatisfy(Objects::isNull, obj1, obj2, obj3);
P.S. General note for novice programmers. Guava is great library, but if you want it only because of some small and easy bit (like this one, or Strings.isNullOrEmpty
etc), IMO it's much better for your project to just implement the thing yourself and avoid extra dependency.