From c43e516d147fc474845c493a71222a198182cde1 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Mon, 26 Sep 2016 13:51:14 +0800 Subject: [PATCH] --- src/org/redkale/source/EntityCache.java | 12 ++++++------ src/org/redkale/util/Reproduce.java | 13 +++++++------ test/org/redkale/test/util/UntilTestMain.java | 6 +++--- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/org/redkale/source/EntityCache.java b/src/org/redkale/source/EntityCache.java index 367cd0707..d7b2817b0 100644 --- a/src/org/redkale/source/EntityCache.java +++ b/src/org/redkale/source/EntityCache.java @@ -108,14 +108,14 @@ public final class EntityCache { public T find(Serializable id) { if (id == null) return null; T rs = map.get(id); - return rs == null ? null : (needcopy ? newReproduce.copy(this.creator.create(), rs) : rs); + return rs == null ? null : (needcopy ? newReproduce.apply(this.creator.create(), rs) : rs); } public T find(final SelectColumn selects, final Serializable id) { if (id == null) return null; T rs = map.get(id); if (rs == null) return null; - if (selects == null) return (needcopy ? newReproduce.copy(this.creator.create(), rs) : rs); + if (selects == null) return (needcopy ? newReproduce.apply(this.creator.create(), rs) : rs); T t = this.creator.create(); for (Attribute attr : this.info.attributes) { if (selects.test(attr.field())) attr.set(t, attr.get(rs)); @@ -129,7 +129,7 @@ public final class EntityCache { if (filter != null) stream = stream.filter(filter); Optional opt = stream.findFirst(); if (!opt.isPresent()) return null; - if (selects == null) return (needcopy ? newReproduce.copy(this.creator.create(), opt.get()) : opt.get()); + if (selects == null) return (needcopy ? newReproduce.apply(this.creator.create(), opt.get()) : opt.get()); T rs = opt.get(); T t = this.creator.create(); for (Attribute attr : this.info.attributes) { @@ -309,7 +309,7 @@ public final class EntityCache { if (flipper != null) stream = stream.skip(flipper.getOffset()).limit(flipper.getLimit()); final List rs = new ArrayList<>(); if (selects == null) { - Consumer action = x -> rs.add(needcopy ? newReproduce.copy(creator.create(), x) : x); + Consumer action = x -> rs.add(needcopy ? newReproduce.apply(creator.create(), x) : x); if (comparator != null) { stream.forEachOrdered(action); } else { @@ -339,7 +339,7 @@ public final class EntityCache { public void insert(T value) { if (value == null) return; - final T rs = newReproduce.copy(this.creator.create(), value); //确保同一主键值的map与list中的对象必须共用。 + final T rs = newReproduce.apply(this.creator.create(), value); //确保同一主键值的map与list中的对象必须共用。 T old = this.map.put(this.primary.get(rs), rs); if (old == null) { this.list.add(rs); @@ -372,7 +372,7 @@ public final class EntityCache { if (value == null) return; T rs = this.map.get(this.primary.get(value)); if (rs == null) return; - this.chgReproduce.copy(rs, value); + this.chgReproduce.apply(rs, value); } public T update(final T value, Collection> attrs) { diff --git a/src/org/redkale/util/Reproduce.java b/src/org/redkale/util/Reproduce.java index 8f54c3be8..386e3de3d 100644 --- a/src/org/redkale/util/Reproduce.java +++ b/src/org/redkale/util/Reproduce.java @@ -1,7 +1,7 @@ package org.redkale.util; import java.lang.reflect.Modifier; -import java.util.function.Predicate; +import java.util.function.*; import static jdk.internal.org.objectweb.asm.Opcodes.*; import jdk.internal.org.objectweb.asm.*; import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_FRAMES; @@ -15,9 +15,10 @@ import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_FRAMES; * @param 目标对象的数据类型 * @param 源对象的数据类型 */ -public interface Reproduce { +public interface Reproduce extends BiFunction{ - public D copy(D dest, S src); + @Override + public D apply(D dest, S src); public static Reproduce create(final Class destClass, final Class srcClass) { return create(destClass, srcClass, null); @@ -59,7 +60,7 @@ public interface Reproduce { mv.visitEnd(); } { - mv = (cw.visitMethod(ACC_PUBLIC, "copy", "(" + destDesc + srcDesc + ")" + destDesc, null, null)); + mv = (cw.visitMethod(ACC_PUBLIC, "apply", "(" + destDesc + srcDesc + ")" + destDesc, null, null)); //mv.setDebug(true); for (java.lang.reflect.Field field : srcClass.getFields()) { @@ -112,14 +113,14 @@ public interface Reproduce { mv.visitEnd(); } { - mv = (cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "copy", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null)); + mv = (cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "apply", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null)); //mv.setDebug(true); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, destName); mv.visitVarInsn(ALOAD, 2); mv.visitTypeInsn(CHECKCAST, srcName); - mv.visitMethodInsn(INVOKEVIRTUAL, newDynName, "copy", "(" + destDesc + srcDesc + ")" + destDesc, false); + mv.visitMethodInsn(INVOKEVIRTUAL, newDynName, "apply", "(" + destDesc + srcDesc + ")" + destDesc, false); mv.visitInsn(ARETURN); mv.visitMaxs(3, 3); mv.visitEnd(); diff --git a/test/org/redkale/test/util/UntilTestMain.java b/test/org/redkale/test/util/UntilTestMain.java index 33f147e9b..67c78cbe5 100644 --- a/test/org/redkale/test/util/UntilTestMain.java +++ b/test/org/redkale/test/util/UntilTestMain.java @@ -29,7 +29,7 @@ public class UntilTestMain { Reproduce action2 = new Reproduce() { @Override - public TestXBean copy(TestXBean dest, TestBean src) { + public TestXBean apply(TestXBean dest, TestBean src) { dest.time = src.time; dest.setId(src.getId()); dest.setName(src.getName()); @@ -40,13 +40,13 @@ public class UntilTestMain { final int count = 1_000_000; long s = System.nanoTime(); for (int i = 0; i < count; i++) { - action2.copy(beanx, bean); + action2.apply(beanx, bean); } long e = System.nanoTime() - s; System.out.println("静态Reproduce耗时: " + e); s = System.nanoTime(); for (int i = 0; i < count; i++) { - action1.copy(beanx, bean); + action1.apply(beanx, bean); } e = System.nanoTime() - s; System.out.println("动态Reproduce耗时: " + e);