Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConditionalUnaryFunction |
|
| 4.0;4 |
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.core.composite; | |
18 | ||
19 | import java.io.Serializable; | |
20 | ||
21 | import org.apache.commons.functor.UnaryFunction; | |
22 | import org.apache.commons.functor.UnaryPredicate; | |
23 | ||
24 | /** | |
25 | * A {@link UnaryFunction UnaryFunction} | |
26 | * similiar to Java's "ternary" | |
27 | * or "conditional" operator (<code>? :</code>). | |
28 | * Given a {@link UnaryPredicate predicate} | |
29 | * <i>p</i> and {@link UnaryFunction functions} | |
30 | * <i>f</i> and <i>g</i>, {@link #evaluate evalautes} | |
31 | * to | |
32 | * <code>p.test(x) ? f.evaluate(x) : g.evaluate(x)</code>. | |
33 | * <p> | |
34 | * Note that although this class implements | |
35 | * {@link Serializable}, a given instance will | |
36 | * only be truly <code>Serializable</code> if all the | |
37 | * underlying functors are. Attempts to serialize | |
38 | * an instance whose delegates are not all | |
39 | * <code>Serializable</code> will result in an exception. | |
40 | * </p> | |
41 | * @version $Revision: 1170778 $ $Date: 2011-09-14 21:15:01 +0200 (Wed, 14 Sep 2011) $ | |
42 | * @author Rodney Waldhoff | |
43 | */ | |
44 | public final class ConditionalUnaryFunction<A, T> implements UnaryFunction<A, T>, Serializable { | |
45 | /** | |
46 | * serialVersionUID declaration. | |
47 | */ | |
48 | private static final long serialVersionUID = -8152490481969255068L; | |
49 | ||
50 | /** Base hash integer used to shift hash */ | |
51 | private static final int HASH_SHIFT = 4; | |
52 | // attributes | |
53 | // ------------------------------------------------------------------------ | |
54 | private final UnaryPredicate<? super A> ifPred; | |
55 | private final UnaryFunction<? super A, ? extends T> thenFunc; | |
56 | private final UnaryFunction<? super A, ? extends T> elseFunc; | |
57 | ||
58 | // constructor | |
59 | // ------------------------------------------------------------------------ | |
60 | /** | |
61 | * Create a new ConditionalUnaryFunction. | |
62 | * @param ifPred if | |
63 | * @param thenFunc then | |
64 | * @param elseFunc else | |
65 | */ | |
66 | public ConditionalUnaryFunction(UnaryPredicate<? super A> ifPred, UnaryFunction<? super A, ? extends T> thenFunc, | |
67 | 20 | UnaryFunction<? super A, ? extends T> elseFunc) { |
68 | 20 | if (ifPred == null) { |
69 | 0 | throw new IllegalArgumentException("UnaryPredicate argument was null"); |
70 | } | |
71 | 20 | this.ifPred = ifPred; |
72 | 20 | if (thenFunc == null || elseFunc == null) { |
73 | 0 | throw new IllegalArgumentException("One or more UnaryFunction arguments was null"); |
74 | } | |
75 | 20 | this.thenFunc = thenFunc; |
76 | 20 | this.elseFunc = elseFunc; |
77 | 20 | } |
78 | ||
79 | // predicate interface | |
80 | // ------------------------------------------------------------------------ | |
81 | /** | |
82 | * {@inheritDoc} | |
83 | */ | |
84 | public T evaluate(A obj) { | |
85 | 4 | if (ifPred.test(obj)) { |
86 | 2 | return thenFunc.evaluate(obj); |
87 | } else { | |
88 | 2 | return elseFunc.evaluate(obj); |
89 | } | |
90 | } | |
91 | ||
92 | /** | |
93 | * {@inheritDoc} | |
94 | */ | |
95 | public boolean equals(Object that) { | |
96 | 24 | return that == this || (that instanceof ConditionalUnaryFunction<?, ?> |
97 | && equals((ConditionalUnaryFunction<?, ?>) that)); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Learn whether another ConditionalUnaryFunction is equal to this. | |
102 | * @param that ConditionalUnaryFunction to test | |
103 | * @return boolean | |
104 | */ | |
105 | public boolean equals(ConditionalUnaryFunction<?, ?> that) { | |
106 | 18 | return null != that |
107 | && (null == ifPred ? null == that.ifPred : ifPred.equals(that.ifPred)) | |
108 | && (null == thenFunc ? null == that.thenFunc : thenFunc.equals(that.thenFunc)) | |
109 | && (null == elseFunc ? null == that.elseFunc : elseFunc.equals(that.elseFunc)); | |
110 | } | |
111 | ||
112 | /** | |
113 | * {@inheritDoc} | |
114 | */ | |
115 | public int hashCode() { | |
116 | 26 | int hash = "ConditionalUnaryFunction".hashCode(); |
117 | 26 | if (null != ifPred) { |
118 | 26 | hash <<= HASH_SHIFT; |
119 | 26 | hash ^= ifPred.hashCode(); |
120 | } | |
121 | 26 | if (null != thenFunc) { |
122 | 26 | hash <<= HASH_SHIFT; |
123 | 26 | hash ^= thenFunc.hashCode(); |
124 | } | |
125 | 26 | if (null != elseFunc) { |
126 | 26 | hash <<= HASH_SHIFT; |
127 | 26 | hash ^= elseFunc.hashCode(); |
128 | } | |
129 | 26 | return hash; |
130 | } | |
131 | ||
132 | /** | |
133 | * {@inheritDoc} | |
134 | */ | |
135 | public String toString() { | |
136 | 18 | return "ConditionalUnaryFunction<" + ifPred + "?" + thenFunc + ":" + elseFunc + ">"; |
137 | } | |
138 | ||
139 | } |