From 767e3935af601fd5fb24823b762a5f5797d3c8a8 Mon Sep 17 00:00:00 2001 From: Ben McIlwain Date: Tue, 15 Mar 2022 13:00:54 -0400 Subject: [PATCH] Make DigestType.fromWireValue() more performant (#1555) * Make DigestType.fromWireValue() more performant --- .../main/java/google/registry/tools/DigestType.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/google/registry/tools/DigestType.java b/core/src/main/java/google/registry/tools/DigestType.java index 9fab50054..8723868fd 100644 --- a/core/src/main/java/google/registry/tools/DigestType.java +++ b/core/src/main/java/google/registry/tools/DigestType.java @@ -14,6 +14,9 @@ package google.registry.tools; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import java.util.Arrays; import java.util.Optional; /** @@ -43,14 +46,12 @@ public enum DigestType { this.bytes = bytes; } + private static final ImmutableMap WIRE_VALUE_TO_DIGEST_TYPE = + Maps.uniqueIndex(Arrays.stream(DigestType.values()).iterator(), DigestType::getWireValue); + /** Fetches a DigestType enumeration constant by its IANA assigned value. */ public static Optional fromWireValue(int wireValue) { - for (DigestType alg : DigestType.values()) { - if (alg.getWireValue() == wireValue) { - return Optional.of(alg); - } - } - return Optional.empty(); + return Optional.ofNullable(WIRE_VALUE_TO_DIGEST_TYPE.get(wireValue)); } /** Fetches a value in the range [0, 255] that encodes this DS digest type on the wire. */