ConvertResponse.java
1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cn.com.glsx.entmicro.converter;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import java.util.List;
/**
* @author zhouhaibao
* @version 1.0
* @description: 返回体转化参数
* @date 2021/7/9 11:21
*/
@Data
public class ConvertResponse {
/**
* 转化集合
*
* @param input 原集合
* @param clazz 新集合类
* @param <T> 泛型
* @return
*/
public static <E, T> List<T> convertList(List<E> input, Class<T> clazz) {
List<T> output = Lists.newArrayList();
if (CollectionUtils.isNotEmpty(input)) {
for (E source : input) {
T target = BeanUtils.instantiateClass(clazz);
BeanUtils.copyProperties(source, target);
output.add(target);
}
}
return output;
}
/**
* 转化类型
*
* @param object 原类
* @param clazz 新类
* @return
*/
public static <T> T convertObject(Object object, Class<T> clazz) {
T target = BeanUtils.instantiateClass(clazz);
BeanUtils.copyProperties(object, target);
return target;
}
}