001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.functor.adapter;
018
019 import java.io.Serializable;
020
021 import org.apache.commons.functor.BinaryFunction;
022 import org.apache.commons.functor.UnaryFunction;
023
024 /**
025 * Adapts a
026 * {@link UnaryFunction UnaryFunction}
027 * to the
028 * {@link BinaryFunction BinaryFunction} interface
029 * by ignoring the second binary argument.
030 * <p/>
031 * Note that although this class implements
032 * {@link Serializable}, a given instance will
033 * only be truly <code>Serializable</code> if the
034 * underlying functor is. Attempts to serialize
035 * an instance whose delegate is not
036 * <code>Serializable</code> will result in an exception.
037 *
038 * @param <L> the left argument type.
039 * @param <R> the right argument type.
040 * @param <T> the returned value type.
041 * @version $Revision: 1157614 $ $Date: 2011-08-14 21:46:59 +0200 (Sun, 14 Aug 2011) $
042 * @author Rodney Waldhoff
043 */
044 public final class IgnoreRightFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
045 /**
046 * serialVersionUID declaration.
047 */
048 private static final long serialVersionUID = -1564814716024791395L;
049 /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
050 private final UnaryFunction<? super L, ? extends T> function;
051
052 /**
053 * Create a new IgnoreRightFunction.
054 * @param function UnaryFunction to wrap
055 */
056 public IgnoreRightFunction(UnaryFunction<? super L, ? extends T> function) {
057 if (function == null) {
058 throw new IllegalArgumentException("UnaryFunction argument was null");
059 }
060 this.function = function;
061 }
062
063 /**
064 * {@inheritDoc}
065 */
066 public T evaluate(L left, R right) {
067 return function.evaluate(left);
068 }
069
070 /**
071 * {@inheritDoc}
072 */
073 public boolean equals(Object that) {
074 return that == this || (that instanceof IgnoreRightFunction<?, ?, ?>
075 && equals((IgnoreRightFunction<?, ?, ?>) that));
076 }
077
078 /**
079 * Learn whether another IgnoreRightFunction is equal to this.
080 * @param that IgnoreRightFunction to test
081 * @return boolean
082 */
083 public boolean equals(IgnoreRightFunction<?, ?, ?> that) {
084 return null != that && (null == function ? null == that.function : function.equals(that.function));
085 }
086
087 /**
088 * {@inheritDoc}
089 */
090 public int hashCode() {
091 int hash = "IgnoreRightFunction".hashCode();
092 if (null != function) {
093 hash ^= function.hashCode();
094 }
095 return hash;
096 }
097
098 /**
099 * {@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 }