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