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: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
40 */
41 public final class BinaryPredicateBinaryFunction<L, R> implements BinaryFunction<L, R, Boolean>, Serializable {
42 /**
43 * serialVersionUID declaration.
44 */
45 private static final long serialVersionUID = 207209665276797678L;
46 /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */
47 private final BinaryPredicate<? super L, ? super R> predicate;
48
49 /**
50 * Create a new BinaryPredicateBinaryFunction.
51 * @param predicate to adapt
52 */
53 public BinaryPredicateBinaryFunction(BinaryPredicate<? super L, ? super R> predicate) {
54 this.predicate = predicate;
55 }
56
57 /**
58 * {@inheritDoc}
59 * Returns <code>Boolean.TRUE</code> (<code>Boolean.FALSE</code>)
60 * when the {@link BinaryPredicate#test test} method of my underlying
61 * predicate returns <code>true</code> (<code>false</code>).
62 *
63 * @return a non-<code>null</code> <code>Boolean</code> instance
64 */
65 public Boolean evaluate(L left, R right) {
66 return predicate.test(left, right) ? Boolean.TRUE : Boolean.FALSE;
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 @Override
73 public boolean equals(Object that) {
74 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 return null != that && predicate.equals(that.predicate);
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override
92 public int hashCode() {
93 int hash = "BinaryPredicateBinaryFunction".hashCode();
94 hash ^= predicate.hashCode();
95 return hash;
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 @Override
102 public String toString() {
103 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 return null == predicate ? null : new BinaryPredicateBinaryFunction<L, R>(predicate);
123 }
124
125 }