From dcc8493e5e680e66cb6dd3310287a001c0604909 Mon Sep 17 00:00:00 2001 From: NotEvil Date: Tue, 14 Apr 2026 02:29:46 +0200 Subject: [PATCH] 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. --- .../v2/bondage/component/ComponentType.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/tiedup/remake/v2/bondage/component/ComponentType.java b/src/main/java/com/tiedup/remake/v2/bondage/component/ComponentType.java index 7a26186..ca16f26 100644 --- a/src/main/java/com/tiedup/remake/v2/bondage/component/ComponentType.java +++ b/src/main/java/com/tiedup/remake/v2/bondage/component/ComponentType.java @@ -1,6 +1,9 @@ package com.tiedup.remake.v2.bondage.component; import com.google.gson.JsonObject; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.function.Function; import org.jetbrains.annotations.Nullable; @@ -12,6 +15,16 @@ public enum ComponentType { private final String jsonKey; private final Function factory; + /** Pre-built lookup map for O(1) fromKey() instead of linear scan. */ + private static final Map BY_KEY; + static { + Map map = new HashMap<>(); + for (ComponentType type : values()) { + map.put(type.jsonKey, type); + } + BY_KEY = Collections.unmodifiableMap(map); + } + ComponentType( String jsonKey, Function factory @@ -30,9 +43,6 @@ public enum ComponentType { @Nullable public static ComponentType fromKey(String key) { - for (ComponentType type : values()) { - if (type.jsonKey.equals(key)) return type; - } - return null; + return BY_KEY.get(key); } }