mirror of
https://github.com/apache/rocketmq.git
synced 2026-08-28 20:09:14 +08:00
[ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType (#10513)
Co-authored-by: wangjiahua.wjh <wangjiahua.wjh@alibaba-inc.com>
This commit is contained in:
@@ -38,6 +38,19 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
private byte code;
|
||||
|
||||
LanguageCode(byte code) {
|
||||
@@ -45,12 +58,8 @@ public enum LanguageCode {
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
@@ -21,6 +21,8 @@ public enum SerializeType {
|
||||
JSON((byte) 0),
|
||||
ROCKETMQ((byte) 1);
|
||||
|
||||
private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ};
|
||||
|
||||
private byte code;
|
||||
|
||||
SerializeType(byte code) {
|
||||
@@ -28,12 +30,8 @@ public enum SerializeType {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public byte getCode() {
|
||||
|
||||
Reference in New Issue
Block a user