增加 ConvertLoader
This commit is contained in:
@@ -16,7 +16,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
import java.util.concurrent.atomic.*;
|
import java.util.concurrent.atomic.*;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.*;
|
import java.util.stream.*;
|
||||||
|
import org.redkale.convert.bson.BsonConvert;
|
||||||
import org.redkale.convert.ext.*;
|
import org.redkale.convert.ext.*;
|
||||||
|
import org.redkale.convert.json.JsonConvert;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,6 +34,10 @@ import org.redkale.util.*;
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
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;
|
private final ConvertFactory parent;
|
||||||
|
|
||||||
protected Convert<R, W> convert;
|
protected Convert<R, W> convert;
|
||||||
@@ -139,7 +145,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void convertTo(W out, java.sql.Date value) {
|
public void convertTo(W out, java.sql.Date value) {
|
||||||
out.writeSmallString(value == null ? null : value.toString());
|
out.writeSmallString(value == null ? null : value.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -188,6 +194,25 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
return this.parent;
|
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 ConvertType getConvertType();
|
||||||
|
|
||||||
public abstract boolean isReversible(); //是否可逆的
|
public abstract boolean isReversible(); //是否可逆的
|
||||||
|
|||||||
23
src/org/redkale/convert/ConvertLoader.java
Normal file
23
src/org/redkale/convert/ConvertLoader.java
Normal 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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user