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 import org.apache.commons.lang3.Validate;
24
25 /**
26 * Adapts a
27 * {@link UnaryFunction UnaryFunction}
28 * to the
29 * {@link BinaryFunction BinaryFunction} interface
30 * by ignoring the first binary 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 functor is. Attempts to serialize
36 * an instance whose delegate is not
37 * <code>Serializable</code> will result in an exception.
38 *
39 * @param <L> the left argument type.
40 * @param <R> the right argument type.
41 * @param <T> the returned value type.
42 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
43 */
44 public final class IgnoreLeftFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
45 /**
46 * serialVersionUID declaration.
47 */
48 private static final long serialVersionUID = 4677703245851183542L;
49 /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
50 private final UnaryFunction<? super R, ? extends T> function;
51
52 /**
53 * Create a new IgnoreLeftFunction.
54 * @param function UnaryFunction for right argument
55 */
56 public IgnoreLeftFunction(UnaryFunction<? super R, ? extends T> function) {
57 this.function = Validate.notNull(function, "UnaryFunction argument was null");
58 }
59
60 /**
61 * {@inheritDoc}
62 */
63 public T evaluate(L left, R right) {
64 return function.evaluate(right);
65 }
66
67 /**
68 * {@inheritDoc}
69 */
70 @Override
71 public boolean equals(Object that) {
72 return that == this || (that instanceof IgnoreLeftFunction<?, ?, ?>
73 && equals((IgnoreLeftFunction<?, ?, ?>) that));
74 }
75
76 /**
77 * Learn whether another IgnoreLeftFunction is equal to this.
78 * @param that IgnoreLeftFunction to test
79 * @return boolean
80 */
81 public boolean equals(IgnoreLeftFunction<?, ?, ?> that) {
82 return null != that && function.equals(that.function);
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public int hashCode() {
90 int hash = "IgnoreLeftFunction".hashCode();
91 hash ^= function.hashCode();
92 return hash;
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 @Override
99 public String toString() {
100 return "IgnoreLeftFunction<" + function + ">";
101 }
102
103 /**
104 * Adapt a UnaryFunction to the BinaryFunction interface.
105 * @param <L> left type
106 * @param <R> right type
107 * @param <T> result type
108 * @param function to adapt
109 * @return IgnoreLeftFunction
110 */
111 public static <L, R, T> IgnoreLeftFunction<L, R, T> adapt(UnaryFunction<? super R, ? extends T> function) {
112 return null == function ? null : new IgnoreLeftFunction<L, R, T>(function);
113 }
114
115 }