Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BinaryPredicateBinaryFunction |
|
| 2.0;2 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.functor.adapter; | |
18 | ||
19 | import java.io.Serializable; | |
20 | ||
21 | import org.apache.commons.functor.BinaryFunction; | |
22 | import org.apache.commons.functor.BinaryPredicate; | |
23 | ||
24 | /** | |
25 | * Adapts a | |
26 | * {@link BinaryPredicate BinaryPredicate} | |
27 | * to the | |
28 | * {@link BinaryFunction BinaryFunction} interface. | |
29 | * <p/> | |
30 | * Note that although this class implements | |
31 | * {@link Serializable}, a given instance will | |
32 | * only be truly <code>Serializable</code> if the | |
33 | * underlying predicate is. Attempts to serialize | |
34 | * an instance whose delegate is not | |
35 | * <code>Serializable</code> will result in an exception. | |
36 | * | |
37 | * @param <L> the left argument type. | |
38 | * @param <R> the right argument type. | |
39 | * @version $Revision: 1156767 $ $Date: 2011-08-11 21:38:12 +0200 (Thu, 11 Aug 2011) $ | |
40 | * @author Rodney Waldhoff | |
41 | */ | |
42 | 8 | public final class BinaryPredicateBinaryFunction<L, R> implements BinaryFunction<L, R, Boolean>, Serializable { |
43 | /** | |
44 | * serialVersionUID declaration. | |
45 | */ | |
46 | private static final long serialVersionUID = 207209665276797678L; | |
47 | /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */ | |
48 | private final BinaryPredicate<? super L, ? super R> predicate; | |
49 | ||
50 | /** | |
51 | * Create a new BinaryPredicateBinaryFunction. | |
52 | * @param predicate to adapt | |
53 | */ | |
54 | 38 | public BinaryPredicateBinaryFunction(BinaryPredicate<? super L, ? super R> predicate) { |
55 | 38 | this.predicate = predicate; |
56 | 38 | } |
57 | ||
58 | /** | |
59 | * {@inheritDoc} | |
60 | * Returns <code>Boolean.TRUE</code> (<code>Boolean.FALSE</code>) | |
61 | * when the {@link BinaryPredicate#test test} method of my underlying | |
62 | * predicate returns <code>true</code> (<code>false</code>). | |
63 | * | |
64 | * @return a non-<code>null</code> <code>Boolean</code> instance | |
65 | */ | |
66 | public Boolean evaluate(L left, R right) { | |
67 | 8 | return predicate.test(left, right) ? Boolean.TRUE : Boolean.FALSE; |
68 | } | |
69 | ||
70 | /** | |
71 | * {@inheritDoc} | |
72 | */ | |
73 | public boolean equals(Object that) { | |
74 | 34 | return that == this |
75 | || (that instanceof BinaryPredicateBinaryFunction<?, ?> | |
76 | && equals((BinaryPredicateBinaryFunction<?, ?>) that)); | |
77 | } | |
78 | ||
79 | /** | |
80 | * Learn whether another BinaryPredicateBinaryFunction is equal to this. | |
81 | * @param that BinaryPredicateBinaryFunction to test | |
82 | * @return boolean | |
83 | */ | |
84 | public boolean equals(BinaryPredicateBinaryFunction<?, ?> that) { | |
85 | 26 | return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)); |
86 | } | |
87 | ||
88 | /** | |
89 | * {@inheritDoc} | |
90 | */ | |
91 | public int hashCode() { | |
92 | 44 | int hash = "BinaryPredicateBinaryFunction".hashCode(); |
93 | 44 | if (null != predicate) { |
94 | 44 | hash ^= predicate.hashCode(); |
95 | } | |
96 | 44 | return hash; |
97 | } | |
98 | ||
99 | /** | |
100 | * {@inheritDoc} | |
101 | */ | |
102 | public String toString() { | |
103 | 32 | return "BinaryPredicateBinaryFunction<" + predicate + ">"; |
104 | } | |
105 | ||
106 | /** | |
107 | * Adapt the given, possibly-<code>null</code>, | |
108 | * {@link BinaryPredicate BinaryPredicate} to the | |
109 | * {@link BinaryFunction BinaryFunction} interface. | |
110 | * When the given <code>BinaryPredicate</code> is <code>null</code>, | |
111 | * returns <code>null</code>. | |
112 | * | |
113 | * @param <L> left type | |
114 | * @param <R> right type | |
115 | * @param predicate the possibly-<code>null</code> | |
116 | * {@link BinaryPredicate BinaryPredicate} to adapt | |
117 | * @return a <code>BinaryPredicateBinaryFunction</code> wrapping the given | |
118 | * {@link BinaryPredicate BinaryPredicate}, or <code>null</code> | |
119 | * if the given <code>BinaryPredicate</code> is <code>null</code> | |
120 | */ | |
121 | public static <L, R> BinaryPredicateBinaryFunction<L, R> adapt(BinaryPredicate<? super L, ? super R> predicate) { | |
122 | 20 | return null == predicate ? null : new BinaryPredicateBinaryFunction<L, R>(predicate); |
123 | } | |
124 | ||
125 | } |