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