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 BinaryProcedure as a Procedure by sending the same argument to both sides of the BinaryProcedure.
025 * @param <A> the argument type.
026 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
027 */
028public final class BinaryProcedureProcedure<A> implements Procedure<A> {
029    /**
030     * The adapted procedure.
031     */
032    private final BinaryProcedure<? super A, ? super A> procedure;
033
034    /**
035     * Create a new BinaryProcedureProcedure.
036     * @param procedure to adapt
037     */
038    public BinaryProcedureProcedure(BinaryProcedure<? super A, ? super A> procedure) {
039        this.procedure = Validate.notNull(procedure, "BinaryProcedure argument was null");
040    }
041
042    /**
043     * {@inheritDoc}
044     */
045    public void run(A obj) {
046        procedure.run(obj, obj);
047    };
048
049    /**
050     * {@inheritDoc}
051     */
052    @Override
053    public boolean equals(Object obj) {
054        if (obj == this) {
055            return true;
056        }
057        if (!(obj instanceof BinaryProcedureProcedure<?>)) {
058            return false;
059        }
060        BinaryProcedureProcedure<?> that = (BinaryProcedureProcedure<?>) obj;
061        return this.procedure.equals(that.procedure);
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public int hashCode() {
069        return ("BinaryProcedureProcedure".hashCode() << 2) | procedure.hashCode();
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public String toString() {
077        return "BinaryProcedureProcedure<" + procedure + ">";
078    }
079
080    /**
081     * Adapt a BinaryProcedure as a Procedure.
082     * @param <A> argument type
083     * @param procedure BinaryProcedure to adapt
084     * @return Procedure<A>
085     */
086    public static <A> Procedure<A> adapt(BinaryProcedure<? super A, ? super A> procedure) {
087        return null == procedure ? null : new BinaryProcedureProcedure<A>(procedure);
088    }
089
090}