This commit is contained in:
Redkale
2016-09-26 13:51:14 +08:00
parent 8884fabf90
commit c43e516d14
3 changed files with 16 additions and 15 deletions

View File

@@ -108,14 +108,14 @@ public final class EntityCache<T> {
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<T> {
if (filter != null) stream = stream.filter(filter);
Optional<T> 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<T> {
if (flipper != null) stream = stream.skip(flipper.getOffset()).limit(flipper.getLimit());
final List<T> rs = new ArrayList<>();
if (selects == null) {
Consumer<? super T> action = x -> rs.add(needcopy ? newReproduce.copy(creator.create(), x) : x);
Consumer<? super T> 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<T> {
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<T> {
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<Attribute<T, Serializable>> attrs) {

View File

@@ -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 <D> 目标对象的数据类型
* @param <S> 源对象的数据类型
*/
public interface Reproduce<D, S> {
public interface Reproduce<D, S> extends BiFunction<D, S, D>{
public D copy(D dest, S src);
@Override
public D apply(D dest, S src);
public static <D, S> Reproduce<D, S> create(final Class<D> destClass, final Class<S> srcClass) {
return create(destClass, srcClass, null);
@@ -59,7 +60,7 @@ public interface Reproduce<D, S> {
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<D, S> {
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();

View File

@@ -29,7 +29,7 @@ public class UntilTestMain {
Reproduce<TestXBean, TestBean> action2 = new Reproduce<TestXBean, TestBean>() {
@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);