author | zmajo |
Fri, 03 Jul 2015 07:23:45 +0200 | |
changeset 31671 | 362e0c0acece |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
14670 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.util.function; |
|
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 | 29 |
/** |
19040 | 30 |
* Represents a predicate (boolean-valued function) of one argument. |
14670 | 31 |
* |
19040 | 32 |
* <p>This is a <a href="package-summary.html">functional interface</a> |
33 |
* whose functional method is {@link #test(Object)}. |
|
34 |
* |
|
35 |
* @param <T> the type of the input to the predicate |
|
14670 | 36 |
* |
37 |
* @since 1.8 |
|
38 |
*/ |
|
15647
314007859004
8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents:
14856
diff
changeset
|
39 |
@FunctionalInterface |
14670 | 40 |
public interface Predicate<T> { |
41 |
||
42 |
/** |
|
19040 | 43 |
* Evaluates this predicate on the given argument. |
14670 | 44 |
* |
19040 | 45 |
* @param t the input argument |
46 |
* @return {@code true} if the input argument matches the predicate, |
|
47 |
* otherwise {@code false} |
|
14670 | 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 | 52 |
* Returns a composed predicate that represents a short-circuiting logical |
53 |
* AND of this predicate and another. When evaluating the composed |
|
54 |
* predicate, if this predicate is {@code false}, then the {@code other} |
|
55 |
* predicate is not evaluated. |
|
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
56 |
* |
19040 | 57 |
* <p>Any exceptions thrown during evaluation of either predicate are relayed |
58 |
* to the caller; if evaluation of this predicate throws an exception, the |
|
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 | 61 |
* @param other a predicate that will be logically-ANDed with this |
62 |
* predicate |
|
63 |
* @return a composed predicate that represents the short-circuiting logical |
|
64 |
* AND of this predicate and the {@code other} predicate |
|
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 | 67 |
default Predicate<T> and(Predicate<? super T> other) { |
68 |
Objects.requireNonNull(other); |
|
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 | 73 |
* Returns a predicate that represents the logical negation of this |
74 |
* predicate. |
|
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
75 |
* |
19040 | 76 |
* @return a predicate that represents the logical negation of this |
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 | 84 |
* Returns a composed predicate that represents a short-circuiting logical |
85 |
* OR of this predicate and another. When evaluating the composed |
|
86 |
* predicate, if this predicate is {@code true}, then the {@code other} |
|
87 |
* predicate is not evaluated. |
|
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
88 |
* |
19040 | 89 |
* <p>Any exceptions thrown during evaluation of either predicate are relayed |
90 |
* to the caller; if evaluation of this predicate throws an exception, the |
|
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 | 93 |
* @param other a predicate that will be logically-ORed with this |
94 |
* predicate |
|
95 |
* @return a composed predicate that represents the short-circuiting logical |
|
96 |
* OR of this predicate and the {@code other} predicate |
|
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 | 99 |
default Predicate<T> or(Predicate<? super T> other) { |
100 |
Objects.requireNonNull(other); |
|
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 | 105 |
* Returns a predicate that tests if two arguments are equal according |
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 | 108 |
* @param <T> the type of arguments to the predicate |
109 |
* @param targetRef the object reference with which to compare for equality, |
|
110 |
* which may be {@code null} |
|
111 |
* @return a predicate that tests if two arguments are equal according |
|
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 | 114 |
static <T> Predicate<T> isEqual(Object targetRef) { |
115 |
return (null == targetRef) |
|
17695
032254f2467b
8004015: Additional static and instance utils for functional interfaces.
mduigou
parents:
16011
diff
changeset
|
116 |
? Objects::isNull |
19040 | 117 |
: object -> targetRef.equals(object); |
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
118 |
} |
14670 | 119 |
} |