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.BinaryProcedure;
022    import org.apache.commons.functor.Procedure;
023    
024    /**
025     * Adapts a
026     * {@link BinaryProcedure BinaryProcedure}
027     * to the
028     * {@link Procedure Procedure} interface
029     * using a constant left-side argument.
030     * <p/>
031     * Note that although this class implements
032     * {@link Serializable}, a given instance will
033     * only be truly <code>Serializable</code> if the
034     * underlying objects are.  Attempts to serialize
035     * an instance whose delegates are not
036     * <code>Serializable</code> will result in an exception.
037     *
038     * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
039     * @author Matt Benson
040     */
041    public final class FullyBoundProcedure implements Procedure, Serializable {
042        /**
043         * serialVersionUID declaration.
044         */
045        private static final long serialVersionUID = -904891610081737737L;
046        /** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */
047        private final BinaryProcedure<Object, Object> procedure;
048        /** The left parameter to pass to {@code procedure}. */
049        private final Object left;
050        /** The right parameter to pass to {@code procedure}. */
051        private final Object right;
052    
053        /**
054         * Create a new FullyBoundProcedure instance.
055         * @param <L> left type
056         * @param <R> right type
057         * @param procedure the procedure to adapt
058         * @param left the left argument to use
059         * @param right the right argument to use
060         */
061        @SuppressWarnings("unchecked")
062        public <L, R> FullyBoundProcedure(BinaryProcedure<? super L, ? super R> procedure, L left, R right) {
063            if (procedure == null) {
064                throw new IllegalArgumentException("BinaryProcedure argument was null");
065            }
066            this.procedure = (BinaryProcedure<Object, Object>) procedure;
067            this.left = left;
068            this.right = right;
069        }
070    
071        /**
072         * {@inheritDoc}
073         */
074        public void run() {
075            procedure.run(left, right);
076        }
077    
078        /**
079         * {@inheritDoc}
080         */
081        public boolean equals(Object that) {
082            return that == this || (that instanceof FullyBoundProcedure && equals((FullyBoundProcedure) that));
083        }
084    
085        /**
086         * Learn whether another FullyBoundProcedure is equal to this.
087         * @param that FullyBoundProcedure to test
088         * @return boolean
089         */
090        public boolean equals(FullyBoundProcedure that) {
091            return null != that && (null == procedure ? null == that.procedure : procedure.equals(that.procedure))
092                    && (null == left ? null == that.left : left.equals(that.left))
093                    && (null == right ? null == that.right : right.equals(that.right));
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        public int hashCode() {
100            int hash = "FullyBoundProcedure".hashCode();
101            if (null != procedure) {
102                hash <<= 2;
103                hash ^= procedure.hashCode();
104            }
105            hash <<= 2;
106            if (null != left) {
107                hash ^= left.hashCode();
108            }
109            hash <<= 2;
110            if (null != right) {
111                hash ^= right.hashCode();
112            }
113            return hash;
114        }
115    
116        /**
117         * {@inheritDoc}
118         */
119        public String toString() {
120            return "FullyBoundProcedure<" + procedure + "(" + left + ", " + right + ")>";
121        }
122    
123        /**
124         * Adapt a BinaryProcedure to the Procedure interface.
125         * @param <L> left type
126         * @param <R> right type
127         * @param procedure to adapt
128         * @param left left side argument
129         * @param right right side argument
130         * @return FullyBoundProcedure
131         */
132        public static <L, R> FullyBoundProcedure bind(BinaryProcedure<? super L, ? super R> procedure, L left, R right) {
133            return null == procedure ? null : new FullyBoundProcedure(procedure, left, right);
134        }
135    
136    }