All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.speedment.common.codegen.TransformFactory Maven / Gradle / Ivy

/**
 *
 * Copyright (c) 2006-2016, Speedment, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); You may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.speedment.common.codegen;

import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Different {@link Transform} implementations can be installed in the
 * TransformFactory to be instantiated when needed. The transforms
 * are ordered in a graph-like manner so that all tranforms leading away from
 * a particular model type easily can be located.
 * 
 * @author Emil Forslund
 * @since   2.0
 */
public interface TransformFactory {
    /**
     * Returns a unique name of this factory. This can be used to identify
     * a particular factory if multiple ones are used in the same generator.
     * 
     * @return  the unique name
     */
    String getName();
    
    /**
	 * Installs the specified {@link Transform}, assuming that the resulting 
     * class is a String.
     * 
	 * @param           the type to transform from
	 * @param           the transformer
	 * @param from         the model
	 * @param transformer  the view
     * @return             a reference to this
	 */
    default > TransformFactory install(
        Class from, Class transformer) {
        
        return install(from, String.class, transformer);
    }
    
	/**
	 * Installs the specified {@link Transform}.
     * 
	 * @param           the type to transform from
     * @param           the type to transform to
	 * @param           the transformer
	 * @param from         the model class to transform from
     * @param to           the model class to transform to
	 * @param transformer  the view
     * @return             a reference to this
	 */
	> TransformFactory install(
        Class from, Class to, Class transformer);

	/**
	 * Builds a stream of all transforms that match the specified model.
     * 
     * @param    the class to transform from
	 * @param    the transformer
	 * @param from  the model class to transform from
	 * @return      a stream of all matching transforms
	 */
	> Set, T>> allFrom(Class from);
	
	/**
	 * Instantiates the specified class and returns it.
     * 
	 * @param     the return type
	 * @param clazz  a class of the intended return type
	 * @return       the instance
	 */
	static  T create(Class clazz) {
		try {
			return clazz.newInstance();
		} catch (InstantiationException | IllegalAccessException ex) {
			Logger.getLogger(TransformFactory.class.getName()).log(Level.SEVERE, 
				"The class '" + clazz.getName() + 
				"' could not be instantiated using the default constructor. " +
				"Make sure it is the correct class and that the default " +
				"constructor has been properly defined without no parameters.", ex);
		}
		
		return null;
	}
}