Duplicate :
|
A DESCRIPTION OF THE REQUEST : please add java.util.function.Predicate.not( Predicate predicate ), I have found myself writing some variant of the following in every project since Java 8 public interface PredicateUtils { static <T> Predicate<T> not( Predicate<T> predicate ) { return predicate.negate(); } } JUSTIFICATION : it is hard to invert method references, here's my tests for the descriptions methods. @Test public void testNot() throws Exception { assertThat( "predicate", not( ( o ) -> true ), instanceOf( Predicate.class ) ); assertThat( "negates", PredicateUtils.<Boolean>not( ( o ) -> o ).test( true ), is( false ) ); assertThat( "method ref", Stream.of( null, null ).filter( not( Objects::nonNull ) ).count(), is( 2L ) ); } ---------- BEGIN SOURCE ---------- https://bitbucket.org/xenworks/util/src/a6ab361629033e16ffda6b8828fc6ccd41605cad/src/main/java/com/xenoterracide/util/PredicateUtils.java?at=master&fileviewer=file-view-default package com.xenoterracide.util; import java.util.function.Predicate; public interface PredicateUtils { static <T> Predicate<T> not( Predicate<T> predicate ) { return predicate.negate(); } } package com.xenoterracide.util; import org.junit.Test; import java.util.Objects; import java.util.function.Predicate; import java.util.stream.Stream; import static com.xenoterracide.util.PredicateUtils.not; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public class PredicateUtilsTest { @Test public void testNot() throws Exception { assertThat( "predicate", not( ( o ) -> true ), instanceOf( Predicate.class ) ); assertThat( "negates", PredicateUtils.<Boolean>not( ( o ) -> o ).test( true ), is( false ) ); assertThat( "method ref", Stream.of( null, null ).filter( not( Objects::nonNull ) ).count(), is( 2L ) ); } } ---------- END SOURCE ----------