wang-jiahua commented on code in PR #10513:
URL: https://github.com/apache/rocketmq/pull/10513#discussion_r3410867825


##########
remoting/src/main/java/org/apache/rocketmq/remoting/protocol/SerializeType.java:
##########
@@ -21,19 +21,17 @@ public enum SerializeType {
     JSON((byte) 0),
     ROCKETMQ((byte) 1);
 
+    private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ};

Review Comment:
   The static initializer `BY_CODE = {JSON, ROCKETMQ}` is explicitly ordered by 
declaration, not by code value. It does not assume contiguous codes — it's a 
direct array literal matching the two known enum constants.



##########
remoting/src/main/java/org/apache/rocketmq/remoting/protocol/SerializeType.java:
##########
@@ -21,19 +21,17 @@ public enum SerializeType {
     JSON((byte) 0),
     ROCKETMQ((byte) 1);
 
+    private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ};
+
     private byte code;
 
     SerializeType(byte code) {
         this.code = code;
     }
 
     public static SerializeType valueOf(byte code) {
-        for (SerializeType serializeType : SerializeType.values()) {
-            if (serializeType.getCode() == code) {
-                return serializeType;
-            }
-        }
-        return null;
+        int idx = code & 0xFF;
+        return idx < BY_CODE.length ? BY_CODE[idx] : null;

Review Comment:
   Same as above — `BY_CODE = {JSON, ROCKETMQ}` is an explicit array literal. 
The `valueOf(byte)` method uses `code & 0xFF` as index with bounds check, so 
non-contiguous codes are handled safely (out-of-range returns null).



##########
remoting/src/main/java/org/apache/rocketmq/remoting/protocol/LanguageCode.java:
##########
@@ -38,19 +38,28 @@ public enum LanguageCode {
     RUST((byte) 12),
     NODE_JS((byte) 13);
 
+    private static final LanguageCode[] BY_CODE;
+    static {
+        LanguageCode[] all = values();
+        int max = 0;
+        for (LanguageCode lc : all) {
+            max = Math.max(max, lc.code & 0xFF);
+        }
+        BY_CODE = new LanguageCode[max + 1];
+        for (LanguageCode lc : all) {
+            BY_CODE[lc.code & 0xFF] = lc;
+        }

Review Comment:
   Enum constants in `LanguageCode` are assigned unique `byte code` values by 
design — duplicate codes would be a programming error caught at compile time. 
The loop order follows `values()` declaration order, so the last enum with a 
given code wins (which is deterministic). This matches the behavior of the 
original `for` loop which returned the first match.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to