From 00c22d4a72e281e007379d07edc6e78af260b8fa Mon Sep 17 00:00:00 2001 From: redkale Date: Mon, 30 Sep 2024 19:56:51 +0800 Subject: [PATCH] pb --- .../convert/pb/ProtobufArrayEncoder.java | 6 +- .../redkale/convert/pb/ProtobufCoders.java | 100 +++++++++++------- .../convert/pb/ProtobufCollectionEncoder.java | 4 +- .../redkale/convert/pb/ProtobufFactory.java | 2 +- .../convert/pb/ProtobufMapEncoder.java | 2 +- .../convert/pb/ProtobufPrimitivable.java | 2 +- .../convert/pb/ProtobufStreamEncoder.java | 15 ++- 7 files changed, 83 insertions(+), 48 deletions(-) diff --git a/src/main/java/org/redkale/convert/pb/ProtobufArrayEncoder.java b/src/main/java/org/redkale/convert/pb/ProtobufArrayEncoder.java index 51173a9a0..686264c24 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufArrayEncoder.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufArrayEncoder.java @@ -17,12 +17,12 @@ public class ProtobufArrayEncoder extends ArrayEncoder implements ProtobufEncodeable { protected final boolean componentSimpled; - protected final boolean requireSizeFlag; + protected final boolean componentSizeRequired; public ProtobufArrayEncoder(ProtobufFactory factory, Type type) { super(factory, type); this.componentSimpled = getComponentEncoder() instanceof SimpledCoder; - this.requireSizeFlag = ((ProtobufEncodeable) getComponentEncoder()).requireSize(); + this.componentSizeRequired = !(getComponentEncoder() instanceof ProtobufPrimitivable); } @Override @@ -65,6 +65,6 @@ public class ProtobufArrayEncoder extends ArrayEncoder @Override public boolean requireSize() { - return !componentSimpled; + return componentSizeRequired; } } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufCoders.java b/src/main/java/org/redkale/convert/pb/ProtobufCoders.java index c12af709c..7db7ca022 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufCoders.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufCoders.java @@ -440,6 +440,11 @@ public abstract class ProtobufCoders { public Type getType() { return BigInteger.class; } + + @Override + public final boolean requireSize() { + return true; + } } public static class ProtobufBigDecimalSimpledCoder extends BigDecimalSimpledCoder @@ -479,6 +484,11 @@ public abstract class ProtobufCoders { public Type getType() { return InetAddress.class; } + + @Override + public final boolean requireSize() { + return true; + } } public static class ProtobufInetSocketAddressSimpledCoder @@ -501,6 +511,11 @@ public abstract class ProtobufCoders { public Type getType() { return InetSocketAddress.class; } + + @Override + public final boolean requireSize() { + return true; + } } public static class ProtobufLongAdderSimpledCoder extends LongAdderSimpledCoder @@ -537,11 +552,16 @@ public abstract class ProtobufCoders { public Type getType() { return Uint128.class; } + + @Override + public final boolean requireSize() { + return true; + } } // ------------------------------------- boolean[] ------------------------------------- public static class ProtobufBoolArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufBoolArraySimpledCoder instance = new ProtobufBoolArraySimpledCoder(); @@ -562,7 +582,7 @@ public abstract class ProtobufCoders { } public static class ProtobufByteArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufByteArraySimpledCoder instance = new ProtobufByteArraySimpledCoder(); @@ -583,7 +603,7 @@ public abstract class ProtobufCoders { } public static class ProtobufCharArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufCharArraySimpledCoder instance = new ProtobufCharArraySimpledCoder(); @@ -611,7 +631,7 @@ public abstract class ProtobufCoders { } public static class ProtobufShortArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufShortArraySimpledCoder instance = new ProtobufShortArraySimpledCoder(); @@ -639,7 +659,7 @@ public abstract class ProtobufCoders { } public static class ProtobufIntArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufIntArraySimpledCoder instance = new ProtobufIntArraySimpledCoder(); @@ -667,7 +687,7 @@ public abstract class ProtobufCoders { } public static class ProtobufFloatArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufFloatArraySimpledCoder instance = new ProtobufFloatArraySimpledCoder(); @@ -688,7 +708,7 @@ public abstract class ProtobufCoders { } public static class ProtobufLongArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufLongArraySimpledCoder instance = new ProtobufLongArraySimpledCoder(); @@ -716,7 +736,7 @@ public abstract class ProtobufCoders { } public static class ProtobufDoubleArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufDoubleArraySimpledCoder instance = new ProtobufDoubleArraySimpledCoder(); @@ -738,7 +758,7 @@ public abstract class ProtobufCoders { // ------------------------------------- Boolean[] ------------------------------------- public static class ProtobufBoolArraySimpledCoder2 extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufBoolArraySimpledCoder2 instance = new ProtobufBoolArraySimpledCoder2(); @@ -759,7 +779,7 @@ public abstract class ProtobufCoders { } public static class ProtobufByteArraySimpledCoder2 extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufByteArraySimpledCoder2 instance = new ProtobufByteArraySimpledCoder2(); @@ -780,7 +800,7 @@ public abstract class ProtobufCoders { } public static class ProtobufCharArraySimpledCoder2 extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufCharArraySimpledCoder2 instance = new ProtobufCharArraySimpledCoder2(); @@ -808,7 +828,7 @@ public abstract class ProtobufCoders { } public static class ProtobufShortArraySimpledCoder2 extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufShortArraySimpledCoder2 instance = new ProtobufShortArraySimpledCoder2(); @@ -836,7 +856,7 @@ public abstract class ProtobufCoders { } public static class ProtobufIntArraySimpledCoder2 extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufIntArraySimpledCoder2 instance = new ProtobufIntArraySimpledCoder2(); @@ -864,7 +884,7 @@ public abstract class ProtobufCoders { } public static class ProtobufFloatArraySimpledCoder2 extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufFloatArraySimpledCoder2 instance = new ProtobufFloatArraySimpledCoder2(); @@ -885,7 +905,7 @@ public abstract class ProtobufCoders { } public static class ProtobufLongArraySimpledCoder2 extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufLongArraySimpledCoder2 instance = new ProtobufLongArraySimpledCoder2(); @@ -913,7 +933,7 @@ public abstract class ProtobufCoders { } public static class ProtobufDoubleArraySimpledCoder2 extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufDoubleArraySimpledCoder2 instance = new ProtobufDoubleArraySimpledCoder2(); @@ -935,7 +955,7 @@ public abstract class ProtobufCoders { public static class ProtobufAtomicIntegerArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufAtomicIntegerArraySimpledCoder instance = new ProtobufAtomicIntegerArraySimpledCoder(); @@ -965,7 +985,7 @@ public abstract class ProtobufCoders { public static class ProtobufAtomicLongArraySimpledCoder extends SimpledCoder - implements ProtobufPrimitivable, ProtobufEncodeable { + implements ProtobufEncodeable { public static final ProtobufAtomicLongArraySimpledCoder instance = new ProtobufAtomicLongArraySimpledCoder(); @@ -995,7 +1015,7 @@ public abstract class ProtobufCoders { // ------------------------------------- Collection ------------------------------------- public static class ProtobufBoolCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1021,7 +1041,7 @@ public abstract class ProtobufCoders { public static class ProtobufByteCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1047,7 +1067,7 @@ public abstract class ProtobufCoders { public static class ProtobufCharCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1080,7 +1100,7 @@ public abstract class ProtobufCoders { public static class ProtobufShortCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1113,7 +1133,7 @@ public abstract class ProtobufCoders { public static class ProtobufIntCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1146,7 +1166,7 @@ public abstract class ProtobufCoders { public static class ProtobufFloatCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1172,7 +1192,7 @@ public abstract class ProtobufCoders { public static class ProtobufLongCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1205,7 +1225,7 @@ public abstract class ProtobufCoders { public static class ProtobufDoubleCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1231,7 +1251,7 @@ public abstract class ProtobufCoders { public static class ProtobufAtomicBooleanCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1257,7 +1277,7 @@ public abstract class ProtobufCoders { public static class ProtobufAtomicIntegerCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1290,7 +1310,7 @@ public abstract class ProtobufCoders { public static class ProtobufAtomicLongCollectionSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { private final Creator creator; @@ -1324,7 +1344,7 @@ public abstract class ProtobufCoders { // ------------------------------------- Stream ------------------------------------- public static class ProtobufBoolStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufBoolStreamSimpledCoder instance = new ProtobufBoolStreamSimpledCoder(); @@ -1346,7 +1366,7 @@ public abstract class ProtobufCoders { public static class ProtobufByteStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufByteStreamSimpledCoder instance = new ProtobufByteStreamSimpledCoder(); @@ -1368,7 +1388,7 @@ public abstract class ProtobufCoders { public static class ProtobufCharStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufCharStreamSimpledCoder instance = new ProtobufCharStreamSimpledCoder(); @@ -1397,7 +1417,7 @@ public abstract class ProtobufCoders { public static class ProtobufShortStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufShortStreamSimpledCoder instance = new ProtobufShortStreamSimpledCoder(); @@ -1426,7 +1446,7 @@ public abstract class ProtobufCoders { public static class ProtobufIntStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufIntStreamSimpledCoder instance = new ProtobufIntStreamSimpledCoder(); @@ -1455,7 +1475,7 @@ public abstract class ProtobufCoders { public static class ProtobufFloatStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufFloatStreamSimpledCoder instance = new ProtobufFloatStreamSimpledCoder(); @@ -1477,7 +1497,7 @@ public abstract class ProtobufCoders { public static class ProtobufLongStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufLongStreamSimpledCoder instance = new ProtobufLongStreamSimpledCoder(); @@ -1506,7 +1526,7 @@ public abstract class ProtobufCoders { public static class ProtobufDoubleStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufDoubleStreamSimpledCoder instance = new ProtobufDoubleStreamSimpledCoder(); @@ -1528,7 +1548,7 @@ public abstract class ProtobufCoders { public static class ProtobufAtomicBooleanStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufAtomicBooleanStreamSimpledCoder instance = new ProtobufAtomicBooleanStreamSimpledCoder(); @@ -1551,7 +1571,7 @@ public abstract class ProtobufCoders { public static class ProtobufAtomicIntegerStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufAtomicIntegerStreamSimpledCoder instance = new ProtobufAtomicIntegerStreamSimpledCoder(); @@ -1581,7 +1601,7 @@ public abstract class ProtobufCoders { public static class ProtobufAtomicLongStreamSimpledCoder extends SimpledCoder> - implements ProtobufPrimitivable, ProtobufEncodeable> { + implements ProtobufEncodeable> { public static final ProtobufAtomicLongStreamSimpledCoder instance = new ProtobufAtomicLongStreamSimpledCoder(); diff --git a/src/main/java/org/redkale/convert/pb/ProtobufCollectionEncoder.java b/src/main/java/org/redkale/convert/pb/ProtobufCollectionEncoder.java index a8accb488..364742a03 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufCollectionEncoder.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufCollectionEncoder.java @@ -17,10 +17,12 @@ public class ProtobufCollectionEncoder extends CollectionEncoder> { protected final boolean componentSimpled; + protected final boolean componentSizeRequired; public ProtobufCollectionEncoder(ProtobufFactory factory, Type type) { super(factory, type); this.componentSimpled = getComponentEncoder() instanceof SimpledCoder; + this.componentSizeRequired = !(getComponentEncoder() instanceof ProtobufPrimitivable); } @Override @@ -54,6 +56,6 @@ public class ProtobufCollectionEncoder extends CollectionEncoder extends MapEncoder } @Override - public final boolean requireSize() { + public boolean requireSize() { return true; } } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufPrimitivable.java b/src/main/java/org/redkale/convert/pb/ProtobufPrimitivable.java index a79ea16e1..b646dd37b 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufPrimitivable.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufPrimitivable.java @@ -5,7 +5,7 @@ package org.redkale.convert.pb; /** - * + * 只能用于基本类型, 不能用于如String的其他类型 * @author zhangjx */ public interface ProtobufPrimitivable { diff --git a/src/main/java/org/redkale/convert/pb/ProtobufStreamEncoder.java b/src/main/java/org/redkale/convert/pb/ProtobufStreamEncoder.java index 98ba1cc39..a841e8f78 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufStreamEncoder.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufStreamEncoder.java @@ -13,13 +13,16 @@ import org.redkale.convert.*; * @author zhangjx * @param T */ -public class ProtobufStreamEncoder extends StreamEncoder { +public class ProtobufStreamEncoder extends StreamEncoder + implements ProtobufEncodeable> { protected final boolean componentSimpled; + protected final boolean componentSizeRequired; public ProtobufStreamEncoder(ConvertFactory factory, Type type) { super(factory, type); this.componentSimpled = getComponentEncoder() instanceof SimpledCoder; + this.componentSizeRequired = !(getComponentEncoder() instanceof ProtobufPrimitivable); } @Override @@ -46,4 +49,14 @@ public class ProtobufStreamEncoder extends StreamEncoder { } out.writeArrayE(); } + + @Override + public int computeSize(Stream value) { + return 0; + } + + @Override + public boolean requireSize() { + return componentSizeRequired; + } }