Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PredicateUnaryPredicate |
|
| 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.Predicate; | |
22 | import org.apache.commons.functor.UnaryPredicate; | |
23 | ||
24 | /** | |
25 | * Adapts a | |
26 | * {@link Predicate Predicate} | |
27 | * to the | |
28 | * {@link UnaryPredicate UnaryPredicate} interface | |
29 | * by ignoring the given 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 functor is. Attempts to serialize | |
35 | * an instance whose delegate is not | |
36 | * <code>Serializable</code> will result in an exception. | |
37 | * | |
38 | * @param <A> the argument type. | |
39 | * @version $Revision: 1157628 $ $Date: 2011-08-14 22:04:52 +0200 (Sun, 14 Aug 2011) $ | |
40 | * @author Rodney Waldhoff | |
41 | */ | |
42 | public final class PredicateUnaryPredicate<A> implements UnaryPredicate<A>, Serializable { | |
43 | /** | |
44 | * serialVersionUID declaration. | |
45 | */ | |
46 | private static final long serialVersionUID = -5168896606842881702L; | |
47 | /** The {@link Predicate Predicate} I'm wrapping. */ | |
48 | private final Predicate predicate; | |
49 | ||
50 | /** | |
51 | * Create a new PredicateUnaryPredicate. | |
52 | * @param predicate to adapt | |
53 | */ | |
54 | 18 | public PredicateUnaryPredicate(Predicate predicate) { |
55 | 18 | if (predicate == null) { |
56 | 0 | throw new IllegalArgumentException("Predicate argument was null"); |
57 | } | |
58 | 18 | this.predicate = predicate; |
59 | 18 | } |
60 | ||
61 | /** | |
62 | * {@inheritDoc} | |
63 | */ | |
64 | public boolean test(Object obj) { | |
65 | 2 | return predicate.test(); |
66 | } | |
67 | ||
68 | /** | |
69 | * {@inheritDoc} | |
70 | */ | |
71 | public boolean equals(Object that) { | |
72 | 24 | return that == this || (that instanceof PredicateUnaryPredicate<?> |
73 | && equals((PredicateUnaryPredicate<?>) that)); | |
74 | } | |
75 | ||
76 | /** | |
77 | * Learn whether a given PredicateUnaryPredicate is equal to this. | |
78 | * @param that PredicateUnaryPredicate to test | |
79 | * @return boolean | |
80 | */ | |
81 | public boolean equals(PredicateUnaryPredicate<?> that) { | |
82 | 14 | return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)); |
83 | } | |
84 | ||
85 | /** | |
86 | * {@inheritDoc} | |
87 | */ | |
88 | public int hashCode() { | |
89 | 26 | int hash = "PredicateUnaryPredicate".hashCode(); |
90 | 26 | if (null != predicate) { |
91 | 26 | hash ^= predicate.hashCode(); |
92 | } | |
93 | 26 | return hash; |
94 | } | |
95 | ||
96 | /** | |
97 | * {@inheritDoc} | |
98 | */ | |
99 | public String toString() { | |
100 | 18 | return "PredicateUnaryPredicate<" + predicate + ">"; |
101 | } | |
102 | ||
103 | /** | |
104 | * Adapt a Predicate to the UnaryPredicate interface. | |
105 | * @param <A> the argument type. | |
106 | * @param predicate to adapt | |
107 | * @return PredicateUnaryPredicate<A> | |
108 | */ | |
109 | public static <A> PredicateUnaryPredicate<A> adapt(Predicate predicate) { | |
110 | 4 | return null == predicate ? null : new PredicateUnaryPredicate<A>(predicate); |
111 | } | |
112 | ||
113 | } |