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 */
017package org.apache.commons.functor.adapter;
018
019import org.apache.commons.functor.BinaryProcedure;
020import org.apache.commons.functor.Procedure;
021import org.apache.commons.lang3.Validate;
022
023/**
024 * Adapts a
025 * {@link BinaryProcedure BinaryProcedure}
026 * to the
027 * {@link Procedure Procedure} interface
028 * using a constant left-side argument.
029 *
030 * @param <A> the argument type.
031 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $
032 */
033public final class RightBoundProcedure<A> implements Procedure<A> {
034    /** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */
035    private final BinaryProcedure<? super A, Object> procedure;
036    /** The parameter to pass to {@code procedure}. */
037    private final Object param;
038
039    /**
040     * Create a new RightBoundProcedure.
041     * @param <R> bound arg type
042     * @param procedure the procedure to adapt
043     * @param arg the constant argument to use
044     */
045    @SuppressWarnings("unchecked")
046    public <R> RightBoundProcedure(BinaryProcedure<? super A, ? super R> procedure, R arg) {
047        this.procedure =
048            (BinaryProcedure<? super A, Object>) Validate.notNull(procedure,
049                "BinaryProcedure argument was null");
050        this.param = arg;
051    }
052
053    /**
054     * {@inheritDoc}
055     */
056    public void run(A obj) {
057        procedure.run(obj, param);
058    }
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public boolean equals(Object obj) {
065        if (obj == this) {
066            return true;
067        }
068        if (!(obj instanceof RightBoundProcedure<?>)) {
069            return false;
070        }
071        RightBoundProcedure<?> that = (RightBoundProcedure<?>) obj;
072        return this.procedure.equals(that.procedure)
073                && (null == param ? null == that.param : param.equals(that.param));
074    }
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public int hashCode() {
081        int hash = "RightBoundProcedure".hashCode();
082        hash <<= 2;
083        hash ^= procedure.hashCode();
084        if (null != param) {
085            hash <<= 2;
086            hash ^= param.hashCode();
087        }
088        return hash;
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public String toString() {
096        return "RightBoundProcedure<" + procedure + "(?," + param + ")>";
097    }
098
099    /**
100     * Get a Procedure from <code>procedure</code>.
101     * @param <L> the left argument type.
102     * @param <R> the right argument type.
103     * @param procedure to adapt
104     * @param arg right side argument
105     * @return RightBoundProcedure
106     */
107    public static <L, R> RightBoundProcedure<L> bind(BinaryProcedure<? super L, ? super R> procedure, R arg) {
108        return null == procedure ? null : new RightBoundProcedure<L>(procedure, arg);
109    }
110
111}