Clément Béra | 26a495c | 2022-03-15 13:43:12 +0000 | [diff] [blame] | 1 | // Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package backport; |
| 6 | |
| 7 | import java.util.Objects; |
| 8 | import java.util.function.Predicate; |
| 9 | |
| 10 | public final class PredicateBackportJava11Main { |
| 11 | public static void main(String[] args) { |
| 12 | testNot(); |
| 13 | } |
| 14 | |
| 15 | private static void testNot() { |
| 16 | |
| 17 | Predicate<Object> isNull = Objects::isNull; |
| 18 | Predicate<Object> notNull = Predicate.not(isNull); |
| 19 | |
| 20 | assertEquals(notNull.test(null), false); |
| 21 | assertEquals(notNull.test("something"), true); |
| 22 | |
| 23 | try { |
| 24 | Predicate.not(null); |
| 25 | throw new AssertionError("Expected to throw NPE"); |
| 26 | } catch (Throwable t) { |
| 27 | // Expected. |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | private static void assertEquals(Object expected, Object actual) { |
| 32 | if (expected != actual && (expected == null || !expected.equals(actual))) { |
| 33 | throw new AssertionError("Expected <" + expected + "> but was <" + actual + '>'); |
| 34 | } |
| 35 | } |
| 36 | } |