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 java.io.Serializable;
020
021import org.apache.commons.functor.BinaryFunction;
022import org.apache.commons.functor.UnaryFunction;
023import org.apache.commons.lang3.Validate;
024
025/**
026 * Adapts a
027 * {@link BinaryFunction BinaryFunction}
028 * to the
029 * {@link UnaryFunction UnaryFunction} interface
030 * using a constant right-side argument.
031 * <p/>
032 * Note that although this class implements
033 * {@link Serializable}, a given instance will
034 * only be truly <code>Serializable</code> if the
035 * underlying objects are.  Attempts to serialize
036 * an instance whose delegates are not
037 * <code>Serializable</code> will result in an exception.
038 *
039 * @param <A> the argument type.
040 * @param <T> the returned value type.
041 * @version $Revision: 1365377 $ $Date: 2012-07-24 20:59:23 -0400 (Tue, 24 Jul 2012) $
042 */
043public final class RightBoundFunction<A, T> implements UnaryFunction<A, T>, Serializable {
044    /**
045     * serialVersionUID declaration.
046     */
047    private static final long serialVersionUID = -1313775632123661422L;
048    /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */
049    private final BinaryFunction<? super A, Object, ? extends T> function;
050    /** The parameter to pass to {@code function}. */
051    private final Object param;
052
053    /**
054     * @param <R> bound arg type
055     * @param function the function to adapt
056     * @param arg the constant argument to use
057     */
058    @SuppressWarnings("unchecked")
059    public <R> RightBoundFunction(BinaryFunction<? super A, ? super R, ? extends T> function, R arg) {
060        this.function =
061            (BinaryFunction<? super A, Object, ? extends T>) Validate.notNull(
062                function, "left-hand BinaryFunction argument was null");
063        this.param = arg;
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    public T evaluate(A obj) {
070        return function.evaluate(obj, param);
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public boolean equals(Object that) {
078        return that == this || (that instanceof RightBoundFunction<?, ?> && equals((RightBoundFunction<?, ?>) that));
079    }
080
081    /**
082     * Learn whether another RightBoundFunction is equal to this.
083     * @param that RightBoundFunction to test
084     * @return boolean
085     */
086    public boolean equals(RightBoundFunction<?, ?> that) {
087        return null != that
088                && function.equals(that.function)
089                && (null == param ? null == that.param : param.equals(that.param));
090    }
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public int hashCode() {
097        int hash = "RightBoundFunction".hashCode();
098        hash <<= 2;
099        hash ^= function.hashCode();
100        if (null != param) {
101            hash <<= 2;
102            hash ^= param.hashCode();
103        }
104        return hash;
105    }
106
107    /**
108     * {@inheritDoc}
109     */
110    @Override
111    public String toString() {
112        return "RightBoundFunction<" + function + "(?," + param + ")>";
113    }
114
115    /**
116     * Adapt a BinaryFunction to the UnaryFunction interface.
117     * @param <L> the left argument type.
118     * @param <R> the right argument type.
119     * @param <T> the returned value type.
120     * @param function BinaryFunction to adapt
121     * @param arg Object that will always be used for the right side of the BinaryFunction delegate.
122     * @return RightBoundFunction
123     */
124    public static <L, R, T> RightBoundFunction<L, T> bind(BinaryFunction<? super L, ? super R, ? extends T> function,
125                                                          R arg) {
126        return null == function ? null : new RightBoundFunction<L, T>(function, arg);
127    }
128
129}