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 import org.apache.commons.lang3.Validate;
24
25 /**
26 * Adapts a
27 * {@link UnaryFunction UnaryFunction}
28 * to the
29 * {@link Function Function} interface
30 * using a constant unary argument.
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 objects are. Attempts to serialize
36 * an instance whose delegates are not
37 * <code>Serializable</code> will result in an exception.
38 *
39 * @param <T> the returned value type.
40 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
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 public <A> BoundFunction(UnaryFunction<? super A, ? extends T> function, A arg) {
60 this.function =
61 (UnaryFunction<Object, ? extends T>) Validate.notNull(function,
62 "UnaryFunction argument was null");
63 this.arg = arg;
64 }
65
66 /**
67 * {@inheritDoc}
68 */
69 public T evaluate() {
70 return function.evaluate(arg);
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 @Override
77 public boolean equals(Object that) {
78 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 if (that == null) {
88 return false;
89 }
90 if (!(that.function.equals(this.function))) {
91 return false;
92 }
93 return that.arg == this.arg || that.arg != null && that.arg.equals(this.arg);
94 }
95
96 /**
97 * {@inheritDoc}
98 */
99 @Override
100 public int hashCode() {
101 int result = "BoundFunction".hashCode();
102 result <<= 2;
103 result |= function.hashCode();
104 result <<= 2;
105 return arg == null ? result : result | arg.hashCode();
106 }
107
108 /**
109 * {@inheritDoc}
110 */
111 @Override
112 public String toString() {
113 return "BoundFunction<" + function.toString() + "(" + arg + ")>";
114 }
115
116 /**
117 * Adapt the given, possibly-<code>null</code>,
118 * {@link UnaryFunction UnaryFunction} to the
119 * {@link Function Function} interface by binding
120 * the specified <code>Object</code> as a constant
121 * argument.
122 * When the given <code>UnaryFunction</code> is <code>null</code>,
123 * returns <code>null</code>.
124 * @param <A> input type
125 * @param <T> result type
126 * @param function the possibly-<code>null</code>
127 * {@link UnaryFunction UnaryFunction} to adapt
128 * @param arg the object to bind as a constant argument
129 * @return a <code>BoundFunction</code> wrapping the given
130 * {@link UnaryFunction UnaryFunction}, or <code>null</code>
131 * if the given <code>UnaryFunction</code> is <code>null</code>
132 */
133 public static <A, T> BoundFunction<T> bind(UnaryFunction<? super A, ? extends T> function, A arg) {
134 return null == function ? null : new BoundFunction<T>(function, arg);
135 }
136
137 }