001    /**
002     * Copyright (c) 2010, SIB. All rights reserved.
003     * 
004     * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005     * https://sourceforge.net/projects/javaprotlib/
006     * 
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions are met:
009     * Redistributions of source code must retain the above copyright notice, this
010     * list of conditions and the following disclaimer. Redistributions in binary
011     * form must reproduce the above copyright notice, this list of conditions and
012     * the following disclaimer in the documentation and/or other materials provided
013     * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014     * its contributors may be used to endorse or promote products derived from this
015     * software without specific prior written permission.
016     * 
017     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020     * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023     * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     */
028    package org.expasy.jpl.commons.base.process;
029    
030    
031    import java.util.ArrayList;
032    import java.util.Iterator;
033    import java.util.List;
034    import org.apache.commons.collections15.Transformer;
035    
036    
037    /**
038     * Manager that handles list of {@code Transformer<T>} and transform all.
039     * 
040     * @author mueller
041     * @author nikitin
042     * 
043     * @param T object type to process.
044     */
045    public final class SequenceTransformer<T> implements Transformer<T, T> {
046            
047            /** the list of object processors */
048            protected List<Transformer<T, T>> transformers;
049            
050            private SequenceTransformer() {
051                    transformers = new ArrayList<Transformer<T, T>>();
052            }
053            
054            public static <T> SequenceTransformer<T> newInstance() {
055                    return new SequenceTransformer<T>();
056            }
057            
058            /**
059             * Get object processors.
060             * 
061             * @return list of processors.
062             */
063            public List<Transformer<T, T>> getTransformers() {
064                    return transformers;
065            }
066            
067            /**
068             * @return the number of transformer in the sequence.
069             */
070            public int size() {
071                    return transformers.size();
072            }
073            
074            /**
075             * Set processors.
076             * 
077             * @param processors list of processors.
078             */
079            public void setTransformers(final List<Transformer<T, T>> transformers) {
080                    this.transformers = transformers;
081            }
082            
083            /**
084             * Add a transformer.
085             * 
086             * @param transformer transformer to add.
087             * 
088             */
089            public void add(final Transformer<T, T> transformer) {
090                    transformers.add(transformer);
091            }
092            
093            /**
094             * Add a list of transformers.
095             * 
096             * @param transformers processors to add.
097             * 
098             */
099            public void addAll(final List<Transformer<T, T>> transformers) {
100                    this.transformers.addAll(transformers);
101            }
102            
103            /**
104             * Remove all transformers.
105             */
106            public void clear() {
107                    transformers.clear();
108            }
109            
110            /**
111             * Successively process object by all processors.
112             * 
113             * @param object object to be processed.
114             * 
115             * @return the object after been processed.
116             */
117            public T transform(final T object) {
118                    T processed = object;
119                    
120                    for (final Iterator<Transformer<T, T>> iterator =
121                        transformers.iterator(); iterator.hasNext();) {
122                            final Transformer<T, T> transformer = iterator.next();
123                            processed = transformer.transform(processed);
124                    }
125                    
126                    return processed;
127            }
128            
129    }