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