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.BinaryProcedure;
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 BinaryProcedure BinaryProcedure} interface
030 * by ignoring the first binary 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 functor is.  Attempts to serialize
036 * an instance whose delegate is not
037 * <code>Serializable</code> will result in an exception.
038 *
039 * @param <L> the left argument type.
040 * @param <R> the right argument type.
041 * @version $Revision: 1365377 $ $Date: 2012-07-24 20:59:23 -0400 (Tue, 24 Jul 2012) $
042 */
043public final class IgnoreLeftProcedure<L, R> implements BinaryProcedure<L, R>, Serializable {
044    /**
045     * serialVersionUID declaration.
046     */
047    private static final long serialVersionUID = 513435556181843298L;
048    /** The {@link UnaryProcedure UnaryProcedure} I'm wrapping. */
049    private final UnaryProcedure<? super R> procedure;
050
051    /**
052     * Create a new IgnoreLeftProcedure.
053     * @param procedure to adapt
054     */
055    public IgnoreLeftProcedure(UnaryProcedure<? super R> procedure) {
056        this.procedure = Validate.notNull(procedure, "UnaryProcedure argument was null");
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    public void run(L left, R right) {
063        procedure.run(right);
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public boolean equals(Object that) {
071        return that == this || (that instanceof IgnoreLeftProcedure<?, ?> && equals((IgnoreLeftProcedure<?, ?>) that));
072    }
073
074    /**
075     * Learn whether another IgnoreLeftProcedure is equal to this.
076     * @param that IgnoreLeftProcedure to test
077     * @return boolean
078     */
079    public boolean equals(IgnoreLeftProcedure<?, ?> that) {
080        return null != that && procedure.equals(that.procedure);
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public int hashCode() {
088        int hash = "IgnoreLeftProcedure".hashCode();
089        hash ^= procedure.hashCode();
090        return hash;
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public String toString() {
098        return "IgnoreLeftProcedure<" + procedure + ">";
099    }
100
101    /**
102     * Adapt a UnaryProcedure to the BinaryProcedure interface.
103     * @param <L> left type
104     * @param <R> right type
105     * @param procedure to adapt
106     * @return IgnoreLeftProcedure<L, R>
107     */
108    public static <L, R> IgnoreLeftProcedure<L, R> adapt(UnaryProcedure<? super R> procedure) {
109        return null == procedure ? null : new IgnoreLeftProcedure<L, R>(procedure);
110    }
111
112}