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 java.io.Serializable;
020    
021    import org.apache.commons.functor.BinaryFunction;
022    import org.apache.commons.functor.BinaryProcedure;
023    
024    /**
025     * Adapts a {@link BinaryFunction BinaryFunction}
026     * to the {@link BinaryProcedure BinaryProcedure}
027     * interface by ignoring the value returned
028     * by the function.
029     * <p/>
030     * Note that although this class implements
031     * {@link Serializable}, a given instance will
032     * only be truly <code>Serializable</code> if the
033     * underlying function is.  Attempts to serialize
034     * an instance whose delegate is not
035     * <code>Serializable</code> will result in an exception.
036     *
037     * @param <L> the left argument type.
038     * @param <R> the right argument type.
039     * @version $Revision: 1156760 $ $Date: 2011-08-11 21:34:07 +0200 (Thu, 11 Aug 2011) $
040     * @author Rodney Waldhoff
041     */
042    public final class BinaryFunctionBinaryProcedure<L, R> implements BinaryProcedure<L, R>, Serializable {
043    
044        /**
045         * serialVersionUID declaration.
046         */
047        private static final long serialVersionUID = 4498276997127058865L;
048        /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */
049        private final BinaryFunction<? super L, ? super R, ?> function;
050    
051        /**
052         * Create an {@link BinaryProcedure BinaryProcedure} wrapping
053         * the given {@link BinaryFunction BinaryFunction}.
054         * @param function the {@link BinaryFunction BinaryFunction} to wrap
055         */
056        public BinaryFunctionBinaryProcedure(BinaryFunction<? super L, ? super R, ?> function) {
057            this.function = function;
058        }
059    
060        /**
061         * {@link BinaryFunction#evaluate Evaluate} my function, but
062         * ignore its returned value.
063         * {@inheritDoc}
064         */
065        public void run(L left, R right) {
066            function.evaluate(left, right);
067        }
068    
069        /**
070         * {@inheritDoc}
071         */
072        public boolean equals(Object that) {
073            return that == this
074                    || (that instanceof BinaryFunctionBinaryProcedure<?, ?>
075                    && equals((BinaryFunctionBinaryProcedure<?, ?>) that));
076        }
077    
078        /**
079         * Learn whether a given BinaryFunctionBinaryPredicate is equal to this.
080         * @param that BinaryFunctionBinaryPredicate to compare
081         * @return boolean
082         */
083        public boolean equals(BinaryFunctionBinaryProcedure<?, ?> that) {
084            return null != that && (null == function ? null == that.function : function.equals(that.function));
085        }
086    
087        /**
088         * {@inheritDoc}
089         */
090        public int hashCode() {
091            int hash = "BinaryFunctionBinaryProcedure".hashCode();
092            if (null != function) {
093                hash ^= function.hashCode();
094            }
095            return hash;
096        }
097    
098        /**
099         * {@inheritDoc}
100         */
101        public String toString() {
102            return "BinaryFunctionBinaryProcedure<" + function + ">";
103        }
104    
105        /**
106         * Adapt the given, possibly-<code>null</code>,
107         * {@link BinaryFunction BinaryFunction} to the
108         * {@link BinaryProcedure BinaryProcedure} interface.
109         * When the given <code>BinaryFunction</code> is <code>null</code>,
110         * returns <code>null</code>.
111         *
112         * @param <L> left type
113         * @param <R> right type
114         * @param function the possibly-<code>null</code>
115         *        {@link BinaryFunction BinaryFunction} to adapt
116         * @return a <code>BinaryFunctionBinaryProcedure</code> wrapping the given
117         *         {@link BinaryFunction BinaryFunction}, or <code>null</code>
118         *         if the given <code>BinaryFunction</code> is <code>null</code>
119         */
120        public static <L, R> BinaryFunctionBinaryProcedure<L, R> adapt(BinaryFunction<? super L, ? super R, ?> function) {
121            return null == function ? null : new BinaryFunctionBinaryProcedure<L, R>(function);
122        }
123    
124    }