BeanCopyUtils.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.inspur.customer.utils;
  2. import com.google.common.collect.Lists;
  3. import lombok.AllArgsConstructor;
  4. import lombok.Builder;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.beans.BeanUtils;
  9. import org.springframework.util.Assert;
  10. import org.springframework.util.CollectionUtils;
  11. import java.util.*;
  12. import java.util.function.BiConsumer;
  13. import java.util.function.Function;
  14. /**
  15. * @author zengweijie
  16. * @version 1.0
  17. * @date 2022/9/20 13:51
  18. **/
  19. @Slf4j
  20. public class BeanCopyUtils {
  21. private BeanCopyUtils() {
  22. }
  23. public static <T, S> List<S> copyList(List<T> fromList, Class<S> toObj) {
  24. return copyList(fromList, toObj, null);
  25. }
  26. public static <T, S> List<S> copyList(List<T> fromList, Class<S> toObj, BiConsumer<T, S> afterPropertyCopied) {
  27. if (CollectionUtils.isEmpty(fromList)) {
  28. return Collections.emptyList();
  29. }
  30. if (null == toObj) {
  31. log.error("Utils-->copyList:toObj is null");
  32. return Collections.emptyList();
  33. }
  34. List<S> toList = Lists.newArrayListWithCapacity(fromList.size());
  35. fromList.forEach(f -> {
  36. S t;
  37. try {
  38. t = toObj.getDeclaredConstructor().newInstance();
  39. } catch (Exception e) {
  40. log.error(e.getMessage(), e);
  41. return;
  42. }
  43. BeanUtils.copyProperties(f, t);
  44. toList.add(t);
  45. Optional.ofNullable(afterPropertyCopied).ifPresent(c -> c.accept(f, t));
  46. });
  47. return toList;
  48. }
  49. public static <T, S> S copy(T t, Class<S> toObj) {
  50. return copy(t, toObj, null);
  51. }
  52. public static <T, S> S copy(T t, Class<S> toObj, BiConsumer<T, S> afterPropertyCopied) {
  53. Assert.notNull(toObj, "Utils-->copy:toObj is null");
  54. try {
  55. S s = toObj.getDeclaredConstructor().newInstance();
  56. if (Objects.nonNull(t)) {
  57. BeanUtils.copyProperties(t, s);
  58. }
  59. Optional.ofNullable(afterPropertyCopied).ifPresent(c -> c.accept(t, s));
  60. return s;
  61. } catch (Exception e) {
  62. log.error(e.getMessage(), e);
  63. throw new RuntimeException(e);// NOSONAR
  64. }
  65. }
  66. public static <T, S> S copyIfNotNull(T t, Class<S> toObj) {
  67. if (Objects.isNull(t)) {
  68. return null;
  69. }
  70. return copy(t, toObj);
  71. }
  72. /**
  73. * 从哈希表中获取指定键列表对应的值列表,并将列表元素转换为指定的目标类型
  74. *
  75. * @param keyList 键列表
  76. * @param map 哈希表
  77. * @param toObj 指定的目标类型反射
  78. * @param <T> 值类型
  79. * @param <S> 指定目标类型
  80. * @param <K> 键类型
  81. * @return 哈希表中对应键集的成员集
  82. */
  83. public static <T, S, K> List<S> getFromMap(List<K> keyList, Map<K, T> map, Class<S> toObj) {
  84. return getFromMap(keyList, t -> t, map, toObj, null);
  85. }
  86. @Data
  87. @Builder
  88. @AllArgsConstructor
  89. @NoArgsConstructor
  90. public static class Entry<K, T> {
  91. private K key;
  92. private T value;
  93. }
  94. public static <T, S, K, V> List<S> getFromMap(List<K> keyList, Function<K, V> keyConverter, Map<V, T> map, Class<S> toObj, BiConsumer<Entry<K, T>, S> afterPropertyCopied) {
  95. return getFromMap(keyList, keyConverter, map, toObj, afterPropertyCopied, null);
  96. }
  97. public static <T, S, K, V> List<S> getFromMap(List<K> keyList, Function<K, V> keyConverter, Map<V, T> map, Class<S> toObj
  98. , BiConsumer<Entry<K, T>, S> afterPropertyCopied, BiConsumer<K, List<S>> onValueNotExists) {
  99. if (CollectionUtils.isEmpty(keyList)) {
  100. return Collections.emptyList();
  101. }
  102. if (null == toObj) {
  103. log.error("Utils-->copyList:toObj is null");
  104. return Collections.emptyList();
  105. }
  106. List<S> toList = Lists.newArrayListWithCapacity(keyList.size());
  107. keyList.forEach(key -> {
  108. S s;
  109. try {
  110. s = toObj.getDeclaredConstructor().newInstance();
  111. } catch (Exception e) {
  112. log.error(e.getMessage(), e);
  113. return;
  114. }
  115. Optional.ofNullable(map.get(keyConverter.apply(key))).ifPresentOrElse(value -> {
  116. BeanUtils.copyProperties(value, s);
  117. Optional.ofNullable(afterPropertyCopied).ifPresent(c -> c.accept(Entry.<K, T>builder().key(key).value(value).build(), s));
  118. toList.add(s);
  119. }, () -> Optional.ofNullable(onValueNotExists).ifPresent(kListBiConsumer -> kListBiConsumer.accept(key, toList)));
  120. });
  121. return toList;
  122. }
  123. }