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