fix(D-01): pre-built map for O(1) ComponentType.fromKey() lookup (RISK-005)
Replace linear values() scan with a static unmodifiable HashMap lookup. While only 3 entries currently exist, this establishes the correct pattern for when more component types are added.
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package com.tiedup.remake.v2.bondage.component;
|
package com.tiedup.remake.v2.bondage.component;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -12,6 +15,16 @@ public enum ComponentType {
|
|||||||
private final String jsonKey;
|
private final String jsonKey;
|
||||||
private final Function<JsonObject, IItemComponent> factory;
|
private final Function<JsonObject, IItemComponent> factory;
|
||||||
|
|
||||||
|
/** Pre-built lookup map for O(1) fromKey() instead of linear scan. */
|
||||||
|
private static final Map<String, ComponentType> BY_KEY;
|
||||||
|
static {
|
||||||
|
Map<String, ComponentType> map = new HashMap<>();
|
||||||
|
for (ComponentType type : values()) {
|
||||||
|
map.put(type.jsonKey, type);
|
||||||
|
}
|
||||||
|
BY_KEY = Collections.unmodifiableMap(map);
|
||||||
|
}
|
||||||
|
|
||||||
ComponentType(
|
ComponentType(
|
||||||
String jsonKey,
|
String jsonKey,
|
||||||
Function<JsonObject, IItemComponent> factory
|
Function<JsonObject, IItemComponent> factory
|
||||||
@@ -30,9 +43,6 @@ public enum ComponentType {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static ComponentType fromKey(String key) {
|
public static ComponentType fromKey(String key) {
|
||||||
for (ComponentType type : values()) {
|
return BY_KEY.get(key);
|
||||||
if (type.jsonKey.equals(key)) return type;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user