代码优化

This commit is contained in:
redkale
2024-08-20 18:49:03 +08:00
parent f6d20a6324
commit 21cb0e191e
9 changed files with 26 additions and 17 deletions

View File

@@ -19,7 +19,6 @@ import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.stream.*;
import org.redkale.annotation.ConstructorParameters;
import org.redkale.convert.bson.BsonConvert;
import org.redkale.convert.ext.*;
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;
// 配置属性集合
/**
* 配置属性集合
* @see org.redkale.convert.Convert#FEATURE_NULLABLE
* @see org.redkale.convert.Convert#FEATURE_TINY
*/
protected int features;
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(AnyValue.class, Creator.create(AnyValueWriter.class));
this.register(HttpCookie.class, new Creator<HttpCookie>() {
@Override
@ConstructorParameters({"name", "value"})
public HttpCookie create(Object... params) {
return new HttpCookie((String) params[0], (String) params[1]);
}
});
this.register(
HttpCookie.class, (Object... params) -> new HttpCookie((String) params[0], (String) params[1]));
try {
Class sqldateClass =
Thread.currentThread().getContextClassLoader().loadClass("java.sql.Date");
@@ -900,14 +898,14 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
return this;
}
ConvertFactory child = createChild();
if (encode) {
if (encode && encoderList != null) {
for (Encodeable item : encoderList) {
child.register(item.getType(), item);
if (item instanceof ObjectEncoder) {
((ObjectEncoder) item).init(child);
}
}
} else {
} else if (decoderList != null) {
for (Decodeable item : decoderList) {
child.register(item.getType(), item);
if (item instanceof ObjectDecoder) {

View File

@@ -31,7 +31,7 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
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);

View File

@@ -60,22 +60,27 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
}
}
@Override
public JsonFactory withFeatures(int features) {
return super.withFeatures(features);
}
@Override
public JsonFactory addFeature(int feature) {
return super.addFeature(feature);
}
@Override
public JsonFactory removeFeature(int feature) {
return super.removeFeature(feature);
}
@Override
public JsonFactory withTinyFeature(boolean tiny) {
return super.withTinyFeature(tiny);
}
@Override
public JsonFactory withNullableFeature(boolean nullable) {
return super.withNullableFeature(nullable);
}

View File

@@ -26,6 +26,7 @@ public abstract class JsonWriter extends Writer {
this.features = JsonFactory.root().getFeatures();
}
@Override
public JsonWriter withFeatures(int features) {
return (JsonWriter) super.withFeatures(features);
}

View File

@@ -15,7 +15,7 @@ import org.redkale.convert.*;
*/
public class ProtobufMapDecoder<K, V> extends MapDecoder<K, V> {
private final boolean enumtostring;
protected final boolean enumtostring;
public ProtobufMapDecoder(ConvertFactory factory, Type type) {
super(factory, type);

View File

@@ -10,7 +10,10 @@ import org.redkale.convert.*;
import org.redkale.util.Attribute;
import org.redkale.util.Utility;
/** @author zhangjx */
/**
* @author zhangjx
* @param <T> T
*/
public class ProtobufObjectEncoder<T> extends ObjectEncoder<ProtobufWriter, T> {
protected ProtobufObjectEncoder(Type type) {

View File

@@ -26,7 +26,7 @@ public class ProtobufReader extends Reader {
protected boolean enumtostring;
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() {}

View File

@@ -41,7 +41,7 @@ class ProtobufStreamWriter extends ProtobufByteBufferWriter {
@Override
public void writeTo(final byte ch) {
try {
out.write((byte) ch);
out.write(ch);
} catch (IOException e) {
throw new ConvertException(e);
}

View File

@@ -32,7 +32,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
protected ProtobufWriter parent;
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) {
@@ -48,6 +48,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
this.content = bs;
}
@Override
public ProtobufWriter withFeatures(int features) {
super.withFeatures(features);
return this;
@@ -131,6 +132,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
handler.completed(content, 0, count, callback, this);
}
@Override
public byte[] toArray() {
if (count == content.length) {
return content;