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.BinaryFunction;
22 import org.apache.commons.lang3.Validate;
23
24 /**
25 * A {@link BinaryFunction BinaryFunction} composed of
26 * three binary functions, <i>f</i>, <i>g</i> and <i>h</i>,
27 * evaluating the ordered parameters <i>x</i>, <i>y</i>
28 * to <code><i>f</i>(<i>g</i>(<i>x</i>,<i>y</i>),<i>h</i>(<i>x</i>,<i>y</i>))</code>.
29 * <p>
30 * Note that although this class implements
31 * {@link Serializable}, a given instance will
32 * only be truly <code>Serializable</code> if all the
33 * underlying functors are. Attempts to serialize
34 * an instance whose delegates are not all
35 * <code>Serializable</code> will result in an exception.
36 * </p>
37 * @param <L> the function left argument type.
38 * @param <R> the function right argument type.
39 * @param <T> the function returned value type.
40 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
41 */
42 public class BinaryCompositeBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
43
44 /**
45 * serialVersionUID declaration.
46 */
47 private static final long serialVersionUID = 2570517284319064043L;
48
49 /** Base hash integer used to shift hash. */
50 private static final int HASH_SHIFT = 4;
51
52 /**
53 * Type-remembering Helper.
54 *
55 * @param <G> the function left argument type.
56 * @param <H> the function right argument type.
57 */
58 private static class Helper<G, H, L, R, T> implements BinaryFunction<L, R, T>, Serializable {
59 /**
60 * serialVersionUID declaration.
61 */
62 private static final long serialVersionUID = 6013646799505641592L;
63 /**
64 * Global evaluator.
65 */
66 private BinaryFunction<? super G, ? super H, ? extends T> f;
67 /**
68 * This function evaluation will be the left argument of main evaluator.
69 */
70 private BinaryFunction<? super L, ? super R, ? extends G> g;
71 /**
72 * This function evaluation will be the right argument of main evaluator.
73 */
74 private BinaryFunction<? super L, ? super R, ? extends H> h;
75
76 /**
77 * Create a new Helper.
78 * @param f final BinaryFunction to evaluate
79 * @param g left preceding BinaryFunction
80 * @param h right preceding BinaryFunction
81 */
82 public Helper(BinaryFunction<? super G, ? super H, ? extends T> f,
83 BinaryFunction<? super L, ? super R, ? extends G> g,
84 BinaryFunction<? super L, ? super R, ? extends H> h) {
85 this.f = f;
86 this.g = g;
87 this.h = h;
88 }
89
90 /**
91 * {@inheritDoc}
92 */
93 public T evaluate(L left, R right) {
94 return f.evaluate(g.evaluate(left, right), h.evaluate(left, right));
95 }
96 }
97
98 /**
99 * The helper used for the evaluation.
100 */
101 private final Helper<?, ?, L, R, T> helper;
102
103 // constructor
104 // ------------------------------------------------------------------------
105 /**
106 * Create a new BinaryCompositeBinaryFunction.
107 *
108 * @param <G> the main function left argument type.
109 * @param <H> the main function right argument type.
110 * @param f final BinaryFunction to evaluate
111 * @param g left preceding BinaryFunction
112 * @param h right preceding BinaryFunction
113 */
114 public <G, H> BinaryCompositeBinaryFunction(BinaryFunction<? super G, ? super H, ? extends T> f,
115 BinaryFunction<? super L, ? super R, ? extends G> g, BinaryFunction<? super L, ? super R, ? extends H> h) {
116 this.helper = new Helper<G, H, L, R, T>(
117 Validate.notNull(f, "final BinaryFunction argument must not be null"),
118 Validate.notNull(g, "left preceding BinaryFunction argument must not be null"),
119 Validate.notNull(h, "right preceding BinaryFunction argument must not be null")
120 );
121 }
122
123 // function interface
124 // ------------------------------------------------------------------------
125 /**
126 * {@inheritDoc}
127 */
128 public final T evaluate(L left, R right) {
129 return helper.evaluate(left, right);
130 }
131
132 /**
133 * {@inheritDoc}
134 */
135 @Override
136 public final boolean equals(Object that) {
137 return that == this || (that instanceof BinaryCompositeBinaryFunction<?, ?, ?>
138 && equals((BinaryCompositeBinaryFunction<?, ?, ?>) that));
139 }
140
141 /**
142 * Learn whether another BinaryCompositeBinaryFunction is equal to this.
143 * @param that BinaryCompositeBinaryFunction to test
144 * @return boolean
145 */
146 public final boolean equals(BinaryCompositeBinaryFunction<?, ?, ?> that) {
147 return null != that
148 && helper.f.equals(that.helper.f)
149 && helper.g.equals(that.helper.g)
150 && helper.h.equals(that.helper.h);
151 }
152
153 /**
154 * {@inheritDoc}
155 */
156 @Override
157 public int hashCode() {
158 int hash = "BinaryCompositeBinaryFunction".hashCode();
159 hash <<= HASH_SHIFT;
160 hash ^= helper.f.hashCode();
161 hash <<= HASH_SHIFT;
162 hash ^= helper.g.hashCode();
163 hash <<= HASH_SHIFT;
164 hash ^= helper.h.hashCode();
165 return hash;
166 }
167
168 /**
169 * {@inheritDoc}
170 */
171 @Override
172 public String toString() {
173 return "BinaryCompositeBinaryFunction<" + helper.f + ";" + helper.g + ";" + helper.h + ">";
174 }
175
176 }