Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BoundPredicate |
|
| 2.5714285714285716;2.571 |
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 UnaryPredicate UnaryPredicate} | |
27 | * to the | |
28 | * {@link Predicate Predicate} interface | |
29 | * using a constant unary 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 | * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $ | |
39 | * @author Rodney Waldhoff | |
40 | */ | |
41 | public final class BoundPredicate implements Predicate, Serializable { | |
42 | /** | |
43 | * serialVersionUID declaration. | |
44 | */ | |
45 | private static final long serialVersionUID = -5721164265625291834L; | |
46 | /** The {@link UnaryPredicate UnaryPredicate} I'm wrapping. */ | |
47 | private final UnaryPredicate<Object> predicate; | |
48 | /** The parameter to pass to {@code predicate}. */ | |
49 | private final Object param; | |
50 | ||
51 | /** | |
52 | * Create a new BoundPredicate instance. | |
53 | * @param <A> input type | |
54 | * @param predicate the predicate to adapt | |
55 | * @param arg the constant argument to use | |
56 | */ | |
57 | @SuppressWarnings("unchecked") | |
58 | 30 | public <A> BoundPredicate(UnaryPredicate<? super A> predicate, A arg) { |
59 | 30 | if (predicate == null) { |
60 | 0 | throw new IllegalArgumentException("UnaryPredicate argument was null"); |
61 | } | |
62 | 30 | this.predicate = (UnaryPredicate<Object>) predicate; |
63 | 30 | this.param = arg; |
64 | 30 | } |
65 | ||
66 | /** | |
67 | * {@inheritDoc} | |
68 | */ | |
69 | public boolean test() { | |
70 | 30 | return predicate.test(param); |
71 | } | |
72 | ||
73 | /** | |
74 | * {@inheritDoc} | |
75 | */ | |
76 | public boolean equals(Object that) { | |
77 | 30 | return that == this || (that instanceof BoundPredicate && equals((BoundPredicate) that)); |
78 | } | |
79 | ||
80 | /** | |
81 | * Learn whether another BoundPredicate is equal to this. | |
82 | * @param that BoundPredicate to test | |
83 | * @return boolean | |
84 | */ | |
85 | public boolean equals(BoundPredicate that) { | |
86 | 22 | return null != that |
87 | && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)) | |
88 | && (null == param ? null == that.param : param.equals(that.param)); | |
89 | ||
90 | } | |
91 | ||
92 | /** | |
93 | * {@inheritDoc} | |
94 | */ | |
95 | public int hashCode() { | |
96 | 32 | int hash = "BoundPredicate".hashCode(); |
97 | 32 | if (null != predicate) { |
98 | 32 | hash <<= 2; |
99 | 32 | hash ^= predicate.hashCode(); |
100 | } | |
101 | 32 | if (null != param) { |
102 | 30 | hash <<= 2; |
103 | 30 | hash ^= param.hashCode(); |
104 | } | |
105 | 32 | return hash; |
106 | } | |
107 | ||
108 | /** | |
109 | * {@inheritDoc} | |
110 | */ | |
111 | public String toString() { | |
112 | 24 | return "BoundPredicate<" + predicate + "(" + param + ")>"; |
113 | } | |
114 | ||
115 | /** | |
116 | * Adapt the given, possibly-<code>null</code>, | |
117 | * {@link UnaryPredicate UnaryPredicate} to the | |
118 | * {@link Predicate Predicate} interface by binding | |
119 | * the specified <code>Object</code> as a constant | |
120 | * argument. | |
121 | * When the given <code>UnaryPredicate</code> is <code>null</code>, | |
122 | * returns <code>null</code>. | |
123 | * | |
124 | * @param <A> input type | |
125 | * @param predicate the possibly-<code>null</code> | |
126 | * {@link UnaryPredicate UnaryPredicate} to adapt | |
127 | * @param arg the object to bind as a constant argument | |
128 | * @return a <code>BoundPredicate</code> wrapping the given | |
129 | * {@link UnaryPredicate UnaryPredicate}, or <code>null</code> | |
130 | * if the given <code>UnaryPredicate</code> is <code>null</code> | |
131 | */ | |
132 | public static <A> BoundPredicate bind(UnaryPredicate<? super A> predicate, A arg) { | |
133 | 6 | return null == predicate ? null : new BoundPredicate(predicate, arg); |
134 | } | |
135 | ||
136 | } |