All Null, Any Null is possible guava Predicate

guava predicates
java predicate
guava functions
guava consumer
functional guava extensions
java predicate two arguments
guava collections
remove null from list java 8

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.

All Null, Any Null is possible guava Predicate, All Null, Any Null is possible guava Predicate all are null boolean bool1 = (obj1 == null && obj2 == null && obj3 == null); // any of them is null boolean bool2  If the object being tested is null this predicate evaluates to false. If you want to filter an Iterable to narrow its type, consider using Iterables.filter(Iterable, Class) in preference. Warning: contrary to the typical assumptions about predicates (as documented at Predicate.apply(T)), the returned predicate may not be consistent with equals.

Apparently you insist on having Predicates, which makes absolutely no sense in your use case. So maaartinus' answer is correct, but doesn't provide the answer with predicates. Here is a solution with predicates.

Predicate<Iterable<?>> anyIsNull = new Predicate<Iterable<?>>() {
    @Override public boolean apply(Iterable<?> it) {
        return Iterables.any(it, Predicates.isNull());
    }
};

Predicate<Iterable<?>> allAreNull = new Predicate<Iterable<?>>() {
    @Override public boolean apply(Iterable<?> it) {
        return Iterables.all(it, Predicates.isNull());
    }
};

Usage:

bool1 = anyIsNull.apply(Arrays.asList(obj1, obj2, obj3));
bool2 = allAreNull.apply(Arrays.asList(obj1, obj2, obj3));

Predicates (Guava: Google Core Libraries for Java 19.0 API), All methods returns serializable predicates as long as they're given If components is empty, the returned predicate will always evaluate to true . This approach doesn't block any potential users since it is always possible to use Predicates. Filtering and Transforming Collections in Guava. Last We'll be using an out of the box Predicate provided by the library and Remove Null Values While

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.

Tired of Null Pointer Exceptions? Consider Using Java SE 8's , Make your code more readable and protect it against null pointer exceptions. to safely navigate through potential null references. It is important to note that the intention of the Optional class is not to replace every single null reference. If a value is present in the Optional object and it matches the predicate, the filter  Returns an Optional containing the first element in this fluent iterable that satisfies the given predicate, if such an element exists. Warning: avoid using a predicate that matches null. If null is matched in this fluent iterable, a NullPointerException will be thrown.

Removing all nulls from a List in Java, How to remove all nulls from a List using plain Java, Guava, the Commons nulls using Guava and a more functional approach, via predicates: ? And that's it – some quick and very useful solutions for getting rid of all null  NullPointerException - if any elements of this fluent iterable is null, or if keyFunction produces null for any value Since: 14.0; toArray @GwtIncompatible(value="Array.newArray(Class, int)") @CheckReturnValue public final E[] toArray(Class<E> type)

Remove null values from a List in Java 8 and above, In this post, we will see how to remove null values from a list using streams in Java. It removes all elements of the list that satisfy the given predicate. of removing null values from a list, we can replace the null values with any custom value. In this post, we will see how to remove nulls from a List in Java using plain java, Guava library and Apache Commons Collections. 1. List.remove() List.remove(Object) removes the first occurrence of the specified object from the list. It returns true if object is removed from the list and returns false if object is not present in the list.

Java 8 - Optional Class, Returns an empty Optional instance. 2. boolean equals(Object obj). Indicates whether some other object is "equal to" this Optional. 3. The methods of the returned iterator may throw NullPointerException if any of the input iterators is null. Note: the current implementation is not suitable for nested concatenated iterators, i.e. the following should be avoided when in a loop: iterator = Iterators.concat(iterator, suffix); , since iteration over the resulting iterator has a

Comments
  • Do you need a precondition or predicate? Both are different.
  • I need predicate implementation with two methods which will cover those two booleans.
  • This question is plain wrong: you want a 3-parameters Predicate, where only one exists (you can use lists to match that). You want to kill a fly with a bazooka as your code is perfectly readable and makes complete sense; no need to use Guava. Finally, you want to use Predicates for no reason. I'll provide an answer that answer strictly to your question, but it still makes no sense.