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    import java.util.ArrayList;
021    import java.util.Iterator;
022    import java.util.List;
023    
024    import org.apache.commons.functor.UnaryProcedure;
025    
026    /**
027     * A {@link UnaryProcedure UnaryProcedure}
028     * that {@link UnaryProcedure#run runs} an ordered
029     * sequence of {@link UnaryProcedure UnaryProcedures}.
030     * When the sequence is empty, this procedure is does
031     * nothing.
032     * <p>
033     * Note that although this class implements
034     * {@link Serializable}, a given instance will
035     * only be truly <code>Serializable</code> if all the
036     * underlying functors are.  Attempts to serialize
037     * an instance whose delegates are not all
038     * <code>Serializable</code> will result in an exception.
039     * </p>
040     * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
041     * @author Rodney Waldhoff
042     */
043    public class UnarySequence<A> implements UnaryProcedure<A>, Serializable {
044    
045        /**
046         * serialVersionUID declaration.
047         */
048        private static final long serialVersionUID = 9194268249717820246L;
049        // attributes
050        // ------------------------------------------------------------------------
051        private List<UnaryProcedure<? super A>> list = new ArrayList<UnaryProcedure<? super A>>();
052    
053        // constructor
054        // ------------------------------------------------------------------------
055        /**
056         * Create a new UnarySequence.
057         */
058        public UnarySequence() {
059            super();
060        }
061    
062        /**
063         * Create a new UnarySequence instance.
064         *
065         * @param procedures to run sequentially
066         */
067        public UnarySequence(UnaryProcedure<? super A>... procedures) {
068            this();
069            if (procedures != null) {
070                for (UnaryProcedure<? super A> p : procedures) {
071                    then(p);
072                }
073            }
074        }
075    
076        /**
077         * Create a new UnarySequence instance.
078         *
079         * @param procedures to run sequentially
080         */
081        public UnarySequence(Iterable<UnaryProcedure<? super A>> procedures) {
082            this();
083            if (procedures != null) {
084                for (UnaryProcedure<? super A> p : procedures) {
085                    then(p);
086                }
087            }
088        }
089    
090        // modifiers
091        // ------------------------------------------------------------------------
092        /**
093         * Fluently add a UnaryProcedure to the sequence.
094         * @param p UnaryProcedure to add
095         * @return this
096         */
097        public UnarySequence<A> then(UnaryProcedure<? super A> p) {
098            list.add(p);
099            return this;
100        }
101    
102        // predicate interface
103        // ------------------------------------------------------------------------
104        /**
105         * {@inheritDoc}
106         */
107        public void run(A obj) {
108            for (Iterator<UnaryProcedure<? super A>> iter = list.iterator(); iter.hasNext();) {
109                iter.next().run(obj);
110            }
111        }
112    
113        /**
114         * {@inheritDoc}
115         */
116        public boolean equals(Object that) {
117            return that == this || (that instanceof UnarySequence<?> && equals((UnarySequence<?>) that));
118        }
119    
120        /**
121         * Learn whether another UnarySequence is equal to this.
122         * @param that UnarySequence to test
123         * @return boolean
124         */
125        public boolean equals(UnarySequence<?> that) {
126            // by construction, list is never null
127            return null != that && list.equals(that.list);
128        }
129    
130        /**
131         * {@inheritDoc}
132         */
133        public int hashCode() {
134            // by construction, list is never null
135            return "UnarySequence".hashCode() ^ list.hashCode();
136        }
137    
138        /**
139         * {@inheritDoc}
140         */
141        public String toString() {
142            return "UnarySequence<" + list + ">";
143        }
144    
145    }