增加 ConvertLoader

This commit is contained in:
Redkale
2020-09-08 09:51:28 +08:00
parent 676285e20b
commit 509524144d
2 changed files with 49 additions and 1 deletions

View File

@@ -16,7 +16,9 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.*;
import java.util.regex.Pattern;
import java.util.stream.*;
import org.redkale.convert.bson.BsonConvert;
import org.redkale.convert.ext.*;
import org.redkale.convert.json.JsonConvert;
import org.redkale.util.*;
/**
@@ -32,6 +34,10 @@ import org.redkale.util.*;
@SuppressWarnings("unchecked")
public abstract class ConvertFactory<R extends Reader, W extends Writer> {
private static final AtomicBoolean loaderInited = new AtomicBoolean();
private static Convert defProtobufConvert;
private final ConvertFactory parent;
protected Convert<R, W> convert;
@@ -139,7 +145,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
@Override
public void convertTo(W out, java.sql.Date value) {
out.writeSmallString(value == null ? null : value.toString());
out.writeSmallString(value == null ? null : value.toString());
}
@Override
@@ -188,6 +194,25 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
return this.parent;
}
public static Convert findConvert(ConvertType type) {
if (type == ConvertType.JSON) return JsonConvert.root();
if (type == ConvertType.BSON) return BsonConvert.root();
if (loaderInited.get()) {
if (type == ConvertType.PROTOBUF) return defProtobufConvert;
}
synchronized (loaderInited) {
if (!loaderInited.get()) {
Iterator<ConvertLoader> it = ServiceLoader.load(ConvertLoader.class).iterator();
while (it.hasNext()) {
ConvertLoader cl = it.next();
if (cl.type() == ConvertType.PROTOBUF) defProtobufConvert = cl.convert();
}
loaderInited.set(true);
}
}
return null;
}
public abstract ConvertType getConvertType();
public abstract boolean isReversible(); //是否可逆的

View File

@@ -0,0 +1,23 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.convert;
/**
* Convert的扩展实现类加载器
*
*
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.2.0
*/
public interface ConvertLoader {
public ConvertType type();
public Convert convert();
}