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