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.core.composite;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.functor.BinaryProcedure;
022    
023    /**
024     * Transposes (swaps) the arguments to some other
025     * {@link BinaryProcedure procedure}.
026     * For example, given a procedure <i>p</i>
027     * and the ordered pair of arguments <i>a</i>,
028     * <i>b</i>.
029     * {@link #run runs}
030     * <code>p.run(b,a)</code>.
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 functor is.  Attempts to serialize
036     * an instance whose delegate is not
037     * <code>Serializable</code> will result in an exception.
038     * </p>
039     * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
040     * @author Rodney Waldhoff
041     */
042    public class TransposedProcedure<L, R> implements BinaryProcedure<L, R>, Serializable {
043        /**
044         * serialVersionUID declaration.
045         */
046        private static final long serialVersionUID = 4472683678460290015L;
047        // attributes
048        // ------------------------------------------------------------------------
049        private final BinaryProcedure<? super R, ? super L> procedure;
050    
051        // constructor
052        // ------------------------------------------------------------------------
053        /**
054         * Create a new TransposedProcedure.
055         * @param procedure BinaryProcedure to transpose
056         */
057        public TransposedProcedure(BinaryProcedure<? super R, ? super L> procedure) {
058            if (procedure == null) {
059                throw new IllegalArgumentException("BinaryProcedure argument was null");
060            }
061            this.procedure = procedure;
062        }
063    
064        // functor interface
065        // ------------------------------------------------------------------------
066        /**
067         * {@inheritDoc}
068         */
069        public void run(L left, R right) {
070            procedure.run(right, left);
071        }
072    
073        /**
074         * {@inheritDoc}
075         */
076        public boolean equals(Object that) {
077            return that == this || (that instanceof TransposedProcedure<?, ?> && equals((TransposedProcedure<?, ?>) that));
078        }
079    
080        /**
081         * Learn whether another TransposedProcedure is equal to this.
082         * @param that TransposedPredicate to test
083         * @return boolean
084         */
085        public boolean equals(TransposedProcedure<?, ?> that) {
086            return null != that && (null == procedure ? null == that.procedure : procedure.equals(that.procedure));
087        }
088    
089        /**
090         * {@inheritDoc}
091         */
092        public int hashCode() {
093            int hash = "TransposedProcedure".hashCode();
094            if (null != procedure) {
095                hash ^= procedure.hashCode();
096            }
097            return hash;
098        }
099    
100        /**
101         * {@inheritDoc}
102         */
103        public String toString() {
104            return "TransposedProcedure<" + procedure + ">";
105        }
106    
107        // static
108        // ------------------------------------------------------------------------
109        /**
110         * Transpose a BinaryProcedure.
111         * @param p to transpose
112         * @return TransposedProcedure
113         */
114        public static <L, R> TransposedProcedure<R, L> transpose(BinaryProcedure<? super L, ? super R> p) {
115            return null == p ? null : new TransposedProcedure<R, L>(p);
116        }
117    
118    }