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.UnaryFunction;
22 import org.apache.commons.functor.UnaryPredicate;
23
24 /**
25 * Adapts a <code>Boolean</code>-valued
26 * {@link UnaryFunction UnaryFunction}
27 * to the {@link UnaryPredicate UnaryPredicate}
28 * 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 function is. Attempts to serialize
34 * an instance whose delegate is not
35 * <code>Serializable</code> will result in an exception.
36 *
37 * @param <A> the argument type.
38 * @version $Revision: 1157648 $ $Date: 2011-08-14 22:22:47 +0200 (Sun, 14 Aug 2011) $
39 * @author Rodney Waldhoff
40 */
41 public final class UnaryFunctionUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
42 /**
43 * serialVersionUID declaration.
44 */
45 private static final long serialVersionUID = -9211927278252224707L;
46 /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
47 private final UnaryFunction<? super A, Boolean> function;
48
49 /**
50 * Create an {@link UnaryPredicate UnaryPredicate} wrapping
51 * the given {@link UnaryFunction UnaryFunction}.
52 * @param function the {@link UnaryFunction UnaryFunction} to wrap
53 */
54 public UnaryFunctionUnaryPredicate(UnaryFunction<? super A, Boolean> function) {
55 if (function == null) {
56 throw new IllegalArgumentException("UnaryFunction argument was null");
57 }
58 this.function = function;
59 }
60
61 /**
62 * {@inheritDoc}
63 * Returns the <code>boolean</code> value of the non-<code>null</code>
64 * <code>Boolean</code> returned by the {@link UnaryFunction#evaluate evaluate}
65 * method of my underlying function.
66 */
67 public boolean test(A obj) {
68 return function.evaluate(obj);
69 }
70
71 /**
72 * {@inheritDoc}
73 */
74 public boolean equals(Object that) {
75 return that == this
76 || (that instanceof UnaryFunctionUnaryPredicate<?> && equals((UnaryFunctionUnaryPredicate<?>) that));
77 }
78
79 /**
80 * Learn whether another UnaryFunctionUnaryPredicate is equal to this.
81 * @param that UnaryFunctionUnaryPredicate to test
82 * @return boolean
83 */
84 public boolean equals(UnaryFunctionUnaryPredicate<?> that) {
85 return null != that && (null == function ? null == that.function : function.equals(that.function));
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 public int hashCode() {
92 int hash = "UnaryFunctionUnaryPredicate".hashCode();
93 if (null != function) {
94 hash ^= function.hashCode();
95 }
96 return hash;
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 public String toString() {
103 return "UnaryFunctionUnaryPredicate<" + function + ">";
104 }
105
106 /**
107 * Adapt the given, possibly-<code>null</code>,
108 * {@link UnaryFunction UnaryFunction} to the
109 * {@link UnaryPredicate UnaryPredicate} interface.
110 * When the given <code>UnaryFunction</code> is <code>null</code>,
111 * returns <code>null</code>.
112 *
113 * @param <A> the argument type.
114 * @param function the possibly-<code>null</code>
115 * {@link UnaryFunction UnaryFunction} to adapt
116 * @return a {@link UnaryPredicate UnaryPredicate} wrapping the given
117 * {@link UnaryFunction UnaryFunction}, or <code>null</code>
118 * if the given <code>UnaryFunction</code> is <code>null</code>
119 */
120 public static <A> UnaryFunctionUnaryPredicate<A> adapt(UnaryFunction<? super A, Boolean> function) {
121 return null == function ? null : new UnaryFunctionUnaryPredicate<A>(function);
122 }
123
124 }