001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.generator;
022    
023    import java.io.IOException;
024    
025    import org.granite.generator.exception.GenerationException;
026    
027    /**
028     * @author Franck WOLFF
029     */
030    public abstract class Transformer<I extends Input<?>, O extends Output<?>, C extends Configuration> {
031    
032            private C config = null;
033            private Listener listener = null;
034            
035            public Transformer() {
036            }
037            
038            public Transformer(Configuration config, Listener listener) {
039                    setConfig(config);
040                    this.listener = listener;
041            }
042            
043            public C getConfig() {
044                    return config;
045            }
046    
047            @SuppressWarnings("unchecked")
048            public void setConfig(Configuration config) {
049                    this.config = (C)config;
050            }
051            
052            public Listener getListener() {
053                    return listener;
054            }
055    
056            public void setListener(Listener listener) {
057                    this.listener = listener;
058            }
059            
060            protected abstract boolean accept(Input<?> input);
061            
062            protected abstract O[] getOutputs(I input) throws IOException, GenerationException;
063    
064            @SuppressWarnings("unchecked")
065            public Output<?>[] generate(Input<?> input) throws IOException, GenerationException {
066                    O[] outputs = getOutputs((I)input);
067                    
068                    for (O output : outputs) {
069                            if (output.isOutdated()) {
070                                    listener.generating(input, output);
071                                    generate((I)input, output);
072                            }
073                            else
074                                    listener.skipping(input, output);
075                    }
076                    
077                    return outputs;
078            }
079            
080            protected abstract void generate(I input, O output) throws IOException, GenerationException;
081            
082            @Override
083            public boolean equals(Object obj) {
084                    return obj != null && obj.getClass().equals(getClass());
085            }
086    
087            @Override
088            public int hashCode() {
089                    return getClass().hashCode();
090            }
091    
092            @Override
093            public String toString() {
094                    return getClass().getName();
095            }
096    }