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 BinaryFunction as a Function by sending the same argument to both sides of the BinaryFunction.
025 * It sounds nonsensical, but using Composite functions, can be made to do something useful.
026 * @param <A> the argument type.
027 * @param <T> the returned value type.
028 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
029 */
030public final class BinaryFunctionFunction<A, T> implements Function<A, T> {
031    /**
032     * The adapted function.
033     */
034    private final BinaryFunction<? super A, ? super A, ? extends T> function;
035
036    /**
037     * Create a new BinaryFunctionFunction.
038     * @param function to adapt
039     */
040    public BinaryFunctionFunction(BinaryFunction<? super A, ? super A, ? extends T> function) {
041        this.function = Validate.notNull(function, "BinaryFunction argument was null");
042    }
043
044    /**
045     * {@inheritDoc}
046     */
047    public T evaluate(A obj) {
048        return function.evaluate(obj, obj);
049    }
050
051    /**
052     * {@inheritDoc}
053     */
054    @Override
055    public boolean equals(Object obj) {
056        if (obj == this) {
057            return true;
058        }
059        if (!(obj instanceof BinaryFunctionFunction<?, ?>)) {
060            return false;
061        }
062        BinaryFunctionFunction<?, ?> that = (BinaryFunctionFunction<?, ?>) obj;
063        return this.function.equals(that.function);
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public int hashCode() {
071        return ("BinaryFunctionFunction".hashCode() << 2) | function.hashCode();
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public String toString() {
079        return "BinaryFunctionFunction<" + function + ">";
080    }
081
082    /**
083     * Adapt a BinaryFunction as a Function.
084     * @param <A> input type
085     * @param <T> result type
086     * @param function to adapt
087     * @return Function<A, T>
088     */
089    public static <A, T> Function<A, T> adapt(BinaryFunction<? super A, ? super A, ? extends T> function) {
090        return null == function ? null : new BinaryFunctionFunction<A, T>(function);
091    }
092
093}