jdk/src/java.base/share/classes/java/util/function/Predicate.java
author zmajo
Fri, 03 Jul 2015 07:23:45 +0200
changeset 31671 362e0c0acece
parent 25859 3317bb8137f4
permissions -rw-r--r--
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics Summary: Annotate possibly intrinsified methods with @HotSpotIntrinsicCandidate. Add checks omitted by intrinsics to the library code. Add CheckIntrinsics flags to check consistency of intrinsics. Reviewed-by: jrose, kvn, thartmann, vlivanov, abuckley, darcy, ascarpino, briangoetz, alanb, aph, dnsimon
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14670
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
     1
/*
15647
314007859004 8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents: 14856
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
14670
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
     4
 *
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    10
 *
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    15
 * accompanied this code).
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    16
 *
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    20
 *
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    23
 * questions.
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    24
 */
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    25
package java.util.function;
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    26
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    27
import java.util.Objects;
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    28
14670
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    29
/**
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    30
 * Represents a predicate (boolean-valued function) of one argument.
14670
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    31
 *
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    32
 * <p>This is a <a href="package-summary.html">functional interface</a>
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    33
 * whose functional method is {@link #test(Object)}.
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    34
 *
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    35
 * @param <T> the type of the input to the predicate
14670
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    36
 *
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    37
 * @since 1.8
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    38
 */
15647
314007859004 8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents: 14856
diff changeset
    39
@FunctionalInterface
14670
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    40
public interface Predicate<T> {
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    41
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    42
    /**
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    43
     * Evaluates this predicate on the given argument.
14670
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    44
     *
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    45
     * @param t the input argument
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    46
     * @return {@code true} if the input argument matches the predicate,
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    47
     * otherwise {@code false}
14670
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
    48
     */
17695
032254f2467b 8004015: Additional static and instance utils for functional interfaces.
mduigou
parents: 16011
diff changeset
    49
    boolean test(T t);
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    50
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    51
    /**
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    52
     * Returns a composed predicate that represents a short-circuiting logical
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    53
     * AND of this predicate and another.  When evaluating the composed
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    54
     * predicate, if this predicate is {@code false}, then the {@code other}
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    55
     * predicate is not evaluated.
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    56
     *
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    57
     * <p>Any exceptions thrown during evaluation of either predicate are relayed
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    58
     * to the caller; if evaluation of this predicate throws an exception, the
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    59
     * {@code other} predicate will not be evaluated.
17695
032254f2467b 8004015: Additional static and instance utils for functional interfaces.
mduigou
parents: 16011
diff changeset
    60
     *
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    61
     * @param other a predicate that will be logically-ANDed with this
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    62
     *              predicate
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    63
     * @return a composed predicate that represents the short-circuiting logical
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    64
     * AND of this predicate and the {@code other} predicate
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    65
     * @throws NullPointerException if other is null
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    66
     */
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    67
    default Predicate<T> and(Predicate<? super T> other) {
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    68
        Objects.requireNonNull(other);
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    69
        return (t) -> test(t) && other.test(t);
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    70
    }
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    71
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    72
    /**
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    73
     * Returns a predicate that represents the logical negation of this
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    74
     * predicate.
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    75
     *
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    76
     * @return a predicate that represents the logical negation of this
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    77
     * predicate
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    78
     */
17695
032254f2467b 8004015: Additional static and instance utils for functional interfaces.
mduigou
parents: 16011
diff changeset
    79
    default Predicate<T> negate() {
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    80
        return (t) -> !test(t);
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    81
    }
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    82
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    83
    /**
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    84
     * Returns a composed predicate that represents a short-circuiting logical
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    85
     * OR of this predicate and another.  When evaluating the composed
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    86
     * predicate, if this predicate is {@code true}, then the {@code other}
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    87
     * predicate is not evaluated.
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    88
     *
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    89
     * <p>Any exceptions thrown during evaluation of either predicate are relayed
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    90
     * to the caller; if evaluation of this predicate throws an exception, the
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    91
     * {@code other} predicate will not be evaluated.
17695
032254f2467b 8004015: Additional static and instance utils for functional interfaces.
mduigou
parents: 16011
diff changeset
    92
     *
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    93
     * @param other a predicate that will be logically-ORed with this
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    94
     *              predicate
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    95
     * @return a composed predicate that represents the short-circuiting logical
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    96
     * OR of this predicate and the {@code other} predicate
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    97
     * @throws NullPointerException if other is null
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
    98
     */
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
    99
    default Predicate<T> or(Predicate<? super T> other) {
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   100
        Objects.requireNonNull(other);
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   101
        return (t) -> test(t) || other.test(t);
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
   102
    }
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
   103
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
   104
    /**
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   105
     * Returns a predicate that tests if two arguments are equal according
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   106
     * to {@link Objects#equals(Object, Object)}.
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
   107
     *
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   108
     * @param <T> the type of arguments to the predicate
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   109
     * @param targetRef the object reference with which to compare for equality,
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   110
     *               which may be {@code null}
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   111
     * @return a predicate that tests if two arguments are equal according
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   112
     * to {@link Objects#equals(Object, Object)}
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
   113
     */
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   114
    static <T> Predicate<T> isEqual(Object targetRef) {
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   115
        return (null == targetRef)
17695
032254f2467b 8004015: Additional static and instance utils for functional interfaces.
mduigou
parents: 16011
diff changeset
   116
                ? Objects::isNull
19040
7b25fde2a4ed 8019840: Spec updates for java.util.function
mduigou
parents: 17695
diff changeset
   117
                : object -> targetRef.equals(object);
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15647
diff changeset
   118
    }
14670
964ac9f1463c 8001634: Initial set of functional interface types
mduigou
parents:
diff changeset
   119
}