| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package com.inspur.customer.utils;
- import com.google.common.collect.Lists;
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.BeanUtils;
- import org.springframework.util.Assert;
- import org.springframework.util.CollectionUtils;
- import java.util.*;
- import java.util.function.BiConsumer;
- import java.util.function.Function;
- /**
- * @author zengweijie
- * @version 1.0
- * @date 2022/9/20 13:51
- **/
- @Slf4j
- public class BeanCopyUtils {
- private BeanCopyUtils() {
- }
- public static <T, S> List<S> copyList(List<T> fromList, Class<S> toObj) {
- return copyList(fromList, toObj, null);
- }
- public static <T, S> List<S> copyList(List<T> fromList, Class<S> toObj, BiConsumer<T, S> afterPropertyCopied) {
- if (CollectionUtils.isEmpty(fromList)) {
- return Collections.emptyList();
- }
- if (null == toObj) {
- log.error("Utils-->copyList:toObj is null");
- return Collections.emptyList();
- }
- List<S> toList = Lists.newArrayListWithCapacity(fromList.size());
- fromList.forEach(f -> {
- S t;
- try {
- t = toObj.getDeclaredConstructor().newInstance();
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- return;
- }
- BeanUtils.copyProperties(f, t);
- toList.add(t);
- Optional.ofNullable(afterPropertyCopied).ifPresent(c -> c.accept(f, t));
- });
- return toList;
- }
- public static <T, S> S copy(T t, Class<S> toObj) {
- return copy(t, toObj, null);
- }
- public static <T, S> S copy(T t, Class<S> toObj, BiConsumer<T, S> afterPropertyCopied) {
- Assert.notNull(toObj, "Utils-->copy:toObj is null");
- try {
- S s = toObj.getDeclaredConstructor().newInstance();
- if (Objects.nonNull(t)) {
- BeanUtils.copyProperties(t, s);
- }
- Optional.ofNullable(afterPropertyCopied).ifPresent(c -> c.accept(t, s));
- return s;
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- throw new RuntimeException(e);// NOSONAR
- }
- }
- public static <T, S> S copyIfNotNull(T t, Class<S> toObj) {
- if (Objects.isNull(t)) {
- return null;
- }
- return copy(t, toObj);
- }
- /**
- * 从哈希表中获取指定键列表对应的值列表,并将列表元素转换为指定的目标类型
- *
- * @param keyList 键列表
- * @param map 哈希表
- * @param toObj 指定的目标类型反射
- * @param <T> 值类型
- * @param <S> 指定目标类型
- * @param <K> 键类型
- * @return 哈希表中对应键集的成员集
- */
- public static <T, S, K> List<S> getFromMap(List<K> keyList, Map<K, T> map, Class<S> toObj) {
- return getFromMap(keyList, t -> t, map, toObj, null);
- }
- @Data
- @Builder
- @AllArgsConstructor
- @NoArgsConstructor
- public static class Entry<K, T> {
- private K key;
- private T value;
- }
- 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) {
- return getFromMap(keyList, keyConverter, map, toObj, afterPropertyCopied, null);
- }
- 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, BiConsumer<K, List<S>> onValueNotExists) {
- if (CollectionUtils.isEmpty(keyList)) {
- return Collections.emptyList();
- }
- if (null == toObj) {
- log.error("Utils-->copyList:toObj is null");
- return Collections.emptyList();
- }
- List<S> toList = Lists.newArrayListWithCapacity(keyList.size());
- keyList.forEach(key -> {
- S s;
- try {
- s = toObj.getDeclaredConstructor().newInstance();
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- return;
- }
- Optional.ofNullable(map.get(keyConverter.apply(key))).ifPresentOrElse(value -> {
- BeanUtils.copyProperties(value, s);
- Optional.ofNullable(afterPropertyCopied).ifPresent(c -> c.accept(Entry.<K, T>builder().key(key).value(value).build(), s));
- toList.add(s);
- }, () -> Optional.ofNullable(onValueNotExists).ifPresent(kListBiConsumer -> kListBiConsumer.accept(key, toList)));
- });
- return toList;
- }
- }
|