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