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     */
017    package org.apache.commons.functor.adapter;
018    
019    import org.apache.commons.functor.BinaryFunction;
020    import org.apache.commons.functor.UnaryFunction;
021    
022    /**
023     * Adapts a BinaryFunction as a UnaryFunction by sending the same argument to both sides of the BinaryFunction.
024     * It sounds nonsensical, but using Composite functions, can be made to do something useful.
025     * @param <A> the argument type.
026     * @param <T> the returned value type.
027     * @version $Revision: 1156765 $ $Date: 2011-08-11 21:37:08 +0200 (Thu, 11 Aug 2011) $
028     * @author Rodney Waldhoff
029     */
030    public final class BinaryFunctionUnaryFunction<A, T> implements UnaryFunction<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 BinaryFunctionUnaryFunction.
038         * @param function to adapt
039         */
040        public BinaryFunctionUnaryFunction(BinaryFunction<? super A, ? super A, ? extends T> function) {
041            if (null == function) {
042                throw new IllegalArgumentException("BinaryFunction argument was null");
043            }
044            this.function = function;
045        }
046    
047        /**
048         * {@inheritDoc}
049         */
050        public T evaluate(A obj) {
051            return function.evaluate(obj, obj);
052        }
053    
054        /**
055         * {@inheritDoc}
056         */
057        @Override
058        public boolean equals(Object obj) {
059            return obj == this || obj instanceof BinaryFunctionUnaryFunction<?, ?>
060                    && equals((BinaryFunctionUnaryFunction<?, ?>) obj);
061        }
062    
063        /**
064         * Learn whether another BinaryFunctionUnaryFunction is equal to <code>this</code>.
065         * @param that BinaryFunctionUnaryFunction to check
066         * @return whether equal
067         */
068        public boolean equals(BinaryFunctionUnaryFunction<?, ?> that) {
069            return that != null && that.function.equals(this.function);
070        }
071    
072        /**
073         * {@inheritDoc}
074         */
075        @Override
076        public int hashCode() {
077            return ("BinaryFunctionUnaryFunction".hashCode() << 2) | function.hashCode();
078        }
079    
080        /**
081         * {@inheritDoc}
082         */
083        @Override
084        public String toString() {
085            return "BinaryFunctionUnaryFunction<" + function + ">";
086        }
087    
088        /**
089         * Adapt a BinaryFunction as a UnaryFunction.
090         * @param <A> input type
091         * @param <T> result type
092         * @param function to adapt
093         * @return UnaryFunction<A, T>
094         */
095        public static <A, T> UnaryFunction<A, T> adapt(BinaryFunction<? super A, ? super A, ? extends T> function) {
096            return null == function ? null : new BinaryFunctionUnaryFunction<A, T>(function);
097        }
098    
099    }