Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TransposedFunction |
|
| 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.core.composite; | |
18 | ||
19 | import java.io.Serializable; | |
20 | ||
21 | import org.apache.commons.functor.BinaryFunction; | |
22 | ||
23 | /** | |
24 | * Transposes (swaps) the arguments to some other | |
25 | * {@link BinaryFunction function}. | |
26 | * For example, given a function <i>f</i> | |
27 | * and the ordered pair of arguments <i>a</i>, | |
28 | * <i>b</i>. | |
29 | * {@link #evaluate evaluates} to | |
30 | * <code>f.evaluate(b,a)</code>. | |
31 | * <p> | |
32 | * Note that although this class implements | |
33 | * {@link Serializable}, a given instance will | |
34 | * only be truly <code>Serializable</code> if the | |
35 | * underlying functor is. Attempts to serialize | |
36 | * an instance whose delegate is not | |
37 | * <code>Serializable</code> will result in an exception. | |
38 | * </p> | |
39 | * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $ | |
40 | * @author Rodney Waldhoff | |
41 | */ | |
42 | public class TransposedFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable { | |
43 | /** | |
44 | * serialVersionUID declaration. | |
45 | */ | |
46 | private static final long serialVersionUID = -5824252875453493940L; | |
47 | // attributes | |
48 | // ------------------------------------------------------------------------ | |
49 | private final BinaryFunction<? super R, ? super L, ? extends T> function; | |
50 | ||
51 | // constructor | |
52 | // ------------------------------------------------------------------------ | |
53 | /** | |
54 | * Create a new TransposedFunction. | |
55 | * @param function BinaryFunction to transpose. | |
56 | */ | |
57 | 18 | public TransposedFunction(BinaryFunction<? super R, ? super L, ? extends T> function) { |
58 | 18 | if (function == null) { |
59 | 0 | throw new IllegalArgumentException("BinaryFunction argument was null"); |
60 | } | |
61 | 18 | this.function = function; |
62 | 18 | } |
63 | ||
64 | // functor interface | |
65 | // ------------------------------------------------------------------------ | |
66 | /** | |
67 | * {@inheritDoc} | |
68 | */ | |
69 | public final T evaluate(L left, R right) { | |
70 | 4 | return function.evaluate(right, left); |
71 | } | |
72 | ||
73 | /** | |
74 | * {@inheritDoc} | |
75 | */ | |
76 | public final boolean equals(Object that) { | |
77 | 22 | return that == this || (that instanceof TransposedFunction<?, ?, ?> |
78 | && equals((TransposedFunction<?, ?, ?>) that)); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Learn whether another TransposedFunction is equal to this. | |
83 | * @param that TransposedFunction to test | |
84 | * @return boolean | |
85 | */ | |
86 | public final boolean equals(TransposedFunction<?, ?, ?> that) { | |
87 | 14 | return null != that && (null == function ? null == that.function : function.equals(that.function)); |
88 | } | |
89 | ||
90 | /** | |
91 | * {@inheritDoc} | |
92 | */ | |
93 | public int hashCode() { | |
94 | 24 | int hash = "TransposedFunction".hashCode(); |
95 | 24 | if (null != function) { |
96 | 24 | hash ^= function.hashCode(); |
97 | } | |
98 | 24 | return hash; |
99 | } | |
100 | ||
101 | /** | |
102 | * {@inheritDoc} | |
103 | */ | |
104 | public String toString() { | |
105 | 16 | return "TransposedFunction<" + function + ">"; |
106 | } | |
107 | ||
108 | // static | |
109 | // ------------------------------------------------------------------------ | |
110 | /** | |
111 | * Transpose a BinaryFunction. | |
112 | * @param f BinaryFunction to transpose | |
113 | * @return TransposedFunction | |
114 | */ | |
115 | public static <L, R, T> TransposedFunction<R, L, T> transpose(BinaryFunction<? super L, ? super R, ? extends T> f) { | |
116 | 4 | return null == f ? null : new TransposedFunction<R, L, T>(f); |
117 | } | |
118 | ||
119 | } |