diff --git a/storage/src/main/java/com/zfoo/storage/convert/ArrayConverter.java b/storage/src/main/java/com/zfoo/storage/convert/ArrayConverter.java index a97fc58db..1433645a8 100644 --- a/storage/src/main/java/com/zfoo/storage/convert/ArrayConverter.java +++ b/storage/src/main/java/com/zfoo/storage/convert/ArrayConverter.java @@ -26,6 +26,10 @@ */ public class ArrayConverter implements ConditionalGenericConverter { + public static final ArrayConverter INSTANCE = new ArrayConverter(); + + private ArrayConverter() { + } @Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { diff --git a/storage/src/main/java/com/zfoo/storage/convert/ConvertUtils.java b/storage/src/main/java/com/zfoo/storage/convert/ConvertUtils.java index bffb4df50..712a748d8 100644 --- a/storage/src/main/java/com/zfoo/storage/convert/ConvertUtils.java +++ b/storage/src/main/java/com/zfoo/storage/convert/ConvertUtils.java @@ -14,6 +14,7 @@ import com.zfoo.protocol.util.StringUtils; import org.springframework.context.support.ConversionServiceFactoryBean; +import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; import java.lang.reflect.Array; @@ -27,29 +28,36 @@ public abstract class ConvertUtils { private static final TypeDescriptor TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class); - private static final ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean(); + private static ConversionService conversionService = customConvert(Collections.emptySet()); - static { + + public static ConversionService customConvert(Set customConverts) { var converters = new HashSet<>(); - converters.add(new ArrayConverter()); - converters.add(new ListConverter()); - converters.add(new SetConverter()); - converters.add(new JsonToMapConverter()); - converters.add(new JsonToObjectConverter()); - converters.add(new StringToClassConverter()); - converters.add(new StringToDateConverter()); + // default convert + converters.add(ArrayConverter.INSTANCE); + converters.add(ListConverter.INSTANCE); + converters.add(SetConverter.INSTANCE); + converters.add(JsonToMapConverter.INSTANCE); + converters.add(JsonToObjectConverter.INSTANCE); + converters.add(StringToClassConverter.INSTANCE); + converters.add(StringToDateConverter.INSTANCE); + // custom convert + converters.addAll(customConverts); + + ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean(); conversionServiceFactoryBean.setConverters(converters); conversionServiceFactoryBean.afterPropertiesSet(); + return conversionServiceFactoryBean.getObject(); } public static T convert(String content, Class targetType) { - return conversionServiceFactoryBean.getObject().convert(content, targetType); + return conversionService.convert(content, targetType); } public static Object convertField(String content, Field field) { var targetType = new TypeDescriptor(field); - return conversionServiceFactoryBean.getObject().convert(content, TYPE_DESCRIPTOR, targetType); + return conversionService.convert(content, TYPE_DESCRIPTOR, targetType); } public static Object convertToArray(String content, Class componentType) { @@ -75,7 +83,7 @@ public static List convertToList(String content, Class genericType) { var splits = content.split(StringUtils.COMMA_REGEX); var list = new ArrayList(); for (var split : splits) { - var value = ConvertUtils.convert(StringUtils.trim(split), genericType); + var value = convert(StringUtils.trim(split), genericType); list.add(value); } return Collections.unmodifiableList(list); diff --git a/storage/src/main/java/com/zfoo/storage/convert/JsonToMapConverter.java b/storage/src/main/java/com/zfoo/storage/convert/JsonToMapConverter.java index 2969b3e76..927689e06 100644 --- a/storage/src/main/java/com/zfoo/storage/convert/JsonToMapConverter.java +++ b/storage/src/main/java/com/zfoo/storage/convert/JsonToMapConverter.java @@ -26,6 +26,12 @@ * @author godotg */ public class JsonToMapConverter implements ConditionalGenericConverter { + + public static final JsonToMapConverter INSTANCE = new JsonToMapConverter(); + + private JsonToMapConverter() { + } + @Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { return sourceType.getType() == String.class && targetType.isMap(); diff --git a/storage/src/main/java/com/zfoo/storage/convert/JsonToObjectConverter.java b/storage/src/main/java/com/zfoo/storage/convert/JsonToObjectConverter.java index 673f7a5ab..cbf989cc2 100644 --- a/storage/src/main/java/com/zfoo/storage/convert/JsonToObjectConverter.java +++ b/storage/src/main/java/com/zfoo/storage/convert/JsonToObjectConverter.java @@ -29,6 +29,10 @@ */ public class JsonToObjectConverter implements ConditionalGenericConverter { + public static final JsonToObjectConverter INSTANCE = new JsonToObjectConverter(); + + private JsonToObjectConverter() { + } @Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { diff --git a/storage/src/main/java/com/zfoo/storage/convert/ListConverter.java b/storage/src/main/java/com/zfoo/storage/convert/ListConverter.java index c04037da8..edcffdc79 100644 --- a/storage/src/main/java/com/zfoo/storage/convert/ListConverter.java +++ b/storage/src/main/java/com/zfoo/storage/convert/ListConverter.java @@ -30,6 +30,10 @@ */ public class ListConverter implements ConditionalGenericConverter { + public static final ListConverter INSTANCE = new ListConverter(); + + private ListConverter() { + } @Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { diff --git a/storage/src/main/java/com/zfoo/storage/convert/SetConverter.java b/storage/src/main/java/com/zfoo/storage/convert/SetConverter.java index 36660a896..823ad09e0 100644 --- a/storage/src/main/java/com/zfoo/storage/convert/SetConverter.java +++ b/storage/src/main/java/com/zfoo/storage/convert/SetConverter.java @@ -29,6 +29,10 @@ */ public class SetConverter implements ConditionalGenericConverter { + public static final SetConverter INSTANCE = new SetConverter(); + + private SetConverter() { + } @Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { diff --git a/storage/src/main/java/com/zfoo/storage/convert/StringToClassConverter.java b/storage/src/main/java/com/zfoo/storage/convert/StringToClassConverter.java index 71295852e..acf405bed 100644 --- a/storage/src/main/java/com/zfoo/storage/convert/StringToClassConverter.java +++ b/storage/src/main/java/com/zfoo/storage/convert/StringToClassConverter.java @@ -22,6 +22,10 @@ */ public class StringToClassConverter implements Converter> { + public static final StringToClassConverter INSTANCE = new StringToClassConverter(); + + private StringToClassConverter() { + } @Override public Class convert(String source) { diff --git a/storage/src/main/java/com/zfoo/storage/convert/StringToDateConverter.java b/storage/src/main/java/com/zfoo/storage/convert/StringToDateConverter.java index fa921fac3..d6a96997b 100644 --- a/storage/src/main/java/com/zfoo/storage/convert/StringToDateConverter.java +++ b/storage/src/main/java/com/zfoo/storage/convert/StringToDateConverter.java @@ -25,6 +25,10 @@ */ public class StringToDateConverter implements Converter { + public static final StringToDateConverter INSTANCE = new StringToDateConverter(); + + private StringToDateConverter() { + } @Override public Date convert(String source) { diff --git a/storage/src/test/java/com/zfoo/storage/convert/ConversionTest.java b/storage/src/test/java/com/zfoo/storage/convert/ConversionTest.java index 753d347d8..68aab6747 100644 --- a/storage/src/test/java/com/zfoo/storage/convert/ConversionTest.java +++ b/storage/src/test/java/com/zfoo/storage/convert/ConversionTest.java @@ -31,10 +31,10 @@ public class ConversionTest { private static final ConversionServiceFactoryBean csfb = new ConversionServiceFactoryBean(); private static final Set converters = new HashSet<>(); - private static final StringToDateConverter std = new StringToDateConverter(); - private static final StringToClassConverter stcc = new StringToClassConverter(); - private static final JsonToMapConverter jtmc = new JsonToMapConverter(); - private static final ArrayConverter jtac = new ArrayConverter(); + private static final StringToDateConverter std = StringToDateConverter.INSTANCE; + private static final StringToClassConverter stcc = StringToClassConverter.INSTANCE; + private static final JsonToMapConverter jtmc = JsonToMapConverter.INSTANCE; + private static final ArrayConverter jtac = ArrayConverter.INSTANCE; static { converters.add(std);