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 BinaryFunction BinaryFunction}
026 * to the
027 * {@link Function Function} interface
028 * using a constant right-side argument.
029 *
030 * @param <A> the argument type.
031 * @param <T> the returned value type.
032 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $
033 */
034public final class RightBoundFunction<A, T> implements Function<A, T> {
035    /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */
036    private final BinaryFunction<? super A, Object, ? extends T> function;
037    /** The parameter to pass to {@code function}. */
038    private final Object param;
039
040    /**
041     * @param <R> bound arg type
042     * @param function the function to adapt
043     * @param arg the constant argument to use
044     */
045    @SuppressWarnings("unchecked")
046    public <R> RightBoundFunction(BinaryFunction<? super A, ? super R, ? extends T> function, R arg) {
047        this.function =
048            (BinaryFunction<? super A, Object, ? extends T>) Validate.notNull(
049                function, "left-hand BinaryFunction argument was null");
050        this.param = arg;
051    }
052
053    /**
054     * {@inheritDoc}
055     */
056    public T evaluate(A obj) {
057        return function.evaluate(obj, param);
058    }
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public boolean equals(Object obj) {
065        if (obj == this) {
066            return true;
067        }
068        if (!(obj instanceof RightBoundFunction<?, ?>)) {
069            return false;
070        }
071        RightBoundFunction<?, ?> that = (RightBoundFunction<?, ?>) obj;
072        return function.equals(that.function)
073                && (null == param ? null == that.param : param.equals(that.param));
074    }
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public int hashCode() {
081        int hash = "RightBoundFunction".hashCode();
082        hash <<= 2;
083        hash ^= function.hashCode();
084        if (null != param) {
085            hash <<= 2;
086            hash ^= param.hashCode();
087        }
088        return hash;
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public String toString() {
096        return "RightBoundFunction<" + function + "(?," + param + ")>";
097    }
098
099    /**
100     * Adapt a BinaryFunction to the Function interface.
101     * @param <L> the left argument type.
102     * @param <R> the right argument type.
103     * @param <T> the returned value type.
104     * @param function BinaryFunction to adapt
105     * @param arg Object that will always be used for the right side of the BinaryFunction delegate.
106     * @return RightBoundFunction
107     */
108    public static <L, R, T> RightBoundFunction<L, T> bind(BinaryFunction<? super L, ? super R, ? extends T> function,
109                                                          R arg) {
110        return null == function ? null : new RightBoundFunction<L, T>(function, arg);
111    }
112
113}