From 289a0a0ccd51c60d151926ff6cdc91d42f270051 Mon Sep 17 00:00:00 2001 From: sun Date: Sat, 7 Oct 2023 15:42:23 +0800 Subject: [PATCH] chore[protocol]: rename and delete unused code --- protocol/README.md | 9 +- protocol/README_CN.md | 5 +- .../protocol/registration/EnhanceUtils.java | 34 ++--- .../registration/field/BaseField.java | 2 +- .../serializer/CutDownArraySerializer.java | 116 ------------------ 5 files changed, 25 insertions(+), 141 deletions(-) diff --git a/protocol/README.md b/protocol/README.md index 6cea69e34..93bb5cef3 100644 --- a/protocol/README.md +++ b/protocol/README.md @@ -95,9 +95,8 @@ For complex objects, ZFOO package size 2216, KRYO package size 2528, and Protobu - Default data format support eliminates the need for users to register manually.[参考类定义](src/test/java/com/zfoo/protocol/packet/ComplexObject.java) - - boolean,byte,short,int,long,float,double,char,String - - Boolean,Byte,Short,Integer,Long,Float,Double,Character,If it is null during serialization, a default value of 0 - will be given (the default value of Character is Character.MIN VALUE) + - boolean,byte,short,int,long,float,double,String + - Boolean,Byte,Short,Integer,Long,Float,Double,String,If it is null during serialization, a default value will be given - int[],Integer[],If it is null, it is parsed as an array of length 0 - Native generic List, Set, Map, deserialization return type Hash Set, Array List, Hash Map, and null pointer safe (returns a collection of size 0) @@ -110,8 +109,8 @@ For complex objects, ZFOO package size 2216, KRYO package size 2528, and Protobu - int[][],Arrays above two dimensions, considering that not all languages support multidimensional arrays - List[],Map[],The Java language itself does not support generic class arrays - List,Map,Generics are set in arrays, which looks strange and has few actual uses - - Enumerated classes, considering that many other languages do not support enumerated classes, can be replaced at - the code level with int or string + - char or Character, many other languages do not support char and can be replaced with string + - Enumerated classes, many other languages do not support enumerated classes and can be replaced with int or string - Custom generic class XXX Class, generic classes are prone to performance and parsing problems in many frameworks, and are not supported in all languages - Circular references, although the underlying support circular references, but considering that circular references diff --git a/protocol/README_CN.md b/protocol/README_CN.md index f98265909..62390ce25 100644 --- a/protocol/README_CN.md +++ b/protocol/README_CN.md @@ -80,8 +80,8 @@ cpu: i9900k ### Ⅵ. 数据类型 - 默认的数据格式支持,无需用户手动注册,[参考类定义](src/test/java/com/zfoo/protocol/packet/ComplexObject.java) - - boolean,byte,short,int,long,float,double,char,String - - Boolean,Byte,Short,Integer,Long,Float,Double,Character,序列化的时候如果null,会给个默认值0(Character默认值为Character.MIN_VALUE) + - boolean,byte,short,int,long,float,double,String + - Boolean,Byte,Short,Integer,Long,Float,Double,String 序列化的时候如果null,会给个默认值 - int[],Integer[],如果是null,则解析后的为一个长度为0的数组 - 原生泛型List,Set,Map,反序列化返回类型为HashSet,ArrayList,HashMap,并且空指针安全(返回大小为0的集合) - List,必须指定泛型类,如果发送的是[1,1,null,1],接收到的是[1,1,0,1] @@ -91,6 +91,7 @@ cpu: i9900k - int[][],二维以上数组,考虑到不是所有语言都支持多维数组 - List[],Map[],Java语言本身就没有支持泛型类数组 - List,Map,泛型里面套数组,这种写法看起来比较奇怪,实际使用的地方很少 + - char和Character类型,考虑到很多其他语言不支持char,可以用string在代码层面做替换 - 枚举类,考虑到很多其他语言不支持枚举类,可以用int或者string在代码层面做替换 - 自定义泛型类XXXClass,泛型类在很多框架中都极易出现性能上和解析上的问题,而且并不是所有语言都支持 - 循环引用,虽然底层支持循环引用,但是考虑到循环引用带来语义上难以理解,容易出现错误,所以就屏蔽了 diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java b/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java index 93b2effed..138532c79 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java @@ -39,7 +39,7 @@ public abstract class EnhanceUtils { // 临时变量,是一个基本类型序列化器对应的增强类型序列化器 - private static Map tempEnhanceSerializerMap = new HashMap<>(); + private static Map enhanceSerializerMap = new HashMap<>(); public static String byteBufUtils = ByteBufUtils.class.getSimpleName(); public static String byteBufUtilsWriteBooleanFalse = byteBufUtils + ".writeBoolean($1, false);"; @@ -69,28 +69,28 @@ public abstract class EnhanceUtils { } } - tempEnhanceSerializerMap.put(BooleanSerializer.INSTANCE, new EnhanceBooleanSerializer()); - tempEnhanceSerializerMap.put(ByteSerializer.INSTANCE, new EnhanceByteSerializer()); - tempEnhanceSerializerMap.put(ShortSerializer.INSTANCE, new EnhanceShortSerializer()); - tempEnhanceSerializerMap.put(IntSerializer.INSTANCE, new EnhanceIntSerializer()); - tempEnhanceSerializerMap.put(LongSerializer.INSTANCE, new EnhanceLongSerializer()); - tempEnhanceSerializerMap.put(FloatSerializer.INSTANCE, new EnhanceFloatSerializer()); - tempEnhanceSerializerMap.put(DoubleSerializer.INSTANCE, new EnhanceDoubleSerializer()); - tempEnhanceSerializerMap.put(StringSerializer.INSTANCE, new EnhanceStringSerializer()); - tempEnhanceSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new EnhanceObjectProtocolSerializer()); - tempEnhanceSerializerMap.put(ListSerializer.INSTANCE, new EnhanceListSerializer()); - tempEnhanceSerializerMap.put(SetSerializer.INSTANCE, new EnhanceSetSerializer()); - tempEnhanceSerializerMap.put(MapSerializer.INSTANCE, new EnhanceMapSerializer()); - tempEnhanceSerializerMap.put(ArraySerializer.INSTANCE, new EnhanceArraySerializer()); + enhanceSerializerMap.put(BooleanSerializer.INSTANCE, new EnhanceBooleanSerializer()); + enhanceSerializerMap.put(ByteSerializer.INSTANCE, new EnhanceByteSerializer()); + enhanceSerializerMap.put(ShortSerializer.INSTANCE, new EnhanceShortSerializer()); + enhanceSerializerMap.put(IntSerializer.INSTANCE, new EnhanceIntSerializer()); + enhanceSerializerMap.put(LongSerializer.INSTANCE, new EnhanceLongSerializer()); + enhanceSerializerMap.put(FloatSerializer.INSTANCE, new EnhanceFloatSerializer()); + enhanceSerializerMap.put(DoubleSerializer.INSTANCE, new EnhanceDoubleSerializer()); + enhanceSerializerMap.put(StringSerializer.INSTANCE, new EnhanceStringSerializer()); + enhanceSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new EnhanceObjectProtocolSerializer()); + enhanceSerializerMap.put(ListSerializer.INSTANCE, new EnhanceListSerializer()); + enhanceSerializerMap.put(SetSerializer.INSTANCE, new EnhanceSetSerializer()); + enhanceSerializerMap.put(MapSerializer.INSTANCE, new EnhanceMapSerializer()); + enhanceSerializerMap.put(ArraySerializer.INSTANCE, new EnhanceArraySerializer()); } public static IEnhanceSerializer enhanceSerializer(ISerializer serializer) { - return tempEnhanceSerializerMap.get(serializer); + return enhanceSerializerMap.get(serializer); } public static void clear() { - tempEnhanceSerializerMap.clear(); - tempEnhanceSerializerMap = null; + enhanceSerializerMap.clear(); + enhanceSerializerMap = null; byteBufUtils = null; byteBufUtilsWriteBooleanFalse = null; diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/field/BaseField.java b/protocol/src/main/java/com/zfoo/protocol/registration/field/BaseField.java index 8231bf447..55249c49b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/field/BaseField.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/field/BaseField.java @@ -17,7 +17,7 @@ /** * 一个包里所包含的变量还有这个变量的序列化器 - * 描述boolean,byte,short,int,long,float,double,char,String等基本序列化器 + * 描述boolean,byte,short,int,long,float,double,String等基本序列化器 * * @author godotg */ diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java index 10442c6fb..ef5569ade 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java @@ -450,60 +450,6 @@ public boolean writeObject(StringBuilder builder, String objectStr, Field field, flag = false; } break; - case "char": - switch (language) { - case Enhance: - builder.append(StringUtils.format("{}.writeCharArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); - break; - case GdScript: - case Python: - builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS); - break; - case Lua: - builder.append(StringUtils.format("buffer:writeCharArray({})", objectStr)).append(LS); - break; - case CSharp: - builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); - break; - case Go: - builder.append(StringUtils.format("buffer.WriteCharArray({})", objectStr)).append(LS); - break; - case Cpp: - case JavaScript: - case TypeScript: - builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); - break; - default: - flag = false; - } - break; - case "Character": - switch (language) { - case Enhance: - builder.append(StringUtils.format("{}.writeCharBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); - break; - case GdScript: - case Python: - builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS); - break; - case Lua: - builder.append(StringUtils.format("buffer:writeCharArray({})", objectStr)).append(LS); - break; - case CSharp: - builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); - break; - case Go: - builder.append(StringUtils.format("buffer.WriteCharArray({})", objectStr)).append(LS); - break; - case Cpp: - case JavaScript: - case TypeScript: - builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); - break; - default: - flag = false; - } - break; default: if (arrayField.getArrayElementRegistration() instanceof ObjectProtocolField) { var protocolId = ((ObjectProtocolField) arrayField.getArrayElementRegistration()).getProtocolId(); @@ -1015,68 +961,6 @@ public String readObject(StringBuilder builder, Field field, IFieldRegistration flag = false; } break; - case "char": - switch (language) { - case Enhance: - builder.append(StringUtils.format("{}[] {} = {}.readCharArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); - break; - case GdScript: - builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS); - break; - case Python: - builder.append(StringUtils.format("{} = buffer.readCharArray()", array)).append(LS); - break; - case Lua: - builder.append(StringUtils.format("local {} = buffer:readCharArray()", array)).append(LS); - break; - case CSharp: - builder.append(StringUtils.format("var {} = buffer.ReadCharArray();", array)).append(LS); - break; - case Go: - builder.append(StringUtils.format("var {} = buffer.ReadCharArray()", array)).append(LS); - break; - case Cpp: - builder.append(StringUtils.format("auto {} = buffer.readCharArray();", array)).append(LS); - break; - case JavaScript: - case TypeScript: - builder.append(StringUtils.format("const {} = buffer.readCharArray();", array)).append(LS); - break; - default: - flag = false; - } - break; - case "Character": - switch (language) { - case Enhance: - builder.append(StringUtils.format("{}[] {} = {}.readCharBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); - break; - case GdScript: - builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS); - break; - case Python: - builder.append(StringUtils.format("{} = buffer.readCharArray()", array)).append(LS); - break; - case Lua: - builder.append(StringUtils.format("local {} = buffer:readCharArray()", array)).append(LS); - break; - case CSharp: - builder.append(StringUtils.format("var {} = buffer.ReadCharArray();", array)).append(LS); - break; - case Go: - builder.append(StringUtils.format("var {} = buffer.ReadCharArray()", array)).append(LS); - break; - case Cpp: - builder.append(StringUtils.format("auto {} = buffer.readCharArray();", array)).append(LS); - break; - case JavaScript: - case TypeScript: - builder.append(StringUtils.format("const {} = buffer.readCharArray();", array)).append(LS); - break; - default: - flag = false; - } - break; default: if (arrayField.getArrayElementRegistration() instanceof ObjectProtocolField) { var protocolId = ((ObjectProtocolField) arrayField.getArrayElementRegistration()).getProtocolId();