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.collections.functors;
018    
019    import java.io.Serializable;
020    import java.util.Collection;
021    import java.util.Iterator;
022    
023    import org.apache.commons.collections.Transformer;
024    
025    /**
026     * Transformer implementation that chains the specified transformers together.
027     * <p>
028     * The input object is passed to the first transformer. The transformed result
029     * is passed to the second transformer and so on.
030     * 
031     * @since Commons Collections 3.0
032     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
033     *
034     * @author Stephen Colebourne
035     */
036    public class ChainedTransformer implements Transformer, Serializable {
037    
038        /** Serial version UID */
039        private static final long serialVersionUID = 3514945074733160196L;
040    
041        /** The transformers to call in turn */
042        private final Transformer[] iTransformers;
043    
044        /**
045         * Factory method that performs validation and copies the parameter array.
046         * 
047         * @param transformers  the transformers to chain, copied, no nulls
048         * @return the <code>chained</code> transformer
049         * @throws IllegalArgumentException if the transformers array is null
050         * @throws IllegalArgumentException if any transformer in the array is null
051         */
052        public static Transformer getInstance(Transformer[] transformers) {
053            FunctorUtils.validate(transformers);
054            if (transformers.length == 0) {
055                return NOPTransformer.INSTANCE;
056            }
057            transformers = FunctorUtils.copy(transformers);
058            return new ChainedTransformer(transformers);
059        }
060        
061        /**
062         * Create a new Transformer that calls each transformer in turn, passing the 
063         * result into the next transformer. The ordering is that of the iterator()
064         * method on the collection.
065         * 
066         * @param transformers  a collection of transformers to chain
067         * @return the <code>chained</code> transformer
068         * @throws IllegalArgumentException if the transformers collection is null
069         * @throws IllegalArgumentException if any transformer in the collection is null
070         */
071        public static Transformer getInstance(Collection transformers) {
072            if (transformers == null) {
073                throw new IllegalArgumentException("Transformer collection must not be null");
074            }
075            if (transformers.size() == 0) {
076                return NOPTransformer.INSTANCE;
077            }
078            // convert to array like this to guarantee iterator() ordering
079            Transformer[] cmds = new Transformer[transformers.size()];
080            int i = 0;
081            for (Iterator it = transformers.iterator(); it.hasNext();) {
082                cmds[i++] = (Transformer) it.next();
083            }
084            FunctorUtils.validate(cmds);
085            return new ChainedTransformer(cmds);
086        }
087    
088        /**
089         * Factory method that performs validation.
090         * 
091         * @param transformer1  the first transformer, not null
092         * @param transformer2  the second transformer, not null
093         * @return the <code>chained</code> transformer
094         * @throws IllegalArgumentException if either transformer is null
095         */
096        public static Transformer getInstance(Transformer transformer1, Transformer transformer2) {
097            if (transformer1 == null || transformer2 == null) {
098                throw new IllegalArgumentException("Transformers must not be null");
099            }
100            Transformer[] transformers = new Transformer[] { transformer1, transformer2 };
101            return new ChainedTransformer(transformers);
102        }
103    
104        /**
105         * Constructor that performs no validation.
106         * Use <code>getInstance</code> if you want that.
107         * 
108         * @param transformers  the transformers to chain, not copied, no nulls
109         */
110        public ChainedTransformer(Transformer[] transformers) {
111            super();
112            iTransformers = transformers;
113        }
114    
115        /**
116         * Transforms the input to result via each decorated transformer
117         * 
118         * @param object  the input object passed to the first transformer
119         * @return the transformed result
120         */
121        public Object transform(Object object) {
122            for (int i = 0; i < iTransformers.length; i++) {
123                object = iTransformers[i].transform(object);
124            }
125            return object;
126        }
127    
128        /**
129         * Gets the transformers, do not modify the array.
130         * @return the transformers
131         * @since Commons Collections 3.1
132         */
133        public Transformer[] getTransformers() {
134            return iTransformers;
135        }
136    
137    }