001 /**
002 * GRANITE DATA SERVICES
003 * Copyright (C) 2006-2013 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 */
022
023 package org.granite.generator;
024
025 import java.io.IOException;
026
027 import org.granite.generator.exception.GenerationException;
028
029 /**
030 * @author Franck WOLFF
031 */
032 public abstract class Transformer<I extends Input<?>, O extends Output<?>, C extends Configuration> {
033
034 private C config = null;
035 private Listener listener = null;
036
037 public Transformer() {
038 }
039
040 public Transformer(Configuration config, Listener listener) {
041 setConfig(config);
042 this.listener = listener;
043 }
044
045 public C getConfig() {
046 return config;
047 }
048
049 @SuppressWarnings("unchecked")
050 public void setConfig(Configuration config) {
051 this.config = (C)config;
052 }
053
054 public Listener getListener() {
055 return listener;
056 }
057
058 public void setListener(Listener listener) {
059 this.listener = listener;
060 }
061
062 protected abstract boolean accept(Input<?> input);
063
064 protected abstract O[] getOutputs(I input) throws IOException, GenerationException;
065
066 @SuppressWarnings("unchecked")
067 public Output<?>[] generate(Input<?> input) throws IOException, GenerationException {
068 O[] outputs = getOutputs((I)input);
069
070 for (O output : outputs) {
071 if (output.isOutdated()) {
072 listener.generating(input, output);
073 generate((I)input, output);
074 }
075 else
076 listener.skipping(input, output);
077 }
078
079 return outputs;
080 }
081
082 protected abstract void generate(I input, O output) throws IOException, GenerationException;
083
084 @Override
085 public boolean equals(Object obj) {
086 return obj != null && obj.getClass().equals(getClass());
087 }
088
089 @Override
090 public int hashCode() {
091 return getClass().hashCode();
092 }
093
094 @Override
095 public String toString() {
096 return getClass().getName();
097 }
098 }