Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8194272

Add a static method Predicate.not

    Details

      Description

      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 ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: