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     */
017    package org.apache.commons.functor.adapter;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.functor.Procedure;
022    import org.apache.commons.functor.UnaryProcedure;
023    
024    /**
025     * Adapts a
026     * {@link Procedure Procedure}
027     * to the
028     * {@link UnaryProcedure UnaryProcedure} interface
029     * by ignoring the arguments.
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 functor is.  Attempts to serialize
035     * an instance whose delegate is not
036     * <code>Serializable</code> will result in an exception.
037     *
038     * @param <A> the argument type.
039     * @version $Revision: 1157633 $ $Date: 2011-08-14 22:08:42 +0200 (Sun, 14 Aug 2011) $
040     * @author Rodney Waldhoff
041     */
042    public final class ProcedureUnaryProcedure<A> implements UnaryProcedure<A>, Serializable {
043        /**
044         * serialVersionUID declaration.
045         */
046        private static final long serialVersionUID = 501530698794315412L;
047        /** The {@link Procedure Procedure} I'm wrapping. */
048        private final Procedure procedure;
049    
050        /**
051         * Create a new ProcedureUnaryProcedure.
052         * @param procedure to adapt
053         */
054        public ProcedureUnaryProcedure(Procedure procedure) {
055            if (procedure == null) {
056                throw new IllegalArgumentException("Procedure argument was null");
057            }
058            this.procedure = procedure;
059        }
060    
061        /**
062         * {@inheritDoc}
063         */
064        public void run(A obj) {
065            procedure.run();
066        }
067    
068        /**
069         * {@inheritDoc}
070         */
071        public boolean equals(Object that) {
072            return that == this || (that instanceof ProcedureUnaryProcedure<?>
073                                        && equals((ProcedureUnaryProcedure<?>) that));
074        }
075    
076        /**
077         * Learn whether another ProcedureUnaryProcedure is equal to this.
078         * @param that ProcedureUnaryProcedure to test
079         * @return boolean
080         */
081        public boolean equals(ProcedureUnaryProcedure<?> that) {
082            return null != that && (null == procedure ? null == that.procedure : procedure.equals(that.procedure));
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        public int hashCode() {
089            int hash = "ProcedureUnaryProcedure".hashCode();
090            if (null != procedure) {
091                hash ^= procedure.hashCode();
092            }
093            return hash;
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        public String toString() {
100            return "ProcedureUnaryProcedure<" + procedure + ">";
101        }
102    
103        /**
104         * Adapt a Procedure to the UnaryProcedure interface.
105         * @param <A> the argument type.
106         * @param procedure to adapt
107         * @return ProcedureUnaryProcedure<A>
108         */
109        public static <A> ProcedureUnaryProcedure<A> adapt(Procedure procedure) {
110            return null == procedure ? null : new ProcedureUnaryProcedure<A>(procedure);
111        }
112    
113    }