Reproduce优化
This commit is contained in:
@@ -111,6 +111,23 @@ public abstract class AbstractCacheSource extends AbstractService implements Cac
|
||||
return source;
|
||||
}
|
||||
|
||||
protected <U> CompletableFuture<U> supplyFuture(Supplier<U> supplier) {
|
||||
try {
|
||||
return CompletableFuture.completedFuture(supplier.get());
|
||||
} catch (Throwable t) {
|
||||
return CompletableFuture.failedFuture(t);
|
||||
}
|
||||
}
|
||||
|
||||
protected CompletableFuture<Void> runFuture(Runnable runner) {
|
||||
try {
|
||||
runner.run();
|
||||
return CompletableFuture.completedFuture(null);
|
||||
} catch (Throwable t) {
|
||||
return CompletableFuture.failedFuture(t);
|
||||
}
|
||||
}
|
||||
|
||||
protected <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
|
||||
return CompletableFuture.supplyAsync(supplier);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -173,6 +173,10 @@ public interface Creator<T> {
|
||||
return (Object... params) -> func.apply(params);
|
||||
}
|
||||
|
||||
public static <T> Creator<T> load(Class<T> clazz) {
|
||||
return CreatorInner.creatorCacheMap.computeIfAbsent(clazz, v -> create(clazz));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据指定的class采用ASM技术生产Creator。
|
||||
*
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.redkale.util;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.*;
|
||||
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
|
||||
import org.redkale.asm.*;
|
||||
@@ -22,6 +23,34 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
|
||||
@Override
|
||||
public D apply(D dest, S src);
|
||||
|
||||
public static <D, S> D copy(final D dest, final S src) {
|
||||
if (src == null || dest == null) {
|
||||
return null;
|
||||
}
|
||||
Class<D> destClass = (Class<D>) dest.getClass();
|
||||
Creator<D> creator = Creator.load(destClass);
|
||||
return load(destClass, (Class<S>) src.getClass()).apply(creator.create(), src);
|
||||
}
|
||||
|
||||
public static <D, S> D copy(final Class<D> destClass, final S src) {
|
||||
if (src == null) {
|
||||
return null;
|
||||
}
|
||||
Creator<D> creator = Creator.load(destClass);
|
||||
return load(destClass, (Class<S>) src.getClass()).apply(creator.create(), src);
|
||||
}
|
||||
|
||||
public static <D, S> Reproduce<D, S> load(final Class<D> destClass, final Class<S> srcClass) {
|
||||
if (destClass == srcClass) {
|
||||
return ReproduceInner.reproduceOneCaches
|
||||
.computeIfAbsent(destClass, v -> create(destClass, srcClass));
|
||||
} else {
|
||||
return ReproduceInner.reproduceTwoCaches
|
||||
.computeIfAbsent(destClass, t -> new ConcurrentHashMap<>())
|
||||
.computeIfAbsent(srcClass, v -> create(destClass, srcClass));
|
||||
}
|
||||
}
|
||||
|
||||
public static <D, S> Reproduce<D, S> create(final Class<D> destClass, final Class<S> srcClass) {
|
||||
return create(destClass, srcClass, (BiPredicate) null, (Map<String, String>) null);
|
||||
}
|
||||
@@ -84,16 +113,26 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
|
||||
//mv.setDebug(true);
|
||||
|
||||
for (java.lang.reflect.Field field : srcClass.getFields()) {
|
||||
if (Modifier.isStatic(field.getModifiers())) continue;
|
||||
if (Modifier.isFinal(field.getModifiers())) continue;
|
||||
if (!Modifier.isPublic(field.getModifiers())) continue;
|
||||
if (Modifier.isStatic(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
if (Modifier.isFinal(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
if (!Modifier.isPublic(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
final String sfname = field.getName();
|
||||
if (srcColumnPredicate != null && !srcColumnPredicate.test(field, sfname)) continue;
|
||||
if (srcColumnPredicate != null && !srcColumnPredicate.test(field, sfname)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final String dfname = names == null ? sfname : names.getOrDefault(sfname, sfname);
|
||||
java.lang.reflect.Method setter = null;
|
||||
try {
|
||||
if (!field.getType().equals(destClass.getField(dfname).getType())) continue;
|
||||
if (!field.getType().equals(destClass.getField(dfname).getType())) {
|
||||
continue;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
char[] cs = dfname.toCharArray();
|
||||
@@ -116,19 +155,31 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
|
||||
}
|
||||
|
||||
for (java.lang.reflect.Method getter : srcClass.getMethods()) {
|
||||
if (Modifier.isStatic(getter.getModifiers())) continue;
|
||||
if (getter.getParameterTypes().length > 0) continue;
|
||||
if ("getClass".equals(getter.getName())) continue;
|
||||
if (!getter.getName().startsWith("get") && !getter.getName().startsWith("is")) continue;
|
||||
if (Modifier.isStatic(getter.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
if (getter.getParameterTypes().length > 0) {
|
||||
continue;
|
||||
}
|
||||
if ("getClass".equals(getter.getName())) {
|
||||
continue;
|
||||
}
|
||||
if (!getter.getName().startsWith("get") && !getter.getName().startsWith("is")) {
|
||||
continue;
|
||||
}
|
||||
final boolean is = getter.getName().startsWith("is");
|
||||
String sfname = getter.getName().substring(is ? 2 : 3);
|
||||
if (sfname.isEmpty()) continue;
|
||||
if (sfname.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (sfname.length() < 2 || Character.isLowerCase(sfname.charAt(1))) {
|
||||
char[] cs = sfname.toCharArray();
|
||||
cs[0] = Character.toLowerCase(cs[0]);
|
||||
sfname = new String(cs);
|
||||
}
|
||||
if (srcColumnPredicate != null && !srcColumnPredicate.test(getter, sfname)) continue;
|
||||
if (srcColumnPredicate != null && !srcColumnPredicate.test(getter, sfname)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final String dfname = names == null ? sfname : names.getOrDefault(sfname, sfname);
|
||||
java.lang.reflect.Method setter = null;
|
||||
@@ -141,7 +192,9 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
srcField = destClass.getField(dfname);
|
||||
if (!getter.getReturnType().equals(srcField.getType())) continue;
|
||||
if (!getter.getReturnType().equals(srcField.getType())) {
|
||||
continue;
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
continue;
|
||||
}
|
||||
@@ -190,4 +243,12 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
|
||||
}
|
||||
}
|
||||
|
||||
static class ReproduceInner {
|
||||
|
||||
static final ConcurrentHashMap<Class, Reproduce> reproduceOneCaches = new ConcurrentHashMap();
|
||||
|
||||
static final ConcurrentHashMap<Class, ConcurrentHashMap<Class, Reproduce>> reproduceTwoCaches = new ConcurrentHashMap();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -884,6 +884,39 @@ public final class Utility {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个版本号的大小,ver1小于ver2返回 -1
|
||||
*
|
||||
* @param version1 版本号
|
||||
* @param version2 版本号
|
||||
*
|
||||
* @return 版本大小
|
||||
*/
|
||||
public static int compareVersion(String version1, String version2) {
|
||||
if (isEmpty(version1)) {
|
||||
return isEmpty(version2) ? 0 : -1;
|
||||
}
|
||||
if (isEmpty(version2)) {
|
||||
return 1;
|
||||
}
|
||||
String[] ver1 = version1.split("\\.");
|
||||
String[] ver2 = version2.split("\\.");
|
||||
int len = Math.min(ver1.length, ver2.length);
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (ver1[i].length() > ver2[i].length()) {
|
||||
return 1;
|
||||
}
|
||||
if (ver1[i].length() < ver2[i].length()) {
|
||||
return -1;
|
||||
}
|
||||
int v = Integer.parseInt(ver1[i]) - Integer.parseInt(ver2[i]);
|
||||
if (v != 0) {
|
||||
return v > 0 ? 1 : -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将一个或多个新元素添加到数组开始,数组中的元素自动后移
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user