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.BinaryPredicate;
22 import org.apache.commons.functor.UnaryPredicate;
23
24 /**
25 * Adapts a
26 * {@link BinaryPredicate BinaryPredicate}
27 * to the
28 * {@link UnaryPredicate UnaryPredicate} interface
29 * using a constant left-side argument.
30 * <p/>
31 * Note that although this class implements
32 * {@link Serializable}, a given instance will
33 * only be truly <code>Serializable</code> if the
34 * underlying objects are. Attempts to serialize
35 * an instance whose delegates are not
36 * <code>Serializable</code> will result in an exception.
37 *
38 * @param <A> the argument type.
39 * @version $Revision: 1157640 $ $Date: 2011-08-14 22:15:54 +0200 (Sun, 14 Aug 2011) $
40 * @author Rodney Waldhoff
41 */
42 public final class RightBoundPredicate<A> implements UnaryPredicate<A>, Serializable {
43 /**
44 * serialVersionUID declaration.
45 */
46 private static final long serialVersionUID = -1768544281714574302L;
47 /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */
48 private final BinaryPredicate<? super A, Object> predicate;
49 /** The parameter to pass to {@code predicate}. */
50 private final Object param;
51
52 /**
53 * Create a new RightBoundPredicate.
54 * @param <R> bound arg type
55 * @param predicate the predicate to adapt
56 * @param arg the constant argument to use
57 */
58 @SuppressWarnings("unchecked")
59 public <R> RightBoundPredicate(BinaryPredicate<? super A, ? super R> predicate, R arg) {
60 if (predicate == null) {
61 throw new IllegalArgumentException("BinaryPredicate argument was null");
62 }
63 this.predicate = (BinaryPredicate<? super A, Object>) predicate;
64 this.param = arg;
65 }
66
67 /**
68 * {@inheritDoc}
69 */
70 public boolean test(A obj) {
71 return predicate.test(obj, param);
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 public boolean equals(Object that) {
78 return that == this || (that instanceof RightBoundPredicate<?> && equals((RightBoundPredicate<?>) that));
79 }
80
81 /**
82 * Learn whether another RightBoundPredicate is equal to this.
83 * @param that RightBoundPredicate to test
84 * @return boolean
85 */
86 public boolean equals(RightBoundPredicate<?> that) {
87 return null != that
88 && (null == predicate ? null == that.predicate : predicate.equals(that.predicate))
89 && (null == param ? null == that.param : param.equals(that.param));
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 public int hashCode() {
96 int hash = "RightBoundPredicate".hashCode();
97 if (null != predicate) {
98 hash <<= 2;
99 hash ^= predicate.hashCode();
100 }
101 if (null != param) {
102 hash <<= 2;
103 hash ^= param.hashCode();
104 }
105 return hash;
106 }
107
108 /**
109 * {@inheritDoc}
110 */
111 public String toString() {
112 return "RightBoundPredicate<" + predicate + "(?," + param + ")>";
113 }
114
115 /**
116 * Adapt a BinaryPredicate as a UnaryPredicate.
117 * @param <L> the left argument type.
118 * @param <R> the right argument type.
119 * @param predicate to adapt
120 * @param arg right side
121 * @return RightBoundPredicate
122 */
123 public static <L, R> RightBoundPredicate<L> bind(BinaryPredicate<? super L, ? super R> predicate, R arg) {
124 return null == predicate ? null : new RightBoundPredicate<L>(predicate, arg);
125 }
126
127 }