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.Function;
022    import org.apache.commons.functor.Procedure;
023    
024    /**
025     * Adapts a
026     * {@link Procedure Procedure}
027     * to the
028     * {@link Function Function} interface
029     * by always returning <code>null</code>.
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 procedure is.  Attempts to serialize
035     * an instance whose delegate is not
036     * <code>Serializable</code> will result in an exception.
037     *
038     * @param <T> the returned value type.
039     * @version $Revision: 1157630 $ $Date: 2011-08-14 22:06:30 +0200 (Sun, 14 Aug 2011) $
040     * @author Rodney Waldhoff
041     */
042    public final class ProcedureFunction<T> implements Function<T>, Serializable {
043        /**
044         * serialVersionUID declaration.
045         */
046        private static final long serialVersionUID = -655207616317672341L;
047        /** The {@link Procedure Procedure} I'm wrapping. */
048        private final Procedure procedure;
049    
050        /**
051         * Create a new ProcedureFunction.
052         * @param procedure to adapt
053         */
054        public ProcedureFunction(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 T evaluate() {
065            procedure.run();
066            return null;
067        }
068    
069        /**
070         * {@inheritDoc}
071         */
072        public boolean equals(Object that) {
073            return that == this || (that instanceof ProcedureFunction<?> && equals((ProcedureFunction<?>) that));
074        }
075    
076        /**
077         * Learn whether another ProcedureFunction is equal to this.
078         * @param that ProcedureFunction to test
079         * @return boolean
080         */
081        public boolean equals(ProcedureFunction<?> 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 = "ProcedureFunction".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 "ProcedureFunction<" + procedure + ">";
101        }
102    
103        /**
104         * Adapt a Procedure as a Function.
105         * @param <T> the returned value type.
106         * @param procedure to adapt
107         * @return ProcedureFunction<T>
108         */
109        public static <T> ProcedureFunction<T> adapt(Procedure procedure) {
110            return null == procedure ? null : new ProcedureFunction<T>(procedure);
111        }
112    
113    }