author | mduigou |
Tue, 19 Feb 2013 11:56:49 -0800 | |
changeset 16011 | 890a7ed97f6c |
parent 15647 | 314007859004 |
child 17695 | 032254f2467b |
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 |
/** |
30 |
* Determines if the input object matches some criteria. |
|
31 |
* |
|
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
32 |
* @param <T> the type of argument to {@code test} |
14670 | 33 |
* |
34 |
* @since 1.8 |
|
35 |
*/ |
|
15647
314007859004
8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents:
14856
diff
changeset
|
36 |
@FunctionalInterface |
14670 | 37 |
public interface Predicate<T> { |
38 |
||
39 |
/** |
|
40 |
* Returns {@code true} if the input object matches some criteria. |
|
41 |
* |
|
42 |
* @param t the input object |
|
43 |
* @return {@code true} if the input object matches some criteria, otherwise |
|
44 |
* {@code false} |
|
45 |
*/ |
|
46 |
public boolean test(T t); |
|
16011
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
47 |
|
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
48 |
/** |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
49 |
* Returns a predicate which evaluates to {@code true} only if this |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
50 |
* predicate and the provided predicate both evaluate to {@code true}. If |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
51 |
* this predicate returns {@code false} then the remaining predicate is not |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
52 |
* evaluated. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
53 |
* |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
54 |
* @param p a predicate which will be logically-ANDed with this predicate. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
55 |
* @return a new predicate which returns {@code true} only if both |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
56 |
* predicates return {@code true}. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
57 |
*/ |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
58 |
public default Predicate<T> and(Predicate<? super T> p) { |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
59 |
Objects.requireNonNull(p); |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
60 |
return (t) -> test(t) && p.test(t); |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
61 |
} |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
62 |
|
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
63 |
/** |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
64 |
* Returns a predicate which negates the result of this predicate. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
65 |
* |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
66 |
* @return a new predicate who's result is always the opposite of this |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
67 |
* predicate. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
68 |
*/ |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
69 |
public default Predicate<T> negate() { |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
70 |
return (t) -> !test(t); |
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 |
|
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
73 |
/** |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
74 |
* Returns a predicate which evaluates to {@code true} if either this |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
75 |
* predicate or the provided predicate evaluates to {@code true}. If this |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
76 |
* predicate returns {@code true} then the remaining predicate is not |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
77 |
* evaluated. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
78 |
* |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
79 |
* @param p a predicate which will be logically-ORed with this predicate. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
80 |
* @return a new predicate which returns {@code true} if either predicate |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
81 |
* returns {@code true}. |
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 |
public default Predicate<T> or(Predicate<? super T> p) { |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
84 |
Objects.requireNonNull(p); |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
85 |
return (t) -> test(t) || p.test(t); |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
86 |
} |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
87 |
|
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
88 |
/** |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
89 |
* Returns a predicate that evaluates to {@code true} if both or neither of |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
90 |
* the component predicates evaluate to {@code true}. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
91 |
* |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
92 |
* @param p a predicate which will be logically-XORed with this predicte. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
93 |
* @return a predicate that evaluates to {@code true} if both or neither of |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
94 |
* the component predicates evaluate to {@code true}. |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
95 |
*/ |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
96 |
public default Predicate<T> xor(Predicate<? super T> p) { |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
97 |
Objects.requireNonNull(p); |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
98 |
return (t) -> test(t) ^ p.test(t); |
890a7ed97f6c
8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents:
15647
diff
changeset
|
99 |
} |
14670 | 100 |
} |