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