Skip to content

Commit

Permalink
Add 'not' method to Predicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
natros committed Nov 29, 2023
1 parent dd81ecf commit 65e3293
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ default Predicate<T> or(Predicate<? super T> other) {
checkCriticalNotNull(other);
return t -> test(t) || other.test(t);
}

@SuppressWarnings("unchecked")
static <T> Predicate<T> not(Predicate<? super T> other) {
return (Predicate<T>) other.negate();
}
}
2 changes: 2 additions & 0 deletions user/test/com/google/gwt/emultest/EmulJava11Suite.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.gwt.emultest.java11.util.OptionalIntTest;
import com.google.gwt.emultest.java11.util.OptionalLongTest;
import com.google.gwt.emultest.java11.util.OptionalTest;
import com.google.gwt.emultest.java11.util.function.PredicateTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

Expand All @@ -29,6 +30,7 @@
OptionalIntTest.class,
OptionalLongTest.class,
OptionalTest.class,
PredicateTest.class,
})
public class EmulJava11Suite {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.emultest.java11.util.function;

import com.google.gwt.emultest.java.util.EmulTestBase;
import java.util.function.Predicate;

public class PredicateTest extends EmulTestBase {
public void testIsEqual() {
Predicate<String> stringPredicate = Predicate.isEqual("test");
assertTrue(stringPredicate.test("test"));
assertFalse(stringPredicate.test("other string"));
}

public void testTest() {
Predicate<Integer> evenNumberPredicate = x -> x % 2 == 0;
assertTrue(evenNumberPredicate.test(2));
assertFalse(evenNumberPredicate.test(3));
}

public void testNegate() {
Predicate<Integer> evenNumberPredicate = x -> x % 2 == 0;
Predicate<Integer> notEvenPredicate = evenNumberPredicate.negate();
assertTrue(notEvenPredicate.test(3));
assertFalse(notEvenPredicate.test(2));
}

public void testAnd() {
Predicate<Integer> evenNumberPredicate = x -> x % 2 == 0;
Predicate<Integer> greaterThanFourPredicate = x -> x > 4;
Predicate<Integer> combinedPredicate = evenNumberPredicate.and(greaterThanFourPredicate);
assertTrue(combinedPredicate.test(6));
assertFalse(combinedPredicate.test(4));
}

public void testOr() {
Predicate<Integer> evenNumberPredicate = x -> x % 2 == 0;
Predicate<Integer> lessThanOrEqualToFourPredicate = x -> x <= 4;
Predicate<Integer> combinedPredicate = evenNumberPredicate.or(lessThanOrEqualToFourPredicate);
assertTrue(combinedPredicate.test(3));
assertFalse(combinedPredicate.test(5));
}

public void testNot() {
Predicate<Integer> evenNumberPredicate = x -> x % 2 == 0;
Predicate<Integer> notEvenPredicate = Predicate.not(evenNumberPredicate);
assertTrue(notEvenPredicate.test(3));
assertFalse(notEvenPredicate.test(2));
}
}

0 comments on commit 65e3293

Please sign in to comment.