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.List;
033    import org.apache.commons.collections15.Transformer;
034    
035    
036    /**
037     * A sequence of {@code Transformer}s.
038     * 
039     * @author nikitin
040     * 
041     * @param <U> the object type to transform.
042     * @param <V> the object type transformed.
043     * 
044     * @version 1.0
045     * 
046     */
047    @SuppressWarnings("unchecked")
048    public final class U2VSequenceTransformer<U, V> implements Transformer<U, V> {
049            
050            private Object obj;
051            private List<Transformer> transformers;
052            
053            private U2VSequenceTransformer() {
054                    transformers = new ArrayList<Transformer>();
055            }
056            
057            public static <U, V> U2VSequenceTransformer newInstance() {
058                    return new U2VSequenceTransformer<U, V>();
059            }
060            
061            // TODO: how to control the following correct sequence
062            // : the previous return map type T has to be the same as the current param
063            // one
064            // : U -> U1 -> U2 -> ... -> Un-1 -> Un -> V
065            public <U1, U2> void addTransformer(final Transformer<U1, U2> transformer) {
066                    transformers.add(transformer);
067            }
068            
069            public V transform(final U object) {
070                    execute(object);
071                    return (V) obj;
072            }
073            
074            private void execute(U object) {
075                    
076                    obj = object;
077                    
078                    // U -> U1 -> U2 -> ... -> Un-1 -> Un -> V
079                    for (final Transformer mapper : transformers) {
080                            obj = mapper.transform(obj);
081                    }
082            }
083            
084            // public String toString() {
085            // StringBuilder sb = new StringBuilder();
086            //              
087            // sb.append("[");
088            //              
089            // if (mappers.size() > 0) {
090            // for (final JPLIMapper mapper : mappers) {
091            // sb.append(mapper);
092            // sb.append(", ");
093            // }
094            // sb.delete(sb.length() - 2, sb.length());
095            // }
096            //              
097            // sb.append("]");
098            //              
099            // return sb.toString();
100            // }
101            
102    }