-
Notifications
You must be signed in to change notification settings - Fork 12k
[ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType #10513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
[ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType #10513
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,19 +21,17 @@ public enum SerializeType { | |
| JSON((byte) 0), | ||
| ROCKETMQ((byte) 1); | ||
|
|
||
| private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ}; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The static initializer |
||
|
|
||
| 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above — |
||
| } | ||
|
|
||
| public byte getCode() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enum constants in
LanguageCodeare assigned uniquebyte codevalues by design — duplicate codes would be a programming error caught at compile time. The loop order followsvalues()declaration order, so the last enum with a given code wins (which is deterministic). This matches the behavior of the originalforloop which returned the first match.