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.core.composite;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.functor.UnaryFunction;
022    import org.apache.commons.functor.UnaryProcedure;
023    import org.apache.commons.functor.adapter.UnaryProcedureUnaryFunction;
024    
025    /**
026     * A {@link UnaryProcedure UnaryProcedure}
027     * representing the composition of
028     * {@link UnaryFunction UnaryFunctions},
029     * "chaining" the output of one to the input
030     * of another.  For example,
031     * <pre>new CompositeUnaryProcedure(p).of(f)</pre>
032     * {@link #run runs} to
033     * <code>p.run(f.evaluate(obj))</code>, and
034     * <pre>new CompositeUnaryProcedure(p).of(f).of(g)</pre>
035     * {@link #run runs}
036     * <code>p.run(f.evaluate(g.evaluate(obj)))</code>.
037     * <p>
038     * Note that although this class implements
039     * {@link Serializable}, a given instance will
040     * only be truly <code>Serializable</code> if all the
041     * underlying functors are.  Attempts to serialize
042     * an instance whose delegates are not all
043     * <code>Serializable</code> will result in an exception.
044     * </p>
045     * @version $Revision: 1166378 $ $Date: 2011-09-07 22:17:16 +0200 (Wed, 07 Sep 2011) $
046     * @author Rodney Waldhoff
047     */
048    public final class CompositeUnaryProcedure<A> implements UnaryProcedure<A>, Serializable {
049        /**
050         * serialVersionUID declaration.
051         */
052        private static final long serialVersionUID = -7496282561355676509L;
053        // attributes
054        // ------------------------------------------------------------------------
055        private final CompositeUnaryFunction<? super A, Object> function;
056    
057        // constructor
058        // ------------------------------------------------------------------------
059        /**
060         * Create a new CompositeUnaryProcedure.
061         * @param procedure final UnaryProcedure to run
062         */
063        public CompositeUnaryProcedure(UnaryProcedure<? super A> procedure) {
064            if (null == procedure) {
065                throw new IllegalArgumentException("procedure must not be null");
066            }
067            this.function = new CompositeUnaryFunction<A, Object>(new UnaryProcedureUnaryFunction<A, Object>(procedure));
068        }
069    
070        private CompositeUnaryProcedure(CompositeUnaryFunction<? super A, Object> function) {
071            this.function = function;
072        }
073    
074        // modifiers
075        // ------------------------------------------------------------------------
076        /**
077         * Fluently obtain a CompositeUnaryProcedure that runs our procedure against the result of the preceding function.
078         * @param preceding UnaryFunction
079         * @return CompositeUnaryProcedure<P>
080         */
081        public <T> CompositeUnaryProcedure<T> of(UnaryFunction<? super T, ? extends A> preceding) {
082            return new CompositeUnaryProcedure<T>(function.of(preceding));
083        }
084    
085        // predicate interface
086        // ------------------------------------------------------------------------
087        /**
088         * {@inheritDoc}
089         */
090        public void run(A obj) {
091            function.evaluate(obj);
092        }
093    
094        /**
095         * {@inheritDoc}
096         */
097        public boolean equals(Object that) {
098            return that == this || (that instanceof CompositeUnaryProcedure<?>
099                                        && equals((CompositeUnaryProcedure<?>) that));
100        }
101    
102        /**
103         * Learn whether another CompositeUnaryProcedure is equal to this.
104         * @param that CompositeUnaryProcedure to test
105         * @return boolean
106         */
107        public boolean equals(CompositeUnaryProcedure<?> that) {
108            return null != that && function.equals(that.function);
109        }
110    
111        /**
112         * {@inheritDoc}
113         */
114        public int hashCode() {
115            int hash = "CompositeUnaryProcedure".hashCode();
116            hash <<= 2;
117            hash ^= function.hashCode();
118            return hash;
119        }
120    
121        /**
122         * {@inheritDoc}
123         */
124        public String toString() {
125            return "CompositeUnaryProcedure<" + function + ">";
126        }
127    
128    }