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.NullaryFunction;
020import org.apache.commons.functor.NullaryProcedure;
021import org.apache.commons.lang3.Validate;
022
023/**
024 * Adapts a {@link NullaryFunction NullaryFunction}
025 * to the {@link NullaryProcedure NullaryProcedure}
026 * interface by ignoring the value returned
027 * by the function.
028 *
029 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
030 */
031public final class NullaryFunctionNullaryProcedure implements NullaryProcedure {
032    /** The {@link NullaryFunction NullaryFunction} I'm wrapping. */
033    private final NullaryFunction<?> function;
034
035    /**
036     * Create an {@link NullaryProcedure NullaryProcedure} wrapping
037     * the given {@link NullaryFunction NullaryFunction}.
038     * @param function the {@link NullaryFunction NullaryFunction} to wrap
039     */
040    public NullaryFunctionNullaryProcedure(NullaryFunction<?> function) {
041        this.function = Validate.notNull(function, "NullaryFunction argument was null");
042    }
043
044    /**
045     * {@inheritDoc}
046     * {@link NullaryFunction#evaluate Evaluate} my function,
047     * but ignore its returned value.
048     */
049    public void run() {
050        function.evaluate();
051    }
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public boolean equals(Object obj) {
058        if (obj == this) {
059            return true;
060        }
061        if (!(obj instanceof NullaryFunctionNullaryProcedure)) {
062            return false;
063        }
064        NullaryFunctionNullaryProcedure that = (NullaryFunctionNullaryProcedure) obj;
065        return this.function.equals(that.function);
066    }
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public int hashCode() {
073        int hash = "NullaryFunctionNullaryProcedure".hashCode();
074        hash ^= function.hashCode();
075        return hash;
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public String toString() {
083        return "NullaryFunctionNullaryProcedure<" + function + ">";
084    }
085
086    /**
087     * Adapt the given, possibly-<code>null</code>,
088     * {@link NullaryFunction NullaryFunction} to the
089     * {@link NullaryProcedure NullaryProcedure} interface.
090     * When the given <code>NullaryFunction</code> is <code>null</code>,
091     * returns <code>null</code>.
092     *
093     * @param function the possibly-<code>null</code>
094     *        {@link NullaryFunction NullaryFunction} to adapt
095     * @return a {@link NullaryProcedure NullaryProcedure} wrapping the given
096     *         {@link NullaryFunction NullaryFunction}, or <code>null</code>
097     *         if the given <code>NullaryFunction</code> is <code>null</code>
098     */
099    public static NullaryFunctionNullaryProcedure adapt(NullaryFunction<?> function) {
100        return null == function ? null : new NullaryFunctionNullaryProcedure(function);
101    }
102
103}