转化工具类性能优化

v1.4.1
Parker 4 years ago
parent 7d3d71c5eb
commit 024b6bf1a2

@ -22,9 +22,11 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @BelongsProject: opsli-boot
@ -44,118 +46,70 @@ public final class WrapperUtil {
/**
*
* @param from
* @param toClass
* @param <M>
* @return
* @param source
* @param target
* @param <M>
* @return M
*/
public static <T,M> M transformInstance(T from, Class<M> toClass){
M m = null;
try {
Object toObj = ReflectUtil.newInstance(toClass);
Map<String, Object> stringBeanMap = BeanUtil.beanToMap(from);
if(stringBeanMap != null){
Object toInstance = BeanUtil.fillBeanWithMapIgnoreCase(stringBeanMap, toObj, true);
if(toInstance != null){
m = (M) toInstance;
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
return m;
public static <M> M transformInstance(Object source, Class<M> target){
return transformInstance(source, target, false);
}
/**
*
* @param froms
* @param toClass
* @param <M>
* @return
* @param source
* @param target
* @param <M>
* @return List<M>
*/
public static <T,M> List<M> transformInstance(List<T> froms, Class<M> toClass){
List<M> toInstanceList = Lists.newArrayListWithCapacity(froms.size());
try {
for (Object from : froms) {
Object toObj = ReflectUtil.newInstance(toClass);
Map<String, Object> stringBeanMap = BeanUtil.beanToMap(from);
if(stringBeanMap != null){
Object toInstance = BeanUtil.fillBeanWithMapIgnoreCase(stringBeanMap, toObj, true);
if(toInstance != null){
toInstanceList.add((M) toInstance);
}
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
return toInstanceList;
public static <T,M> List<M> transformInstance(Collection<T> source, Class<M> target){
return transformInstance(source, target, false);
}
/**
*
* @param froms
* @param toClass
* @param <M>
* @return
*
* @param source
* @param target
* @param isClone
* @return M
*/
public static <T,M> Set<M> transformInstance(Set<T> froms, Class<M> toClass){
Set<M> toInstanceList = Sets.newHashSetWithExpectedSize(froms.size());
try {
for (Object from : froms) {
Object toObj = ReflectUtil.newInstance(toClass);
Map<String, Object> stringBeanMap = BeanUtil.beanToMap(from);
if(stringBeanMap != null){
Object toInstance = BeanUtil.fillBeanWithMapIgnoreCase(stringBeanMap, toObj, true);
if(toInstance != null){
toInstanceList.add((M) toInstance);
}
}
}
public static <T,M> M transformInstance(Object source, Class<M> target, boolean isClone){
if(isClone){
source = ObjectUtil.cloneByStream(source);
}
M m = null;
try {
m = BeanUtil.copyProperties(source, target);
}catch (Exception e){
log.error(e.getMessage(),e);
}
return toInstanceList;
}
/**
*
* @param from
* @param toClass
* @param <M>
* @return
*/
public static <T,M> M cloneTransformInstance(T from, Class<M> toClass){
T fromByClone = ObjectUtil.cloneByStream(from);
return transformInstance(fromByClone,toClass);
return m;
}
/**
*
* @param froms
* @param toClass
* @param source
* @param target
* @param isClone
* @param <M>
* @return
* @return List<M>
*/
public static <T,M> List<M> cloneTransformInstance(List<T> froms, Class<M> toClass){
List<T> ts = ObjectUtil.cloneByStream(froms);
return transformInstance(ts,toClass);
}
public static <T,M> List<M> transformInstance(Collection<T> source, Class<M> target, boolean isClone){
if(isClone){
source = ObjectUtil.cloneByStream(source);
}
/**
*
* @param froms
* @param toClass
* @param <M>
* @return
*/
public static <T,M> Set<M> cloneTransformInstance(Set<T> froms, Class<M> toClass){
Set<T> ts = ObjectUtil.cloneByStream(froms);
return transformInstance(ts,toClass);
List<M> toInstanceList = Lists.newArrayList();
try {
toInstanceList = source.stream().map((s) -> transformInstance(s, target, true)).collect(Collectors.toList());
}catch (Exception e){
log.error(e.getMessage(),e);
}
return toInstanceList;
}
}

@ -51,7 +51,7 @@ public enum JavaCodeBuilder {
*/
public Map<String,String> createEntity(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
List<CreaterTableColumnModel> columnList = builderModel.getModel().getColumnList();
// 处理数据
for (CreaterTableColumnModel columnModel : columnList) {
@ -95,7 +95,7 @@ public enum JavaCodeBuilder {
*/
public Map<String,String> createMapper(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
String codeStr = EnjoyUtil.render("/backend/mapper/TemplateMapper.html",
this.createKv(builderModel)
);
@ -128,7 +128,7 @@ public enum JavaCodeBuilder {
*/
public Map<String,String> createMapperXML(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
String codeStr = EnjoyUtil.render("/backend/mapper/xml/TemplateMapperXML.html",
this.createKv(builderModel)
);
@ -163,7 +163,7 @@ public enum JavaCodeBuilder {
*/
public Map<String,String> createService(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
String codeStr = EnjoyUtil.render("/backend/service/TemplateService.html",
this.createKv(builderModel)
);
@ -197,7 +197,7 @@ public enum JavaCodeBuilder {
*/
public Map<String,String> createServiceImpl(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
String codeStr = EnjoyUtil.render("/backend/service/impl/TemplateServiceImpl.html",
this.createKv(builderModel)
);
@ -232,7 +232,7 @@ public enum JavaCodeBuilder {
*/
public Map<String,String> createWeb(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
String codeStr = EnjoyUtil.render("/backend/web/TemplateRestController.html",
this.createKv(builderModel)
);
@ -266,7 +266,7 @@ public enum JavaCodeBuilder {
*/
public Map<String,String> createModel(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
// 处理数据
List<CreaterTableColumnModel> columnList = builderModel.getModel().getColumnList();
for (CreaterTableColumnModel columnModel : columnList) {
@ -339,7 +339,7 @@ public enum JavaCodeBuilder {
*/
public Map<String,String> createRestApi(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
String codeStr = EnjoyUtil.render("/backend/api/TemplateRestApi.html",
this.createKv(builderModel)
);

@ -57,7 +57,7 @@ public enum VueCodeBuilder {
*/
public Map<String,String> createIndex(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
List<CreaterTableColumnModel> columnList = builderModel.getModel().getColumnList();
// 处理数据
for (CreaterTableColumnModel columnModel : columnList) {
@ -125,7 +125,7 @@ public enum VueCodeBuilder {
*/
public Map<String,String> createEdit(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
List<CreaterTableColumnModel> columnList = builderModel.getModel().getColumnList();
List<List<CreaterTableColumnModel>> formList = new ArrayList<>();
Map<String,List<String>> valiDict = new HashMap<>();
@ -230,7 +230,7 @@ public enum VueCodeBuilder {
*/
public Map<String,String> createImport(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
List<CreaterTableColumnModel> columnList = builderModel.getModel().getColumnList();
// 处理数据
for (CreaterTableColumnModel columnModel : columnList) {
@ -275,7 +275,7 @@ public enum VueCodeBuilder {
*/
public Map<String,String> createApi(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
List<CreaterTableColumnModel> columnList = builderModel.getModel().getColumnList();
// 处理数据
for (CreaterTableColumnModel columnModel : columnList) {

@ -49,7 +49,7 @@ public enum ReadMeBuilder {
*/
public Map<String,String> createReadMe(CreaterBuilderModel builderModelTmp, String dataStr){
CreaterBuilderModel builderModel =
WrapperUtil.cloneTransformInstance(builderModelTmp, CreaterBuilderModel.class);
WrapperUtil.transformInstance(builderModelTmp, CreaterBuilderModel.class, true);
String codeStr = EnjoyUtil.render("/readme/TemplateReadMe.html",
this.createKv(builderModel)

@ -112,7 +112,7 @@ public class ExcelPlugin {
// 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
excelReader.finish();
return getExtendsBeanList(excelListener.getDataList(),rowModel);
return getExtendsBeanList(excelListener.getDataList(), rowModel);
}
/**
@ -201,6 +201,6 @@ public class ExcelPlugin {
* BeanCopylist
*/
public <T> List<T> getExtendsBeanList(List<?> list,Class<T> typeClazz){
return WrapperUtil.cloneTransformInstance(list,typeClazz);
return WrapperUtil.transformInstance(list, typeClazz);
}
}

Loading…
Cancel
Save