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.core.composite;
018
019import org.apache.commons.functor.BinaryFunction;
020import org.apache.commons.lang3.Validate;
021
022/**
023 * Transposes (swaps) the arguments to some other
024 * {@link BinaryFunction function}.
025 * For example, given a function <i>f</i>
026 * and the ordered pair of arguments <i>a</i>,
027 * <i>b</i>.
028 * {@link #evaluate evaluates} to
029 * <code>f.evaluate(b,a)</code>.
030 *
031 * @param <L> the left argument type.
032 * @param <R> the right argument type.
033 * @param <T> the returned value type.
034 * @version $Revision: 1537602 $ $Date: 2013-10-31 20:53:09 +0100 (Do, 31 Okt 2013) $
035 */
036public class TransposedFunction<L, R, T> implements BinaryFunction<L, R, T> {
037    // attributes
038    // ------------------------------------------------------------------------
039    /**
040     * The adapted function.
041     */
042    private final BinaryFunction<? super R, ? super L, ? extends T> function;
043
044    // constructor
045    // ------------------------------------------------------------------------
046    /**
047     * Create a new TransposedFunction.
048     * @param function BinaryFunction to transpose.
049     */
050    public TransposedFunction(BinaryFunction<? super R, ? super L, ? extends T> function) {
051        this.function = Validate.notNull(function, "BinaryFunction argument was null");
052    }
053
054    // functor interface
055    // ------------------------------------------------------------------------
056    /**
057     * {@inheritDoc}
058     */
059    public final T evaluate(L left, R right) {
060        return function.evaluate(right, left);
061    }
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public final boolean equals(Object obj) {
068        if (obj == this) {
069            return true;
070        }
071        if (!(obj instanceof TransposedFunction<?, ?, ?>)) {
072            return false;
073        }
074        TransposedFunction<?, ?, ?> that = (TransposedFunction<?, ?, ?>) obj;
075        return this.function.equals(that.function);
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public int hashCode() {
083        int hash = "TransposedFunction".hashCode();
084        hash ^= function.hashCode();
085        return hash;
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public String toString() {
093        return "TransposedFunction<" + function + ">";
094    }
095
096    // static
097    // ------------------------------------------------------------------------
098    /**
099     * Transpose a BinaryFunction.
100     * @param <L> the left argument type.
101     * @param <R> the right argument type.
102     * @param <T> the returned value type.
103     * @param f BinaryFunction to transpose
104     * @return TransposedFunction
105     */
106    public static <L, R, T> TransposedFunction<R, L, T> transpose(BinaryFunction<? super L, ? super R, ? extends T> f) {
107        return null == f ? null : new TransposedFunction<R, L, T>(f);
108    }
109
110}