From 8fc09f580851aed89ac83a344b231a2c6d5cf741 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Mon, 2 Apr 2012 17:36:47 +0200 Subject: [PATCH] Make network parameters to BitcoinURI optional. --- .../com/google/bitcoin/uri/BitcoinURI.java | 43 +++++++++---------- .../google/bitcoin/uri/BitcoinURITest.java | 20 --------- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/uri/BitcoinURI.java b/core/src/main/java/com/google/bitcoin/uri/BitcoinURI.java index 65fd51a5f..aa302b126 100644 --- a/core/src/main/java/com/google/bitcoin/uri/BitcoinURI.java +++ b/core/src/main/java/com/google/bitcoin/uri/BitcoinURI.java @@ -22,6 +22,7 @@ import com.google.bitcoin.core.Address; import com.google.bitcoin.core.AddressFormatException; import com.google.bitcoin.core.NetworkParameters; import com.google.bitcoin.core.Utils; +import com.google.common.base.Preconditions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -100,25 +101,25 @@ public class BitcoinURI { private final Map parameterMap = new LinkedHashMap(); /** - * @param params - * The BitCoinJ network parameters that determine which network - * the URI is from - * @param input - * The raw URI data to be parsed (see class comments for accepted - * formats) - * @throws BitcoinURIParseException - * If the input fails Bitcoin URI syntax and semantic checks + * Constructs a new BitcoinURI from the given string. Can be for any network. + * + * @param uri The raw URI data to be parsed (see class comments for accepted formats) + * @throws BitcoinURIParseException if the URI is not syntactically or semantically valid. + */ + public BitcoinURI(String uri) { + this(null, uri); + } + + /** + * @param params The network parameters that determine which network the URI is from, or null if you don't have + * any expectation about what network the URI is for and wish to check yourself. + * @param input The raw URI data to be parsed (see class comments for accepted formats) + * @throws BitcoinURIParseException If the input fails Bitcoin URI syntax and semantic checks. */ public BitcoinURI(NetworkParameters params, String input) { // Basic validation - if (params == null) { - throw new IllegalArgumentException("NetworkParameters cannot be null"); - } - if (input == null) { - throw new IllegalArgumentException("Input cannot be null"); - } - - log.debug("Attempting to parse '{}' for {}", input, params.getId()); + Preconditions.checkNotNull(input); + log.debug("Attempting to parse '{}' for {}", input, params == null ? "any" : params.getId()); // URI validation if (!input.startsWith(BITCOIN_SCHEME)) { @@ -161,7 +162,7 @@ public class BitcoinURI { // Split into '=' tokens. nameValuePairTokens = addressSplitTokens[1].split("&"); } else { - throw new BitcoinURIParseException("Too many question marks in URI '" + input + "'"); + throw new BitcoinURIParseException("Too many question marks in URI '" + uri + "'"); } } @@ -170,11 +171,9 @@ public class BitcoinURI { } /** - * @param params - * The network parameters - * @param nameValuePairTokens - * The tokens representing the name value pairs (assumed to be - * separated by '=' e.g. 'amount=0.2') + * @param params The network parameters or null + * @param nameValuePairTokens The tokens representing the name value pairs (assumed to be + * separated by '=' e.g. 'amount=0.2') */ private void parseParameters(NetworkParameters params, String addressToken, String[] nameValuePairTokens) { // Attempt to parse the addressToken as a Bitcoin address for this network diff --git a/core/src/test/java/com/google/bitcoin/uri/BitcoinURITest.java b/core/src/test/java/com/google/bitcoin/uri/BitcoinURITest.java index f68e5a1c4..7b859275c 100644 --- a/core/src/test/java/com/google/bitcoin/uri/BitcoinURITest.java +++ b/core/src/test/java/com/google/bitcoin/uri/BitcoinURITest.java @@ -110,26 +110,6 @@ public class BitcoinURITest { assertEquals("Unexpected label", 20, testObject.getAddress().getHash160().length); } - /** - * Test missing constructor parameters - */ - @Test - public void testBad_Constructor() { - try { - testObject = new BitcoinURI(null, "blimpcoin:" + PRODNET_GOOD_ADDRESS); - fail("Expecting IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertTrue(e.getMessage().contains("NetworkParameters")); - } - - try { - testObject = new BitcoinURI(NetworkParameters.prodNet(), null); - fail("Expecting IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertTrue(e.getMessage().contains("Input")); - } - } - /** * Test a broken URI (bad scheme) */