From efe15e323bb31ca626791c8f074cc85f123f2253 Mon Sep 17 00:00:00 2001 From: Jasper Siepkes Date: Wed, 7 Jun 2023 12:57:22 +0200 Subject: [PATCH] classlib: add not method to Predicate interface (#709) With this addition the Predicate interface is 100% complete for Java 17. --- .../java/util/function/TPredicate.java | 6 +- .../java/util/function/PredicateTest.java | 74 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tests/src/test/java/org/teavm/classlib/java/util/function/PredicateTest.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/function/TPredicate.java b/classlib/src/main/java/org/teavm/classlib/java/util/function/TPredicate.java index 4376962be..fd555c31f 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/function/TPredicate.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/function/TPredicate.java @@ -33,7 +33,11 @@ public interface TPredicate { return t -> test(t) || other.test(t); } - default TPredicate isEqual(Object targetRef) { + static TPredicate isEqual(Object targetRef) { return t -> TObjects.equals(t, targetRef); } + + static TPredicate not(TPredicate target) { + return (TPredicate) target.negate(); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/function/PredicateTest.java b/tests/src/test/java/org/teavm/classlib/java/util/function/PredicateTest.java new file mode 100644 index 000000000..6bc1e3eeb --- /dev/null +++ b/tests/src/test/java/org/teavm/classlib/java/util/function/PredicateTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2023 Jasper Siepkes. + * + * 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 org.teavm.classlib.java.util.function; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.function.Predicate; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.teavm.junit.TeaVMTestRunner; +import org.teavm.junit.WholeClassCompilation; + +@RunWith(TeaVMTestRunner.class) +@WholeClassCompilation +public class PredicateTest { + + @Test + public void andWorks() { + Predicate greaterThenFour = x -> x > 4; + Predicate greaterThenEight = x -> x > 8; + + assertTrue(greaterThenFour.and(greaterThenEight).test(10)); + assertFalse(greaterThenFour.and(greaterThenEight).test(6)); + } + + @Test + public void negateWorks() { + Predicate greaterThenFour = x -> x > 4; + + assertFalse(greaterThenFour.negate().test(10)); + assertTrue(greaterThenFour.negate().test(1)); + } + + @Test + public void orWorks() { + Predicate smallerThenFour = x -> x < 4; + Predicate greaterThenEight = x -> x > 8; + + assertTrue(smallerThenFour.or(greaterThenEight).test(3)); + assertFalse(smallerThenFour.or(greaterThenEight).test(6)); + assertTrue(smallerThenFour.or(greaterThenEight).test(9)); + } + + @Test + public void isEqualWorks() { + Predicate isThree = Predicate.isEqual(3); + + assertFalse(isThree.test(2)); + assertTrue(isThree.test(3)); + assertFalse(isThree.test(4)); + } + + @Test + public void notWorks() { + Predicate isThree = x -> x == 3; + + assertTrue(Predicate.not(isThree).test(2)); + assertFalse(Predicate.not(isThree).test(3)); + assertTrue(Predicate.not(isThree).test(4)); + } +}