From e7b8508491ead8c87ecd88d9fef6457bfb2e2d09 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Tue, 12 Jul 2022 09:32:13 -0700 Subject: [PATCH] BitcoinNetwork: build name lists and map at init time * In constructor create a list of all names * Build static map of names to BitcoinNetwork at class initialization time --- .../org/bitcoinj/base/BitcoinNetwork.java | 53 ++++++++++++------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/base/BitcoinNetwork.java b/core/src/main/java/org/bitcoinj/base/BitcoinNetwork.java index 53e7861c9..fdd571f68 100644 --- a/core/src/main/java/org/bitcoinj/base/BitcoinNetwork.java +++ b/core/src/main/java/org/bitcoinj/base/BitcoinNetwork.java @@ -16,9 +16,15 @@ package org.bitcoinj.base; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Optional; +import java.util.stream.Stream; import static org.bitcoinj.base.Coin.COIN; @@ -64,11 +70,16 @@ public enum BitcoinNetwork implements Network { public static final String ID_UNITTESTNET = "org.bitcoinj.unittest"; private final String id; - private final String[] alternateNames; + + // All supported names for this BitcoinNetwork + private final List allNames; + + // Maps from names and alternateNames to BitcoinNetwork + private static final Map stringToEnum = mergedNameMap(); BitcoinNetwork(String networkId, String... alternateNames) { this.id = networkId; - this.alternateNames = alternateNames; + this.allNames = combine(this.toString(), alternateNames); } /** @@ -117,22 +128,7 @@ public enum BitcoinNetwork implements Network { * @return An {@code Optional} containing the matching enum or empty */ public static Optional fromString(String nameString) { - final Optional canonicalName = findCanonicalName(nameString); - return canonicalName.isPresent() - ? canonicalName - : findAlternateName(nameString); - } - - private static Optional findCanonicalName(String nameString) { - return Arrays.stream(values()) - .filter(n -> n.toString().equals(nameString)) - .findFirst(); - } - - private static Optional findAlternateName(String nameString) { - return Arrays.stream(values()) - .filter(n -> Arrays.stream(n.alternateNames).anyMatch(a -> a.equals(nameString))) - .findFirst(); + return Optional.ofNullable(stringToEnum.get(nameString)); } /** @@ -147,4 +143,25 @@ public enum BitcoinNetwork implements Network { .filter(n -> n.id.equals(idString)) .findFirst(); } + + // Create a Map that maps name Strings to networks for all instances + private static Map mergedNameMap() { + return Stream.of(values()) + .collect(HashMap::new, // Supply HashMaps as mutable containers + BitcoinNetwork::accumulateNames, // Accumulate one network into hashmap + Map::putAll); // Combine two containers + } + + // Add allNames for this Network as keys to a map that can be used to find it + private static void accumulateNames(Map map, BitcoinNetwork net) { + net.allNames.forEach(name -> map.put(name, net)); + } + + // Combine a String and an array of String and return as an unmodifiable list + private static List combine(String canonical, String[] alternateNames) { + List temp = new ArrayList<>(); + temp.add(canonical); + temp.addAll(Arrays.asList(alternateNames)); + return Collections.unmodifiableList(temp); + } }