This commit is contained in:
@@ -108,14 +108,14 @@ public final class EntityCache<T> {
|
|||||||
public T find(Serializable id) {
|
public T find(Serializable id) {
|
||||||
if (id == null) return null;
|
if (id == null) return null;
|
||||||
T rs = map.get(id);
|
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) {
|
public T find(final SelectColumn selects, final Serializable id) {
|
||||||
if (id == null) return null;
|
if (id == null) return null;
|
||||||
T rs = map.get(id);
|
T rs = map.get(id);
|
||||||
if (rs == null) return null;
|
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();
|
T t = this.creator.create();
|
||||||
for (Attribute attr : this.info.attributes) {
|
for (Attribute attr : this.info.attributes) {
|
||||||
if (selects.test(attr.field())) attr.set(t, attr.get(rs));
|
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);
|
if (filter != null) stream = stream.filter(filter);
|
||||||
Optional<T> opt = stream.findFirst();
|
Optional<T> opt = stream.findFirst();
|
||||||
if (!opt.isPresent()) return null;
|
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 rs = opt.get();
|
||||||
T t = this.creator.create();
|
T t = this.creator.create();
|
||||||
for (Attribute attr : this.info.attributes) {
|
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());
|
if (flipper != null) stream = stream.skip(flipper.getOffset()).limit(flipper.getLimit());
|
||||||
final List<T> rs = new ArrayList<>();
|
final List<T> rs = new ArrayList<>();
|
||||||
if (selects == null) {
|
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) {
|
if (comparator != null) {
|
||||||
stream.forEachOrdered(action);
|
stream.forEachOrdered(action);
|
||||||
} else {
|
} else {
|
||||||
@@ -339,7 +339,7 @@ public final class EntityCache<T> {
|
|||||||
|
|
||||||
public void insert(T value) {
|
public void insert(T value) {
|
||||||
if (value == null) return;
|
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);
|
T old = this.map.put(this.primary.get(rs), rs);
|
||||||
if (old == null) {
|
if (old == null) {
|
||||||
this.list.add(rs);
|
this.list.add(rs);
|
||||||
@@ -372,7 +372,7 @@ public final class EntityCache<T> {
|
|||||||
if (value == null) return;
|
if (value == null) return;
|
||||||
T rs = this.map.get(this.primary.get(value));
|
T rs = this.map.get(this.primary.get(value));
|
||||||
if (rs == null) return;
|
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) {
|
public T update(final T value, Collection<Attribute<T, Serializable>> attrs) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package org.redkale.util;
|
package org.redkale.util;
|
||||||
|
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.*;
|
||||||
import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
||||||
import jdk.internal.org.objectweb.asm.*;
|
import jdk.internal.org.objectweb.asm.*;
|
||||||
import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;
|
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 <D> 目标对象的数据类型
|
||||||
* @param <S> 源对象的数据类型
|
* @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) {
|
public static <D, S> Reproduce<D, S> create(final Class<D> destClass, final Class<S> srcClass) {
|
||||||
return create(destClass, srcClass, null);
|
return create(destClass, srcClass, null);
|
||||||
@@ -59,7 +60,7 @@ public interface Reproduce<D, S> {
|
|||||||
mv.visitEnd();
|
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);
|
//mv.setDebug(true);
|
||||||
|
|
||||||
for (java.lang.reflect.Field field : srcClass.getFields()) {
|
for (java.lang.reflect.Field field : srcClass.getFields()) {
|
||||||
@@ -112,14 +113,14 @@ public interface Reproduce<D, S> {
|
|||||||
mv.visitEnd();
|
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.setDebug(true);
|
||||||
mv.visitVarInsn(ALOAD, 0);
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
mv.visitVarInsn(ALOAD, 1);
|
mv.visitVarInsn(ALOAD, 1);
|
||||||
mv.visitTypeInsn(CHECKCAST, destName);
|
mv.visitTypeInsn(CHECKCAST, destName);
|
||||||
mv.visitVarInsn(ALOAD, 2);
|
mv.visitVarInsn(ALOAD, 2);
|
||||||
mv.visitTypeInsn(CHECKCAST, srcName);
|
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.visitInsn(ARETURN);
|
||||||
mv.visitMaxs(3, 3);
|
mv.visitMaxs(3, 3);
|
||||||
mv.visitEnd();
|
mv.visitEnd();
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class UntilTestMain {
|
|||||||
Reproduce<TestXBean, TestBean> action2 = new Reproduce<TestXBean, TestBean>() {
|
Reproduce<TestXBean, TestBean> action2 = new Reproduce<TestXBean, TestBean>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TestXBean copy(TestXBean dest, TestBean src) {
|
public TestXBean apply(TestXBean dest, TestBean src) {
|
||||||
dest.time = src.time;
|
dest.time = src.time;
|
||||||
dest.setId(src.getId());
|
dest.setId(src.getId());
|
||||||
dest.setName(src.getName());
|
dest.setName(src.getName());
|
||||||
@@ -40,13 +40,13 @@ public class UntilTestMain {
|
|||||||
final int count = 1_000_000;
|
final int count = 1_000_000;
|
||||||
long s = System.nanoTime();
|
long s = System.nanoTime();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
action2.copy(beanx, bean);
|
action2.apply(beanx, bean);
|
||||||
}
|
}
|
||||||
long e = System.nanoTime() - s;
|
long e = System.nanoTime() - s;
|
||||||
System.out.println("静态Reproduce耗时: " + e);
|
System.out.println("静态Reproduce耗时: " + e);
|
||||||
s = System.nanoTime();
|
s = System.nanoTime();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
action1.copy(beanx, bean);
|
action1.apply(beanx, bean);
|
||||||
}
|
}
|
||||||
e = System.nanoTime() - s;
|
e = System.nanoTime() - s;
|
||||||
System.out.println("动态Reproduce耗时: " + e);
|
System.out.println("动态Reproduce耗时: " + e);
|
||||||
|
|||||||
Reference in New Issue
Block a user