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.UnaryProcedure;
023    
024    /**
025     * Adapts a
026     * {@link BinaryProcedure BinaryProcedure}
027     * to the
028     * {@link UnaryProcedure UnaryProcedure} 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     * @param <A> the argument type.
039     * @version $Revision: 1157644 $ $Date: 2011-08-14 22:19:52 +0200 (Sun, 14 Aug 2011) $
040     * @author Rodney Waldhoff
041     */
042    public final class RightBoundProcedure<A> implements UnaryProcedure<A>, Serializable {
043        /**
044         * serialVersionUID declaration.
045         */
046        private static final long serialVersionUID = 3267188080481758226L;
047        /** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */
048        private final BinaryProcedure<? super A, Object> procedure;
049        /** The parameter to pass to {@code procedure}. */
050        private final Object param;
051    
052        /**
053         * Create a new RightBoundProcedure.
054         * @param <R> bound arg type
055         * @param procedure the procedure to adapt
056         * @param arg the constant argument to use
057         */
058        @SuppressWarnings("unchecked")
059        public <R> RightBoundProcedure(BinaryProcedure<? super A, ? super R> procedure, R arg) {
060            if (procedure == null) {
061                throw new IllegalArgumentException("BinaryProcedure argument was null");
062            }
063            this.procedure = (BinaryProcedure<? super A, Object>) procedure;
064            this.param = arg;
065        }
066    
067        /**
068         * {@inheritDoc}
069         */
070        public void run(A obj) {
071            procedure.run(obj, param);
072        }
073    
074        /**
075         * {@inheritDoc}
076         */
077        public boolean equals(Object that) {
078            return that == this || (that instanceof RightBoundProcedure<?> && equals((RightBoundProcedure<?>) that));
079        }
080    
081        /**
082         * Learn whether another RightBoundProcedure is equal to this.
083         * @param that RightBoundProcedure to test
084         * @return boolean
085         */
086        public boolean equals(RightBoundProcedure<?> that) {
087            return null != that
088                    && (null == procedure ? null == that.procedure : procedure.equals(that.procedure))
089                    && (null == param ? null == that.param : param.equals(that.param));
090        }
091    
092        /**
093         * {@inheritDoc}
094         */
095        public int hashCode() {
096            int hash = "RightBoundProcedure".hashCode();
097            if (null != procedure) {
098                hash <<= 2;
099                hash ^= procedure.hashCode();
100            }
101            if (null != param) {
102                hash <<= 2;
103                hash ^= param.hashCode();
104            }
105            return hash;
106        }
107    
108        /**
109         * {@inheritDoc}
110         */
111        public String toString() {
112            return "RightBoundProcedure<" + procedure + "(?," + param + ")>";
113        }
114    
115        /**
116         * Get a UnaryProcedure from <code>procedure</code>.
117         * @param <L> the left argument type.
118         * @param <R> the right argument type.
119         * @param procedure to adapt
120         * @param arg right side argument
121         * @return RightBoundProcedure
122         */
123        public static <L, R> RightBoundProcedure<L> bind(BinaryProcedure<? super L, ? super R> procedure, R arg) {
124            return null == procedure ? null : new RightBoundProcedure<L>(procedure, arg);
125        }
126    
127    }