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 left-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 LeftBoundFunction<A, T> implements UnaryFunction<A, T>, Serializable {
044    /**
045     * serialVersionUID declaration.
046     */
047    private static final long serialVersionUID = -7456827102718911769L;
048    /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */
049    private final BinaryFunction<Object, ? super A, ? extends T> function;
050    /** The parameter to pass to {@code function}. */
051    private final Object param;
052
053    /**
054     * Create a new LeftBoundFunction instance.
055     * @param <L> bound arg type
056     * @param function the function to adapt
057     * @param arg the constant argument to use
058     */
059    @SuppressWarnings("unchecked")
060    public <L> LeftBoundFunction(BinaryFunction<? super L, ? super A, ? extends T> function, L arg) {
061        this.function =
062            (BinaryFunction<Object, ? super A, ? extends T>) Validate.notNull(
063                function, "BinaryFunction argument was null");
064        this.param = arg;
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    public T evaluate(A obj) {
071        return function.evaluate(param, obj);
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public boolean equals(Object that) {
079        return that == this || (that instanceof LeftBoundFunction<?, ?> && equals((LeftBoundFunction<?, ?>) that));
080    }
081
082    /**
083     * Learn whether another LeftBoundFunction is equal to this.
084     * @param that LeftBoundFunction to test
085     * @return boolean
086     */
087    public boolean equals(LeftBoundFunction<?, ?> that) {
088        return null != that
089                && function.equals(that.function)
090                && (null == param ? null == that.param : param.equals(that.param));
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public int hashCode() {
098        int hash = "LeftBoundFunction".hashCode();
099        hash <<= 2;
100        hash ^= function.hashCode();
101        if (null != param) {
102            hash <<= 2;
103            hash ^= param.hashCode();
104        }
105        return hash;
106    }
107
108    /**
109     * {@inheritDoc}
110     */
111    @Override
112    public String toString() {
113        return "LeftBoundFunction<" + function + "(" + param + ",?)>";
114    }
115
116    /**
117     * Adapt a BinaryFunction as a UnaryFunction.
118     * @param <L> left type
119     * @param <R> right type
120     * @param <T> result type
121     * @param function to adapt
122     * @param arg left side argument
123     * @return LeftBoundFunction
124     */
125    public static <L, R, T> LeftBoundFunction<R, T> bind(
126            BinaryFunction<? super L, ? super R, ? extends T> function, L arg) {
127        return null == function ? null : new LeftBoundFunction<R, T>(function, arg);
128    }
129
130}