代码优化
This commit is contained in:
@@ -19,7 +19,6 @@ import java.util.function.BiFunction;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.*;
|
import java.util.stream.*;
|
||||||
import org.redkale.annotation.ConstructorParameters;
|
|
||||||
import org.redkale.convert.bson.BsonConvert;
|
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.convert.json.JsonConvert;
|
||||||
@@ -43,7 +42,11 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
|
|
||||||
protected Convert<R, W> convert;
|
protected Convert<R, W> convert;
|
||||||
|
|
||||||
// 配置属性集合
|
/**
|
||||||
|
* 配置属性集合
|
||||||
|
* @see org.redkale.convert.Convert#FEATURE_NULLABLE
|
||||||
|
* @see org.redkale.convert.Convert#FEATURE_TINY
|
||||||
|
*/
|
||||||
protected int features;
|
protected int features;
|
||||||
|
|
||||||
private final Encodeable<W, ?> anyEncoder = new AnyEncoder(this);
|
private final Encodeable<W, ?> anyEncoder = new AnyEncoder(this);
|
||||||
@@ -148,13 +151,8 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
this.register(String[].class, StringArraySimpledCoder.instance);
|
this.register(String[].class, StringArraySimpledCoder.instance);
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
this.register(AnyValue.class, Creator.create(AnyValueWriter.class));
|
this.register(AnyValue.class, Creator.create(AnyValueWriter.class));
|
||||||
this.register(HttpCookie.class, new Creator<HttpCookie>() {
|
this.register(
|
||||||
@Override
|
HttpCookie.class, (Object... params) -> new HttpCookie((String) params[0], (String) params[1]));
|
||||||
@ConstructorParameters({"name", "value"})
|
|
||||||
public HttpCookie create(Object... params) {
|
|
||||||
return new HttpCookie((String) params[0], (String) params[1]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
try {
|
try {
|
||||||
Class sqldateClass =
|
Class sqldateClass =
|
||||||
Thread.currentThread().getContextClassLoader().loadClass("java.sql.Date");
|
Thread.currentThread().getContextClassLoader().loadClass("java.sql.Date");
|
||||||
@@ -900,14 +898,14 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
ConvertFactory child = createChild();
|
ConvertFactory child = createChild();
|
||||||
if (encode) {
|
if (encode && encoderList != null) {
|
||||||
for (Encodeable item : encoderList) {
|
for (Encodeable item : encoderList) {
|
||||||
child.register(item.getType(), item);
|
child.register(item.getType(), item);
|
||||||
if (item instanceof ObjectEncoder) {
|
if (item instanceof ObjectEncoder) {
|
||||||
((ObjectEncoder) item).init(child);
|
((ObjectEncoder) item).init(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else if (decoderList != null) {
|
||||||
for (Decodeable item : decoderList) {
|
for (Decodeable item : decoderList) {
|
||||||
child.register(item.getType(), item);
|
child.register(item.getType(), item);
|
||||||
if (item instanceof ObjectDecoder) {
|
if (item instanceof ObjectDecoder) {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
|
|
||||||
private final ThreadLocal<JsonBytesWriter> bytesWriterPool = Utility.withInitialThreadLocal(JsonBytesWriter::new);
|
private final ThreadLocal<JsonBytesWriter> bytesWriterPool = Utility.withInitialThreadLocal(JsonBytesWriter::new);
|
||||||
|
|
||||||
private final Consumer<JsonBytesWriter> offerBytesConsumer = w -> offerJsonBytesWriter(w);
|
private final Consumer<JsonBytesWriter> offerBytesConsumer = this::offerJsonBytesWriter;
|
||||||
|
|
||||||
private final ThreadLocal<JsonReader> readerPool = Utility.withInitialThreadLocal(JsonReader::new);
|
private final ThreadLocal<JsonReader> readerPool = Utility.withInitialThreadLocal(JsonReader::new);
|
||||||
|
|
||||||
|
|||||||
@@ -60,22 +60,27 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public JsonFactory withFeatures(int features) {
|
public JsonFactory withFeatures(int features) {
|
||||||
return super.withFeatures(features);
|
return super.withFeatures(features);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public JsonFactory addFeature(int feature) {
|
public JsonFactory addFeature(int feature) {
|
||||||
return super.addFeature(feature);
|
return super.addFeature(feature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public JsonFactory removeFeature(int feature) {
|
public JsonFactory removeFeature(int feature) {
|
||||||
return super.removeFeature(feature);
|
return super.removeFeature(feature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public JsonFactory withTinyFeature(boolean tiny) {
|
public JsonFactory withTinyFeature(boolean tiny) {
|
||||||
return super.withTinyFeature(tiny);
|
return super.withTinyFeature(tiny);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public JsonFactory withNullableFeature(boolean nullable) {
|
public JsonFactory withNullableFeature(boolean nullable) {
|
||||||
return super.withNullableFeature(nullable);
|
return super.withNullableFeature(nullable);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ public abstract class JsonWriter extends Writer {
|
|||||||
this.features = JsonFactory.root().getFeatures();
|
this.features = JsonFactory.root().getFeatures();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public JsonWriter withFeatures(int features) {
|
public JsonWriter withFeatures(int features) {
|
||||||
return (JsonWriter) super.withFeatures(features);
|
return (JsonWriter) super.withFeatures(features);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import org.redkale.convert.*;
|
|||||||
*/
|
*/
|
||||||
public class ProtobufMapDecoder<K, V> extends MapDecoder<K, V> {
|
public class ProtobufMapDecoder<K, V> extends MapDecoder<K, V> {
|
||||||
|
|
||||||
private final boolean enumtostring;
|
protected final boolean enumtostring;
|
||||||
|
|
||||||
public ProtobufMapDecoder(ConvertFactory factory, Type type) {
|
public ProtobufMapDecoder(ConvertFactory factory, Type type) {
|
||||||
super(factory, type);
|
super(factory, type);
|
||||||
|
|||||||
@@ -10,7 +10,10 @@ import org.redkale.convert.*;
|
|||||||
import org.redkale.util.Attribute;
|
import org.redkale.util.Attribute;
|
||||||
import org.redkale.util.Utility;
|
import org.redkale.util.Utility;
|
||||||
|
|
||||||
/** @author zhangjx */
|
/**
|
||||||
|
* @author zhangjx
|
||||||
|
* @param <T> T
|
||||||
|
*/
|
||||||
public class ProtobufObjectEncoder<T> extends ObjectEncoder<ProtobufWriter, T> {
|
public class ProtobufObjectEncoder<T> extends ObjectEncoder<ProtobufWriter, T> {
|
||||||
|
|
||||||
protected ProtobufObjectEncoder(Type type) {
|
protected ProtobufObjectEncoder(Type type) {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public class ProtobufReader extends Reader {
|
|||||||
protected boolean enumtostring;
|
protected boolean enumtostring;
|
||||||
|
|
||||||
public static ObjectPool<ProtobufReader> createPool(int max) {
|
public static ObjectPool<ProtobufReader> createPool(int max) {
|
||||||
return ObjectPool.createSafePool(max, (Object... params) -> new ProtobufReader(), null, (t) -> t.recycle());
|
return ObjectPool.createSafePool(max, (Object... params) -> new ProtobufReader(), null, t -> t.recycle());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProtobufReader() {}
|
public ProtobufReader() {}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class ProtobufStreamWriter extends ProtobufByteBufferWriter {
|
|||||||
@Override
|
@Override
|
||||||
public void writeTo(final byte ch) {
|
public void writeTo(final byte ch) {
|
||||||
try {
|
try {
|
||||||
out.write((byte) ch);
|
out.write(ch);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ConvertException(e);
|
throw new ConvertException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
|||||||
protected ProtobufWriter parent;
|
protected ProtobufWriter parent;
|
||||||
|
|
||||||
public static ObjectPool<ProtobufWriter> createPool(int max) {
|
public static ObjectPool<ProtobufWriter> createPool(int max) {
|
||||||
return ObjectPool.createSafePool(max, (Object... params) -> new ProtobufWriter(), null, (t) -> t.recycle());
|
return ObjectPool.createSafePool(max, (Object... params) -> new ProtobufWriter(), null, t -> t.recycle());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ProtobufWriter(ProtobufWriter parent, int features) {
|
protected ProtobufWriter(ProtobufWriter parent, int features) {
|
||||||
@@ -48,6 +48,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
|||||||
this.content = bs;
|
this.content = bs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public ProtobufWriter withFeatures(int features) {
|
public ProtobufWriter withFeatures(int features) {
|
||||||
super.withFeatures(features);
|
super.withFeatures(features);
|
||||||
return this;
|
return this;
|
||||||
@@ -131,6 +132,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
|||||||
handler.completed(content, 0, count, callback, this);
|
handler.completed(content, 0, count, callback, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public byte[] toArray() {
|
public byte[] toArray() {
|
||||||
if (count == content.length) {
|
if (count == content.length) {
|
||||||
return content;
|
return content;
|
||||||
|
|||||||
Reference in New Issue
Block a user