Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +49 to +51

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

}

private byte code;

LanguageCode(byte code) {
this.code = code;
}

public static LanguageCode valueOf(byte code) {
for (LanguageCode languageCode : LanguageCode.values()) {
if (languageCode.getCode() == code) {
return languageCode;
}
}
return null;
int idx = code & 0xFF;
return idx < BY_CODE.length ? BY_CODE[idx] : null;
}

public byte getCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ public enum SerializeType {
JSON((byte) 0),
ROCKETMQ((byte) 1);

private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


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;
Comment on lines +33 to +34

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

}

public byte getCode() {
Expand Down
Loading