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