convertListFrom

This commit is contained in:
redkale
2024-09-17 12:16:46 +08:00
parent 8cc83af502
commit f2992a76fa
7 changed files with 38 additions and 5 deletions

View File

@@ -66,6 +66,8 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
private final ConcurrentHashMap<Class, BiFunction> fieldFuncs = new ConcurrentHashMap();
private final ConcurrentHashMap<Type, Type> genericListTypes = new ConcurrentHashMap<>();
private final Set<Class> skipIgnores = new HashSet();
final Set<String> ignoreMapColumns = new HashSet();
@@ -253,6 +255,8 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
public abstract ConvertFactory createChild(int features);
protected abstract ConvertFactory rootFactory();
protected SimpledCoder createEnumSimpledCoder(Class enumClass) {
return new EnumSimpledCoder(this, enumClass);
}
@@ -372,6 +376,11 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
return nullable ? addFeature(Convert.FEATURE_NULLABLE) : removeFeature(Convert.FEATURE_NULLABLE);
}
protected Type createGenericListType(Type componentType) {
ConcurrentHashMap<Type, Type> map = rootFactory().genericListTypes;
return map.computeIfAbsent(componentType, t -> TypeToken.createParameterizedType(null, List.class, t));
}
public static boolean checkTinyFeature(int features) {
return (features & Convert.FEATURE_TINY) > 0;
}

View File

@@ -6,6 +6,7 @@
package org.redkale.convert;
import java.lang.reflect.Type;
import java.util.List;
/**
* 文本序列化/反序列化操作类
@@ -29,6 +30,10 @@ public abstract class TextConvert<R extends Reader, W extends Writer> extends Co
public abstract <T> T convertFrom(final Type type, final String text);
public <T> List<T> convertListFrom(final Type componentType, final String text) {
return (List) convertFrom(factory.createGenericListType(componentType), text);
}
/**
* 序列化
*

View File

@@ -89,6 +89,11 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
return this;
}
@Override
protected ConvertFactory rootFactory() {
return instance;
}
public static BsonFactory root() {
return instance;
}

View File

@@ -91,6 +91,11 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
return this;
}
@Override
protected ConvertFactory rootFactory() {
return instance;
}
public static JsonFactory root() {
return instance;
}

View File

@@ -53,6 +53,11 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
}
}
@Override
protected ConvertFactory rootFactory() {
return instance;
}
public static ProtobufFactory root() {
return instance;
}

View File

@@ -4,12 +4,11 @@
*/
package org.redkale.util;
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
import static org.redkale.asm.Opcodes.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
import static org.redkale.asm.Opcodes.*;
/**
* 获取泛型的Type类
@@ -568,7 +567,8 @@ public abstract class TypeToken<T> {
}
/**
* 动态创建 ParameterizedType
* 动态创建 ParameterizedType <br>
* 例如: List&lt;String&gt;: createParameterizedType(null, List.class, String.class)
*
* @param ownerType0 ParameterizedType 的 ownerType, 一般为null
* @param rawType0 ParameterizedType 的 rawType

View File

@@ -21,11 +21,15 @@ public class ScheduledTest {
ScheduleManagerService manager = ScheduleManagerService.create(null);
manager.init(null);
ScheduledTestService service = new ScheduledTestService();
long s = 1000 - System.currentTimeMillis() % 1000;
if (s > 0) {
Utility.sleep(s);
}
manager.schedule(service);
System.out.println("开始执行");
Utility.sleep(2000);
manager.stop("task2");
Utility.sleep(1001);
Utility.sleep(1010);
manager.unschedule(service);
manager.destroy(null);
Assertions.assertEquals(3, service.count1.get());