1
0

BeanCopyUtil.java 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.nju.edu.eval.util;
  2. import org.springframework.beans.BeanUtils;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.function.Supplier;
  6. /**
  7. * @author mars
  8. */
  9. public class BeanCopyUtil extends BeanUtils {
  10. /**
  11. * 集合数据的拷贝
  12. * @param sources: 数据源类
  13. * @param target: 目标类::new(eg: UserVO::new)
  14. * @return
  15. */
  16. public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
  17. return copyListProperties(sources, target, null);
  18. }
  19. /**
  20. * 带回调函数的集合数据的拷贝(可自定义字段拷贝规则)
  21. * @param sources: 数据源类
  22. * @param target: 目标类::new(eg: UserVO::new)
  23. * @param callBack: 回调函数
  24. * @return
  25. */
  26. public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack) {
  27. List<T> list = new ArrayList<>(sources.size());
  28. for (S source : sources) {
  29. T t = target.get();
  30. copyProperties(source, t);
  31. list.add(t);
  32. if (callBack != null) {
  33. // 回调
  34. callBack.callBack(source, t);
  35. }
  36. }
  37. return list;
  38. }
  39. }