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 first 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: 1157610 $ $Date: 2011-08-14 21:44:17 +0200 (Sun, 14 Aug 2011) $
042     * @author Rodney Waldhoff
043     */
044    public final class IgnoreLeftFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
045        /**
046         * serialVersionUID declaration.
047         */
048        private static final long serialVersionUID = 4677703245851183542L;
049        /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
050        private final UnaryFunction<? super R, ? extends T> function;
051    
052        /**
053         * Create a new IgnoreLeftFunction.
054         * @param function UnaryFunction for right argument
055         */
056        public IgnoreLeftFunction(UnaryFunction<? super R, ? 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(right);
068        }
069    
070        /**
071         * {@inheritDoc}
072         */
073        public boolean equals(Object that) {
074            return that == this || (that instanceof IgnoreLeftFunction<?, ?, ?>
075                    && equals((IgnoreLeftFunction<?, ?, ?>) that));
076        }
077    
078        /**
079         * Learn whether another IgnoreLeftFunction is equal to this.
080         * @param that IgnoreLeftFunction to test
081         * @return boolean
082         */
083        public boolean equals(IgnoreLeftFunction<?, ?, ?> 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 = "IgnoreLeftFunction".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 "IgnoreLeftFunction<" + 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 to adapt
111         * @return IgnoreLeftFunction
112         */
113        public static <L, R, T> IgnoreLeftFunction<L, R, T> adapt(UnaryFunction<? super R, ? extends T> function) {
114            return null == function ? null : new IgnoreLeftFunction<L, R, T>(function);
115        }
116    
117    }