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.Function;
022import org.apache.commons.functor.Procedure;
023import org.apache.commons.lang3.Validate;
024
025/**
026 * Adapts a {@link Function Function}
027 * to the {@link Procedure Procedure}
028 * interface by ignoring the value returned
029 * by the function.
030 * <p/>
031 * Note that although this class implements
032 * {@link Serializable}, a given instance will
033 * only be truly <code>Serializable</code> if the
034 * underlying function is.  Attempts to serialize
035 * an instance whose delegate is not
036 * <code>Serializable</code> will result in an exception.
037 *
038 * @version $Revision: 1365377 $ $Date: 2012-07-24 20:59:23 -0400 (Tue, 24 Jul 2012) $
039 */
040public final class FunctionProcedure implements Procedure, Serializable {
041    /**
042     * serialVersionUID declaration.
043     */
044    private static final long serialVersionUID = -7300031015086684901L;
045    /** The {@link Function Function} I'm wrapping. */
046    private final Function<?> function;
047
048    /**
049     * Create an {@link Procedure Procedure} wrapping
050     * the given {@link Function Function}.
051     * @param function the {@link Function Function} to wrap
052     */
053    public FunctionProcedure(Function<?> function) {
054        this.function = Validate.notNull(function, "Function argument was null");
055    }
056
057    /**
058     * {@inheritDoc}
059     * {@link Function#evaluate Evaluate} my function,
060     * but ignore its returned value.
061     */
062    public void run() {
063        function.evaluate();
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public boolean equals(Object that) {
071        return that == this || (that instanceof FunctionProcedure && equals((FunctionProcedure) that));
072    }
073
074    /**
075     * Learn whether another FunctionProcedure is equal to this.
076     * @param that FunctionProcedure to test
077     * @return boolean
078     */
079    public boolean equals(FunctionProcedure that) {
080        return null != that && function.equals(that.function);
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public int hashCode() {
088        int hash = "FunctionProcedure".hashCode();
089        hash ^= function.hashCode();
090        return hash;
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public String toString() {
098        return "FunctionProcedure<" + function + ">";
099    }
100
101    /**
102     * Adapt the given, possibly-<code>null</code>,
103     * {@link Function Function} to the
104     * {@link Procedure Procedure} interface.
105     * When the given <code>Function</code> is <code>null</code>,
106     * returns <code>null</code>.
107     *
108     * @param function the possibly-<code>null</code>
109     *        {@link Function Function} to adapt
110     * @return a {@link Procedure Procedure} wrapping the given
111     *         {@link Function Function}, or <code>null</code>
112     *         if the given <code>Function</code> is <code>null</code>
113     */
114    public static FunctionProcedure adapt(Function<?> function) {
115        return null == function ? null : new FunctionProcedure(function);
116    }
117
118}