JDK-8203428 : Predicate::not
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.util
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 11
  • Submitted: 2018-05-18
  • Updated: 2018-05-31
  • Resolved: 2018-05-25
Related Reports
CSR :  
Relates :  
Description
Summary
-------

Introduce a new static method Predicate::not which will allow
developers to negate predicate lambdas trivially.

Problem
-------

The requirement for predicate negation occurs frequently since predicates
are defined antipodal to a positive selection; isNull, isEmpty, isBlank.

Presently there is no easy way to negate a predicate lambda without
first wrapping in a Predicate Object.

```
Ex.

    List<String> list = bufferedReader
      .lines()
      .filter(((Predicate<String>)String::isEmpty).negate())
      .collect(toList()); 
```


Solution
--------

Introduce a method that accepts an unwrapped lambda and returns the negated
form.

```
Ex.

import static java.util.function.Predicate.not;
...

    List<String> list = bufferedReader
      .lines()
      .filter(not(String::isEmpty))
      .collect(toList()); 
```

Specification
-------------
```
    /**
     * Returns a predicate that is the negation of the
     * supplied predicate.
     * @param <T>     the type of arguments to the
     *                specified predicate
     * @param target  predicate to negate
     *
     * @return a predicate that negates the results
     *         of the supplied predicate
     *
     * @since 11
     */
    static <T> Predicate<T> not(Predicate<? super T> target) {
        return (Predicate<T>)target.negate();
    }
```
Comments
Moving to Approved.
25-05-2018