From 3db69a5eb075459af5eb7f81b81b0f60ee88e933 Mon Sep 17 00:00:00 2001 From: Filipe Sousa Date: Sat, 11 Nov 2023 15:35:07 +0000 Subject: [PATCH] Add 'not' method to Predicate. --- .../emul/java/util/function/Predicate.java | 5 ++ .../google/gwt/emultest/EmulJava11Suite.java | 2 + .../java11/util/function/PredicateTest.java | 66 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 user/test/com/google/gwt/emultest/java11/util/function/PredicateTest.java diff --git a/user/super/com/google/gwt/emul/java/util/function/Predicate.java b/user/super/com/google/gwt/emul/java/util/function/Predicate.java index 6babe4f8783..b9ad769b307 100644 --- a/user/super/com/google/gwt/emul/java/util/function/Predicate.java +++ b/user/super/com/google/gwt/emul/java/util/function/Predicate.java @@ -47,4 +47,9 @@ default Predicate or(Predicate other) { checkCriticalNotNull(other); return t -> test(t) || other.test(t); } + + @SuppressWarnings("unchecked") + static Predicate not(Predicate other) { + return (Predicate) other.negate(); + } } diff --git a/user/test/com/google/gwt/emultest/EmulJava11Suite.java b/user/test/com/google/gwt/emultest/EmulJava11Suite.java index 18260e0714f..5e2ad3ab17e 100644 --- a/user/test/com/google/gwt/emultest/EmulJava11Suite.java +++ b/user/test/com/google/gwt/emultest/EmulJava11Suite.java @@ -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; @@ -29,6 +30,7 @@ OptionalIntTest.class, OptionalLongTest.class, OptionalTest.class, + PredicateTest.class, }) public class EmulJava11Suite { } diff --git a/user/test/com/google/gwt/emultest/java11/util/function/PredicateTest.java b/user/test/com/google/gwt/emultest/java11/util/function/PredicateTest.java new file mode 100644 index 00000000000..e2bf74df739 --- /dev/null +++ b/user/test/com/google/gwt/emultest/java11/util/function/PredicateTest.java @@ -0,0 +1,66 @@ +/* + * 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; + +/** + * A class that tests the functionality of the {@link Predicate} interface. + */ +public class PredicateTest extends EmulTestBase { + public void testIsEqual() { + Predicate stringPredicate = Predicate.isEqual("test"); + assertTrue(stringPredicate.test("test")); + assertFalse(stringPredicate.test("other string")); + } + + public void testTest() { + Predicate evenNumberPredicate = x -> x % 2 == 0; + assertTrue(evenNumberPredicate.test(2)); + assertFalse(evenNumberPredicate.test(3)); + } + + public void testNegate() { + Predicate evenNumberPredicate = x -> x % 2 == 0; + Predicate notEvenPredicate = evenNumberPredicate.negate(); + assertTrue(notEvenPredicate.test(3)); + assertFalse(notEvenPredicate.test(2)); + } + + public void testAnd() { + Predicate evenNumberPredicate = x -> x % 2 == 0; + Predicate greaterThanFourPredicate = x -> x > 4; + Predicate combinedPredicate = evenNumberPredicate.and(greaterThanFourPredicate); + assertTrue(combinedPredicate.test(6)); + assertFalse(combinedPredicate.test(4)); + } + + public void testOr() { + Predicate evenNumberPredicate = x -> x % 2 == 0; + Predicate lessThanOrEqualToFourPredicate = x -> x <= 4; + Predicate combinedPredicate = evenNumberPredicate.or(lessThanOrEqualToFourPredicate); + assertTrue(combinedPredicate.test(3)); + assertFalse(combinedPredicate.test(5)); + } + + public void testNot() { + Predicate evenNumberPredicate = x -> x % 2 == 0; + Predicate notEvenPredicate = Predicate.not(evenNumberPredicate); + assertTrue(notEvenPredicate.test(3)); + assertFalse(notEvenPredicate.test(2)); + } +}