diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/BeanListCopyUtil.java b/hutool-core/src/main/java/cn/hutool/core/bean/BeanListCopyUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..bc01b1d3289d1b06786616a8a52b4fc8d8d2b996 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/bean/BeanListCopyUtil.java @@ -0,0 +1,50 @@ +package cn.hutool.core.bean; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Supplier; + +/** + * + *

+ * 集合复制工具类 + *

+ * + * @author ms
+ * @email
+ * @date 2021-8-25 16:37:46
+ */ +public class BeanListCopyUtil extends BeanUtil { + + /** + * 集合数据的拷贝 + * @param sources: 数据源类 + * @param target: 目标类::new(eg: UserVO::new) + * @return 复制后的集合数据 + */ + public static List copyListProperties(List sources, Supplier target) { + return copyListProperties(sources, target, null); + } + + + /** + * 带回调函数的集合数据的拷贝(可自定义字段拷贝规则) + * @param sources: 数据源类 + * @param target: 目标类::new(eg: UserVO::new) + * @param callBack: 回调函数 + * @return 复制后的集合数据 + */ + public static List copyListProperties(List sources, Supplier target, BeanListCopyUtilCallBack callBack) { + List list = new ArrayList<>(sources.size()); + for (S source : sources) { + T t = target.get(); + copyProperties(source, t); + list.add(t); + if (callBack != null) { + // 回调 + callBack.callBack(source, t); + } + } + return list; + } +} diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/BeanListCopyUtilCallBack.java b/hutool-core/src/main/java/cn/hutool/core/bean/BeanListCopyUtilCallBack.java new file mode 100644 index 0000000000000000000000000000000000000000..0ad13adb1d16ba95bbaf09e9960cf45551b91379 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/bean/BeanListCopyUtilCallBack.java @@ -0,0 +1,22 @@ +package cn.hutool.core.bean; + +/** + * + *

+ * 集合复制回调工具类 + *

+ * + * @author ms
+ * @email
+ * @date 2021-8-25 16:37:46
+ */ +@FunctionalInterface +public interface BeanListCopyUtilCallBack { + + /** + * 定义默认回调方法 + * @param t + * @param s + */ + void callBack(S t, T s); +} diff --git a/hutool-core/src/test/java/cn/hutool/core/bean/BeanListCopyUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/bean/BeanListCopyUtilTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e549c0e2f580c846114476cb256bc2a3a76687fb --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/bean/BeanListCopyUtilTest.java @@ -0,0 +1,60 @@ +package cn.hutool.core.bean; + +import cn.hutool.core.lang.Console; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * 集合复制工具类单元测试 + * + * @author ms + */ +public class BeanListCopyUtilTest { + + @Test + public void copyListPropertiesTest() { + List list1 = new ArrayList<>(); + TestEntity1 test1 = new TestEntity1(); + test1.setName("张三"); + test1.setSex(0); + list1.add(test1); + + TestEntity1 test2 = new TestEntity1(); + test2.setName("李四"); + test2.setSex(1); + list1.add(test2); + + List list2 = BeanListCopyUtil.copyListProperties(list1, TestEntity2::new); + List list3 = BeanListCopyUtil.copyListProperties(list1, TestEntity2::new, + new BeanListCopyUtilCallBack() { + @Override + public void callBack(TestEntity1 source,TestEntity2 target) { + target.setSexName(source.getSex() == 0 ? "男" : "女"); + } + }); + Console.log(list1); + Console.log(list2); + Console.log(list3); + + } + + @Data + @EqualsAndHashCode + private static class TestEntity1 { + private String name; + private Integer sex; + } + + @Data + @EqualsAndHashCode + private static class TestEntity2 { + private String name; + private Integer sex; + private String sexName; + } + +}