Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RightBoundFunction |
|
| 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.BinaryFunction; | |
22 | import org.apache.commons.functor.UnaryFunction; | |
23 | ||
24 | /** | |
25 | * Adapts a | |
26 | * {@link BinaryFunction BinaryFunction} | |
27 | * to the | |
28 | * {@link UnaryFunction UnaryFunction} interface | |
29 | * using a constant right-side 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 | * @param <A> the argument type. | |
39 | * @param <T> the returned value type. | |
40 | * @version $Revision: 1157637 $ $Date: 2011-08-14 22:12:46 +0200 (Sun, 14 Aug 2011) $ | |
41 | * @author Rodney Waldhoff | |
42 | */ | |
43 | public final class RightBoundFunction<A, T> implements UnaryFunction<A, T>, Serializable { | |
44 | /** | |
45 | * serialVersionUID declaration. | |
46 | */ | |
47 | private static final long serialVersionUID = -1313775632123661422L; | |
48 | /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */ | |
49 | private final BinaryFunction<? super A, Object, ? extends T> function; | |
50 | /** The parameter to pass to {@code function}. */ | |
51 | private final Object param; | |
52 | ||
53 | /** | |
54 | * @param <R> bound arg type | |
55 | * @param function the function to adapt | |
56 | * @param arg the constant argument to use | |
57 | */ | |
58 | @SuppressWarnings("unchecked") | |
59 | 22 | public <R> RightBoundFunction(BinaryFunction<? super A, ? super R, ? extends T> function, R arg) { |
60 | 22 | if (function == null) { |
61 | 0 | throw new IllegalArgumentException("left-hand BinaryFunction argument was null"); |
62 | } | |
63 | 22 | this.function = (BinaryFunction<? super A, Object, ? extends T>) function; |
64 | 22 | this.param = arg; |
65 | 22 | } |
66 | ||
67 | /** | |
68 | * {@inheritDoc} | |
69 | */ | |
70 | public T evaluate(A obj) { | |
71 | 2 | return function.evaluate(obj, param); |
72 | } | |
73 | ||
74 | /** | |
75 | * {@inheritDoc} | |
76 | */ | |
77 | public boolean equals(Object that) { | |
78 | 26 | return that == this || (that instanceof RightBoundFunction<?, ?> && equals((RightBoundFunction<?, ?>) that)); |
79 | } | |
80 | ||
81 | /** | |
82 | * Learn whether another RightBoundFunction is equal to this. | |
83 | * @param that RightBoundFunction to test | |
84 | * @return boolean | |
85 | */ | |
86 | public boolean equals(RightBoundFunction<?, ?> that) { | |
87 | 18 | return null != that |
88 | && (null == function ? null == that.function : function.equals(that.function)) | |
89 | && (null == param ? null == that.param : param.equals(that.param)); | |
90 | } | |
91 | ||
92 | /** | |
93 | * {@inheritDoc} | |
94 | */ | |
95 | public int hashCode() { | |
96 | 28 | int hash = "RightBoundFunction".hashCode(); |
97 | 28 | if (null != function) { |
98 | 28 | hash <<= 2; |
99 | 28 | hash ^= function.hashCode(); |
100 | } | |
101 | 28 | if (null != param) { |
102 | 28 | hash <<= 2; |
103 | 28 | hash ^= param.hashCode(); |
104 | } | |
105 | 28 | return hash; |
106 | } | |
107 | ||
108 | /** | |
109 | * {@inheritDoc} | |
110 | */ | |
111 | public String toString() { | |
112 | 20 | return "RightBoundFunction<" + function + "(?," + param + ")>"; |
113 | } | |
114 | ||
115 | /** | |
116 | * Adapt a BinaryFunction to the UnaryFunction interface. | |
117 | * @param <L> the left argument type. | |
118 | * @param <R> the right argument type. | |
119 | * @param <T> the returned value type. | |
120 | * @param function BinaryFunction to adapt | |
121 | * @param arg Object that will always be used for the right side of the BinaryFunction delegate. | |
122 | * @return RightBoundFunction | |
123 | */ | |
124 | public static <L, R, T> RightBoundFunction<L, T> bind(BinaryFunction<? super L, ? super R, ? extends T> function, | |
125 | R arg) { | |
126 | 10 | return null == function ? null : new RightBoundFunction<L, T>(function, arg); |
127 | } | |
128 | ||
129 | } |