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 */
017package org.apache.commons.functor.adapter;
018
019import org.apache.commons.functor.BinaryFunction;
020import org.apache.commons.functor.Function;
021import org.apache.commons.lang3.Validate;
022
023/**
024 * Adapts a
025 * {@link Function Function}
026 * to the
027 * {@link BinaryFunction BinaryFunction} interface
028 * by ignoring the first binary argument.
029 *
030 * @param <L> the left argument type.
031 * @param <R> the right argument type.
032 * @param <T> the returned value type.
033 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $
034 */
035public final class IgnoreLeftFunction<L, R, T> implements BinaryFunction<L, R, T> {
036    /** The {@link Function Function} I'm wrapping. */
037    private final Function<? super R, ? extends T> function;
038
039    /**
040     * Create a new IgnoreLeftFunction.
041     * @param function Function for right argument
042     */
043    public IgnoreLeftFunction(Function<? super R, ? extends T> function) {
044        this.function = Validate.notNull(function, "Function argument was null");
045    }
046
047    /**
048     * {@inheritDoc}
049     */
050    public T evaluate(L left, R right) {
051        return function.evaluate(right);
052    }
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    public boolean equals(Object obj) {
059        if (obj == this) {
060            return true;
061        }
062        if (!(obj instanceof IgnoreLeftFunction<?, ?, ?>)) {
063            return false;
064        }
065        IgnoreLeftFunction<?, ?, ?> that = (IgnoreLeftFunction<?, ?, ?>) obj;
066        return this.function.equals(that.function);
067    }
068
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    public int hashCode() {
074        int hash = "IgnoreLeftFunction".hashCode();
075        hash ^= function.hashCode();
076        return hash;
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public String toString() {
084        return "IgnoreLeftFunction<" + function + ">";
085    }
086
087    /**
088     * Adapt a Function to the BinaryFunction interface.
089     * @param <L> left type
090     * @param <R> right type
091     * @param <T> result type
092     * @param function to adapt
093     * @return IgnoreLeftFunction
094     */
095    public static <L, R, T> IgnoreLeftFunction<L, R, T> adapt(Function<? super R, ? extends T> function) {
096        return null == function ? null : new IgnoreLeftFunction<L, R, T>(function);
097    }
098
099}