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

org.sklsft.commons.mapper.impl.AbstractMapper Maven / Gradle / Ivy

There is a newer version: 5.0.0-M1
Show newest version
package org.sklsft.commons.mapper.impl;

import org.sklsft.commons.api.exception.TechnicalError;
import org.sklsft.commons.mapper.interfaces.Mapper;

/**
 * An abstract {@link Mapper} implementation that uses clazz1(2).newInstance
 * @author Nicolas Thibault
 *
 * @param 
 * @param 
 */
public abstract class AbstractMapper implements Mapper {
	
	private Class clazz1;
	private Class clazz2;
	
	public AbstractMapper (Class clazz1, Class clazz2) {
		this.clazz1 = clazz1;
		this.clazz2 = clazz2;
	}
	
	public Class getClazz1() {
		return clazz1;
	}

	public Class getClazz2() {
		return clazz2;
	}
	

	@Override
	public abstract T mapFrom(T obj1, U obj2);	
	
	
	@Override
	public T mapFrom(U obj2) {
		try {
			return mapFrom(clazz1.newInstance(), obj2);
		} catch (InstantiationException | IllegalAccessException e) {
			throw new TechnicalError(TechnicalError.ERROR_UNKNOWN, e);
		}
	}
	

	@Override
	public abstract U mapTo(T obj1, U obj2);
	

	@Override
	public U mapTo(T obj1) {
		try {
			return mapTo(obj1, clazz2.newInstance());
		} catch (InstantiationException | IllegalAccessException e) {
			throw new TechnicalError(TechnicalError.ERROR_UNKNOWN, e);
		}
	}
}