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.BinaryFunction;
022    import org.apache.commons.functor.BinaryProcedure;
023    import org.apache.commons.functor.UnaryProcedure;
024    
025    /**
026     * A BinaryProcedure composed of a BinaryFunction whose result is then run through a UnaryProcedure.
027     * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $
028     * @author Matt Benson
029     */
030    public class TransformedBinaryProcedure<L, R> implements BinaryProcedure<L, R>, Serializable {
031        /**
032         * serialVersionUID declaration.
033         */
034        private static final long serialVersionUID = -7559182250073101798L;
035    
036        /**
037         * Type-remembering helper
038         * @param <X>
039         */
040        private static final class Helper<X, L, R> implements BinaryProcedure<L, R>, Serializable {
041            /**
042             * serialVersionUID declaration.
043             */
044            private static final long serialVersionUID = -4229496007094829301L;
045            private BinaryFunction<? super L, ? super R, ? extends X> function;
046            private UnaryProcedure<? super X> procedure;
047    
048            /**
049             * Create a new Helper.
050             * @param function BinaryFunction
051             * @param procedure UnaryFunction
052             */
053            private Helper(BinaryFunction<? super L, ? super R, ? extends X> function,
054                    UnaryProcedure<? super X> procedure) {
055                this.function = function;
056                this.procedure = procedure;
057            }
058    
059            /**
060             * {@inheritDoc}
061             */
062            public void run(L left, R right) {
063                procedure.run(function.evaluate(left, right));
064            }
065        }
066    
067        private final Helper<?, L, R> helper;
068    
069        /**
070         * Create a new TransformedBinaryProcedure.
071         * @param <X>
072         * @param function BinaryFunction
073         * @param procedure UnaryProcedure
074         */
075        public <X> TransformedBinaryProcedure(BinaryFunction<? super L, ? super R, ? extends X> function,
076                UnaryProcedure<? super X> procedure) {
077            this.helper = new Helper<X, L, R>(function, procedure);
078        }
079    
080        /**
081         * {@inheritDoc}
082         */
083        public final void run(L left, R right) {
084            helper.run(left, right);
085        }
086    
087        /**
088         * {@inheritDoc}
089         */
090        @Override
091        public final boolean equals(Object obj) {
092            return obj == this || obj instanceof TransformedBinaryProcedure<?, ?>
093                    && equals((TransformedBinaryProcedure<?, ?>) obj);
094        }
095    
096        /**
097         * Learn whether another TransformedBinaryProcedure is equal to <code>this</code>.
098         * @param that instance to test
099         * @return whether equal
100         */
101        public final boolean equals(TransformedBinaryProcedure<?, ?> that) {
102            return that != null && that.helper.function.equals(this.helper.function)
103                    && that.helper.procedure.equals(this.helper.procedure);
104        }
105    
106        /**
107         * {@inheritDoc}
108         */
109        @Override
110        public int hashCode() {
111            int result = "TransformedBinaryProcedure".hashCode();
112            result <<= 2;
113            result |= helper.procedure.hashCode();
114            result <<= 2;
115            result |= helper.function.hashCode();
116            return result;
117        }
118    
119        /**
120         * {@inheritDoc}
121         */
122        @Override
123        public String toString() {
124            return "TransformedBinaryProcedure<" + helper.function + "; " + helper.procedure + ">";
125        }
126    }