1607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller/* 2607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 5607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * This code is free software; you can redistribute it and/or modify it 6607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * under the terms of the GNU General Public License version 2 only, as 7607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * published by the Free Software Foundation. Oracle designates this 8607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * particular file as subject to the "Classpath" exception as provided 9607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * by Oracle in the LICENSE file that accompanied this code. 10607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 11607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * This code is distributed in the hope that it will be useful, but WITHOUT 12607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * version 2 for more details (a copy is included in the LICENSE file that 15607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * accompanied this code). 16607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 17607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * You should have received a copy of the GNU General Public License version 18607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 2 along with this work; if not, write to the Free Software Foundation, 19607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 21607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * or visit www.oracle.com if you need additional information or have any 23607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * questions. 24607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller */ 25607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fullerpackage java.util.function; 26607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller 27607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fullerimport java.util.Objects; 28607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller 29607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller/** 30607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * Represents a predicate (boolean-valued function) of one argument. 31607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 32607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * <p>This is a <a href="package-summary.html">functional interface</a> 33607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * whose functional method is {@link #test(Object)}. 34607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 35607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @param <T> the type of the input to the predicate 36607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 37607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @since 1.8 38607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller */ 39607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller@FunctionalInterface 40607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fullerpublic interface Predicate<T> { 41607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller 42607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller /** 43607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * Evaluates this predicate on the given argument. 44607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 45607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @param t the input argument 46607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @return {@code true} if the input argument matches the predicate, 47607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * otherwise {@code false} 48607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller */ 49607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller boolean test(T t); 50607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller 51607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller /** 52607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * Returns a composed predicate that represents a short-circuiting logical 53607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * AND of this predicate and another. When evaluating the composed 54607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * predicate, if this predicate is {@code false}, then the {@code other} 55607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * predicate is not evaluated. 56607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 57607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * <p>Any exceptions thrown during evaluation of either predicate are relayed 58607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * to the caller; if evaluation of this predicate throws an exception, the 59607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * {@code other} predicate will not be evaluated. 60607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 61607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @param other a predicate that will be logically-ANDed with this 62607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * predicate 63607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @return a composed predicate that represents the short-circuiting logical 64607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * AND of this predicate and the {@code other} predicate 65607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @throws NullPointerException if other is null 66607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller */ 67607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller default Predicate<T> and(Predicate<? super T> other) { 68607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller Objects.requireNonNull(other); 69607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller return (t) -> test(t) && other.test(t); 70607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller } 71607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller 72607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller /** 73607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * Returns a predicate that represents the logical negation of this 74607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * predicate. 75607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 76607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @return a predicate that represents the logical negation of this 77607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * predicate 78607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller */ 79607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller default Predicate<T> negate() { 80607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller return (t) -> !test(t); 81607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller } 82607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller 83607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller /** 84607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * Returns a composed predicate that represents a short-circuiting logical 85607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * OR of this predicate and another. When evaluating the composed 86607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * predicate, if this predicate is {@code true}, then the {@code other} 87607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * predicate is not evaluated. 88607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 89607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * <p>Any exceptions thrown during evaluation of either predicate are relayed 90607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * to the caller; if evaluation of this predicate throws an exception, the 91607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * {@code other} predicate will not be evaluated. 92607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 93607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @param other a predicate that will be logically-ORed with this 94607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * predicate 95607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @return a composed predicate that represents the short-circuiting logical 96607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * OR of this predicate and the {@code other} predicate 97607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @throws NullPointerException if other is null 98607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller */ 99607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller default Predicate<T> or(Predicate<? super T> other) { 100607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller Objects.requireNonNull(other); 101607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller return (t) -> test(t) || other.test(t); 102607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller } 103607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller 104607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller /** 105607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * Returns a predicate that tests if two arguments are equal according 106607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * to {@link Objects#equals(Object, Object)}. 107607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * 108607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @param <T> the type of arguments to the predicate 109607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @param targetRef the object reference with which to compare for equality, 110607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * which may be {@code null} 111607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @return a predicate that tests if two arguments are equal according 112607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * to {@link Objects#equals(Object, Object)} 113607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller */ 114607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller static <T> Predicate<T> isEqual(Object targetRef) { 115607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller return (null == targetRef) 116607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller ? Objects::isNull 117607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller : object -> targetRef.equals(object); 118607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller } 119607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller} 120