io.github.opensabre.common.web.entity.convert.EntityModelConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opensabre-web Show documentation
Show all versions of opensabre-web Show documentation
OpenSabre Web project for Spring Boot
The newest version!
package io.github.opensabre.common.web.entity.convert;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
/**
* 对象转换工具类
* @author zhoutaoo
*/
public class EntityModelConverter implements EntityConverter {
private EntityModelConverter() {
}
/**
* 返回实例
*
* @return 单例
*/
public static EntityModelConverter getInstance() {
return SingletonHolder.sInstance;
}
/**
* 静态内部类单例模式
* 单例初使化
*/
private static class SingletonHolder {
private static final EntityModelConverter sInstance = new EntityModelConverter();
}
/**
* 将源对象转换为 目标对象
*
* @param source 源对象
* @param targetClass 目标对象类型
* @return 目标对象
*/
@SneakyThrows
public P convert(F source, Class targetClass) {
P target = targetClass.getDeclaredConstructor().newInstance();
BeanUtils.copyProperties(source, target);
return target;
}
}