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 two arguments.  This is
31607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * the two-arity specialization of {@link Predicate}.
32607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller *
33607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * <p>This is a <a href="package-summary.html">functional interface</a>
34607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * whose functional method is {@link #test(Object, Object)}.
35607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller *
36607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @param <T> the type of the first argument to the predicate
37607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @param <U> the type of the second argument the predicate
38607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller *
39607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @see Predicate
40607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller * @since 1.8
41607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller */
42607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller@FunctionalInterface
43607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fullerpublic interface BiPredicate<T, U> {
44607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller
45607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    /**
46607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * Evaluates this predicate on the given arguments.
47607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     *
48607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @param t the first input argument
49607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @param u the second input argument
50607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @return {@code true} if the input arguments match the predicate,
51607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * otherwise {@code false}
52607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     */
53607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    boolean test(T t, U u);
54607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller
55607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    /**
56607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * Returns a composed predicate that represents a short-circuiting logical
57607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * AND of this predicate and another.  When evaluating the composed
58607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * predicate, if this predicate is {@code false}, then the {@code other}
59607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * predicate is not evaluated.
60607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     *
61607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * <p>Any exceptions thrown during evaluation of either predicate are relayed
62607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * to the caller; if evaluation of this predicate throws an exception, the
63607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * {@code other} predicate will not be evaluated.
64607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     *
65607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @param other a predicate that will be logically-ANDed with this
66607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     *              predicate
67607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @return a composed predicate that represents the short-circuiting logical
68607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * AND of this predicate and the {@code other} predicate
69607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @throws NullPointerException if other is null
70607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     */
71607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) {
72607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller        Objects.requireNonNull(other);
73607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller        return (T t, U u) -> test(t, u) && other.test(t, u);
74607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    }
75607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller
76607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    /**
77607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * Returns a predicate that represents the logical negation of this
78607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * predicate.
79607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     *
80607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @return a predicate that represents the logical negation of this
81607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * predicate
82607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     */
83607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    default BiPredicate<T, U> negate() {
84607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller        return (T t, U u) -> !test(t, u);
85607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    }
86607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller
87607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    /**
88607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * Returns a composed predicate that represents a short-circuiting logical
89607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * OR of this predicate and another.  When evaluating the composed
90607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * predicate, if this predicate is {@code true}, then the {@code other}
91607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * predicate is not evaluated.
92607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     *
93607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * <p>Any exceptions thrown during evaluation of either predicate are relayed
94607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * to the caller; if evaluation of this predicate throws an exception, the
95607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * {@code other} predicate will not be evaluated.
96607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     *
97607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @param other a predicate that will be logically-ORed with this
98607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     *              predicate
99607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @return a composed predicate that represents the short-circuiting logical
100607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * OR of this predicate and the {@code other} predicate
101607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     * @throws NullPointerException if other is null
102607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller     */
103607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) {
104607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller        Objects.requireNonNull(other);
105607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller        return (T t, U u) -> test(t, u) || other.test(t, u);
106607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller    }
107607050af5a16c46ae53ff4d2c3f47b4ef694b559Neil Fuller}
108