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