新增Copier

This commit is contained in:
redkale
2023-08-07 11:43:17 +08:00
parent 906c147ed6
commit 6d613f715f
12 changed files with 1041 additions and 103 deletions

View File

@@ -4,16 +4,16 @@
package org.redkale.test.util;
import java.util.Map;
import org.redkale.util.Reproduce;
import org.redkale.util.Copier;
/**
*
* @author zhangjx
*/
public class ReproduceBeanMap implements Reproduce<Map<String, Object>, TestInterface> {
public class CopierBeanMap implements Copier<TestInterface, Map<String, Object>> {
@Override
public Map apply(Map<String, Object> dest, TestInterface src) {
public Map apply(TestInterface src, Map<String, Object> dest) {
Object v;
dest.put("id", src.getId());
@@ -24,7 +24,7 @@ public class ReproduceBeanMap implements Reproduce<Map<String, Object>, TestInte
return dest;
}
public Map run(Map<String, Object> dest, TestBean src) {
public Map run(TestBean src, Map<String, Object> dest) {
Object v;
v = src.getName();

View File

@@ -9,10 +9,10 @@ import org.redkale.util.*;
/**
* @author zhangjx
*/
public class ReproduceMapBean implements Reproduce<TestBean, HashMap> {
public class CopierMapBean implements Copier<HashMap, TestBean> {
@Override
public TestBean apply(TestBean dest, HashMap src) {
public TestBean apply(HashMap src, TestBean dest) {
src.forEach((k, v) -> {
if ("id".equals(k) && v != null) {
dest.setId(Utility.convertValue(int.class, v));

View File

@@ -13,10 +13,10 @@ import org.redkale.util.*;
*
* @author zhangjx
*/
public class ReproduceTest {
public class CopierTest {
public static void main(String[] args) throws Throwable {
ReproduceTest test = new ReproduceTest();
CopierTest test = new CopierTest();
test.run1();
test.run2();
test.run3();
@@ -33,9 +33,9 @@ public class ReproduceTest {
bean.time = 55555L;
bean.setName("haha");
bean.setMap(Utility.ofMap("aa", "bbb"));
Map map = new TreeMap(Reproduce.copy(Map.class, bean));
Map map = new TreeMap(Copier.copy(bean, Map.class));
System.out.println(JsonConvert.root().convertTo(map));
TreeMap rs = Reproduce.copy(TreeMap.class, bean);
TreeMap rs = Copier.copy(bean, TreeMap.class);
Assertions.assertEquals(bean.toString(), JsonConvert.root().convertTo(rs));
}
@@ -47,7 +47,7 @@ public class ReproduceTest {
bean.setName("haha");
bean.setMap(Utility.ofMap("aa", "bbb"));
TreeMap rs = new TreeMap();
Reproduce.load(Map.class, TestInterface.class).apply(rs, bean);
Copier.load(TestInterface.class, Map.class).apply(bean, rs);
System.out.println(JsonConvert.root().convertTo(rs));
}
@@ -59,8 +59,8 @@ public class ReproduceTest {
map.put("id", "222");
map.put("map", Utility.ofMap("aa", "bbb"));
TestBean bean = new TestBean();
Reproduce.load(TestInterface.class, Map.class).apply(bean, map);
Assertions.assertEquals("{\"id\":222,\"map\":{\"aa\":\"bbb\"},\"time\":0}", JsonConvert.root().convertTo(bean));
TestInterface ti = Copier.load(Map.class, TestInterface.class).apply(map, new TestBean());;
Assertions.assertEquals("{\"id\":222,\"map\":{\"aa\":\"bbb\"},\"time\":0}", JsonConvert.root().convertTo(ti));
}
@Test
@@ -71,7 +71,7 @@ public class ReproduceTest {
map.put("time", "55555");
map.put("id", "222");
map.put("map", Utility.ofMap("aa", "bbb"));
Reproduce.load(TestBean.class, Map.class).apply(bean, map);
Copier.load(Map.class, TestBean.class).apply(map, bean);
System.out.println(JsonConvert.root().convertTo(bean));
map.put("time", 55555L);
map.put("id", 222);
@@ -86,7 +86,7 @@ public class ReproduceTest {
map.put("id", "222");
map.put("map", Utility.ofMap("aa", "bbb"));
Map rs = new TreeMap();
Reproduce.load(Map.class, Map.class).apply(rs, map);
Copier.load(Map.class, Map.class).apply(map, rs);
System.out.println("Map: " + JsonConvert.root().convertTo(rs));
Assertions.assertEquals(JsonConvert.root().convertTo(map), JsonConvert.root().convertTo(rs));
}
@@ -98,7 +98,7 @@ public class ReproduceTest {
bean.time = 55555L;
bean.setName(null);
bean.setMap(Utility.ofMap("aa", "bbb"));
ConcurrentHashMap rs = Reproduce.copy(ConcurrentHashMap.class, bean);
ConcurrentHashMap rs = Copier.copy(bean, ConcurrentHashMap.class);
System.out.println(JsonConvert.root().convertTo(rs));
System.out.println("------------------------------------------");
}
@@ -111,7 +111,7 @@ public class ReproduceTest {
map.put("time", "55555");
map.put("id", null);
map.put("map", Utility.ofMap("aa", "bbb"));
Reproduce.load(TestBean.class, Map.class).apply(bean, map);
Copier.load(Map.class, TestBean.class).apply(map, bean);
System.out.println(JsonConvert.root().convertTo(bean));
}
}

View File

@@ -18,7 +18,7 @@ import org.redkale.util.*;
public class UntilTestMain {
public static void main(String[] args) throws Throwable {
reproduce(args);
copy(args);
attribute(args);
aes(args);
}
@@ -46,17 +46,17 @@ public class UntilTestMain {
System.out.println(Utility.binToHexString(secret.getBytes()));
}
public static void reproduce(String[] args) throws Throwable {
public static void copy(String[] args) throws Throwable {
final TestBean bean = new TestBean();
bean.setId(123456);
bean.setName("zhangjx");
bean.time = 2000;
final TestXBean beanx = new TestXBean();
Reproduce<TestXBean, TestBean> action1 = Reproduce.create(TestXBean.class, TestBean.class);
Reproduce<TestXBean, TestBean> action2 = new Reproduce<TestXBean, TestBean>() {
Copier<TestBean, TestXBean> action1 = Copier.create(TestBean.class, TestXBean.class);
Copier<TestBean, TestXBean> action2 = new Copier<TestBean, TestXBean>() {
@Override
public TestXBean apply(TestXBean dest, TestBean src) {
public TestXBean apply(TestBean src, TestXBean dest) {
dest.time = src.time;
dest.setId(src.getId());
dest.setName(src.getName());
@@ -67,13 +67,13 @@ public class UntilTestMain {
final int count = 1_000_000;
long s = System.nanoTime();
for (int i = 0; i < count; i++) {
action2.apply(beanx, bean);
action2.apply(bean, beanx);
}
long e = System.nanoTime() - s;
System.out.println("静态Reproduce耗时: " + e);
s = System.nanoTime();
for (int i = 0; i < count; i++) {
action1.apply(beanx, bean);
action1.apply(bean, beanx);
}
e = System.nanoTime() - s;
System.out.println("动态Reproduce耗时: " + e);