Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UnaryPredicateUnaryFunction |
|
| 2.142857142857143;2.143 |
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 | |
26 | * {@link UnaryPredicate UnaryPredicate} | |
27 | * to the | |
28 | * {@link UnaryFunction UnaryFunction} 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 <A> the argument type. | |
38 | * @version $Revision: 1157655 $ $Date: 2011-08-14 22:25:24 +0200 (Sun, 14 Aug 2011) $ | |
39 | * @author Rodney Waldhoff | |
40 | */ | |
41 | 162 | public final class UnaryPredicateUnaryFunction<A> implements UnaryFunction<A, Boolean>, Serializable { |
42 | /** | |
43 | * serialVersionUID declaration. | |
44 | */ | |
45 | private static final long serialVersionUID = 5660724725036398625L; | |
46 | /** The {@link UnaryPredicate UnaryPredicate} I'm wrapping. */ | |
47 | private final UnaryPredicate<? super A> predicate; | |
48 | ||
49 | /** | |
50 | * Create a new UnaryPredicateUnaryFunction. | |
51 | * @param predicate to adapt | |
52 | */ | |
53 | 46 | public UnaryPredicateUnaryFunction(UnaryPredicate<? super A> predicate) { |
54 | 46 | if (predicate == null) { |
55 | 0 | throw new IllegalArgumentException("UnaryPredicate argument was null"); |
56 | } | |
57 | 46 | this.predicate = predicate; |
58 | 46 | } |
59 | ||
60 | /** | |
61 | * {@inheritDoc} | |
62 | * Returns <code>Boolean.TRUE</code> (<code>Boolean.FALSE</code>) | |
63 | * when the {@link UnaryPredicate#test test} method of my underlying | |
64 | * predicate returns <code>true</code> (<code>false</code>). | |
65 | * | |
66 | * @return a non-<code>null</code> <code>Boolean</code> instance | |
67 | */ | |
68 | public Boolean evaluate(A obj) { | |
69 | 162 | return predicate.test(obj); |
70 | } | |
71 | ||
72 | /** | |
73 | * {@inheritDoc} | |
74 | */ | |
75 | public boolean equals(Object that) { | |
76 | 68 | return that == this |
77 | || (that instanceof UnaryPredicateUnaryFunction<?> && equals((UnaryPredicateUnaryFunction<?>) that)); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Learn whether another UnaryPredicateUnaryFunction is equal to this. | |
82 | * @param that UnaryPredicateUnaryFunction to test | |
83 | * @return boolean | |
84 | */ | |
85 | public boolean equals(UnaryPredicateUnaryFunction<?> that) { | |
86 | 48 | return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)); |
87 | } | |
88 | ||
89 | /** | |
90 | * {@inheritDoc} | |
91 | */ | |
92 | public int hashCode() { | |
93 | 92 | int hash = "UnaryPredicateUnaryFunction".hashCode(); |
94 | 92 | if (null != predicate) { |
95 | 92 | hash ^= predicate.hashCode(); |
96 | } | |
97 | 92 | return hash; |
98 | } | |
99 | ||
100 | /** | |
101 | * {@inheritDoc} | |
102 | */ | |
103 | public String toString() { | |
104 | 76 | return "UnaryPredicateUnaryFunction<" + predicate + ">"; |
105 | } | |
106 | ||
107 | /** | |
108 | * Adapt a UnaryPredicate to the UnaryFunction interface. | |
109 | * @param <A> the argument type. | |
110 | * @param predicate to adapt | |
111 | * @return UnaryPredicateUnaryFunction | |
112 | */ | |
113 | public static <A> UnaryPredicateUnaryFunction<A> adapt(UnaryPredicate<? super A> predicate) { | |
114 | 2 | return null == predicate ? null : new UnaryPredicateUnaryFunction<A>(predicate); |
115 | } | |
116 | ||
117 | } |