Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BoundFunction |
|
| 2.7142857142857144;2.714 |
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.Function; | |
22 | import org.apache.commons.functor.UnaryFunction; | |
23 | ||
24 | /** | |
25 | * Adapts a | |
26 | * {@link UnaryFunction UnaryFunction} | |
27 | * to the | |
28 | * {@link Function Function} interface | |
29 | * using a constant unary 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 <T> the returned value type. | |
39 | * @version $Revision: 1157604 $ $Date: 2011-08-14 21:29:42 +0200 (Sun, 14 Aug 2011) $ | |
40 | * @author Rodney Waldhoff | |
41 | */ | |
42 | public final class BoundFunction<T> implements Function<T>, Serializable { | |
43 | /** | |
44 | * serialVersionUID declaration. | |
45 | */ | |
46 | private static final long serialVersionUID = 8873081237760986490L; | |
47 | /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */ | |
48 | private final UnaryFunction<Object, ? extends T> function; | |
49 | /** The argument to pass to {@code function}. */ | |
50 | private final Object arg; | |
51 | ||
52 | /** | |
53 | * Create a new BoundFunction instance. | |
54 | * @param <A> the argument value type | |
55 | * @param function the function to adapt | |
56 | * @param arg the constant argument to use | |
57 | */ | |
58 | @SuppressWarnings("unchecked") | |
59 | 24 | public <A> BoundFunction(UnaryFunction<? super A, ? extends T> function, A arg) { |
60 | 24 | if (function == null) { |
61 | 0 | throw new IllegalArgumentException("UnaryFunction argument was null"); |
62 | } | |
63 | 24 | this.function = (UnaryFunction<Object, ? extends T>) function; |
64 | 24 | this.arg = arg; |
65 | 24 | } |
66 | ||
67 | /** | |
68 | * {@inheritDoc} | |
69 | */ | |
70 | public T evaluate() { | |
71 | 2 | return function.evaluate(arg); |
72 | } | |
73 | ||
74 | /** | |
75 | * {@inheritDoc} | |
76 | */ | |
77 | public boolean equals(Object that) { | |
78 | 30 | return that == this || (that instanceof BoundFunction<?> && equals((BoundFunction<?>) that)); |
79 | } | |
80 | ||
81 | /** | |
82 | * Learn whether another BoundFunction is equal to this. | |
83 | * @param that BoundFunction to test | |
84 | * @return boolean | |
85 | */ | |
86 | public boolean equals(BoundFunction<?> that) { | |
87 | 22 | if (that == null) { |
88 | 0 | return false; |
89 | } | |
90 | 22 | if (!(that.function.equals(this.function))) { |
91 | 4 | return false; |
92 | } | |
93 | 18 | return that.arg == this.arg || that.arg != null && that.arg.equals(this.arg); |
94 | } | |
95 | ||
96 | /** | |
97 | * {@inheritDoc} | |
98 | */ | |
99 | public int hashCode() { | |
100 | 32 | int result = "BoundFunction".hashCode(); |
101 | 32 | result <<= 2; |
102 | 32 | result |= function.hashCode(); |
103 | 32 | result <<= 2; |
104 | 32 | return arg == null ? result : result | arg.hashCode(); |
105 | } | |
106 | ||
107 | /** | |
108 | * {@inheritDoc} | |
109 | */ | |
110 | public String toString() { | |
111 | 24 | return "BoundFunction<" + function.toString() + "(" + arg + ")>"; |
112 | } | |
113 | ||
114 | /** | |
115 | * Adapt the given, possibly-<code>null</code>, | |
116 | * {@link UnaryFunction UnaryFunction} to the | |
117 | * {@link Function Function} interface by binding | |
118 | * the specified <code>Object</code> as a constant | |
119 | * argument. | |
120 | * When the given <code>UnaryFunction</code> is <code>null</code>, | |
121 | * returns <code>null</code>. | |
122 | * @param <A> input type | |
123 | * @param <T> result type | |
124 | * @param function the possibly-<code>null</code> | |
125 | * {@link UnaryFunction UnaryFunction} to adapt | |
126 | * @param arg the object to bind as a constant argument | |
127 | * @return a <code>BoundFunction</code> wrapping the given | |
128 | * {@link UnaryFunction UnaryFunction}, or <code>null</code> | |
129 | * if the given <code>UnaryFunction</code> is <code>null</code> | |
130 | */ | |
131 | public static <A, T> BoundFunction<T> bind(UnaryFunction<? super A, ? extends T> function, A arg) { | |
132 | 6 | return null == function ? null : new BoundFunction<T>(function, arg); |
133 | } | |
134 | ||
135 | } |