Rename Coin.toNanoCoin(String) to Coin.parseCoin(String).

This commit is contained in:
Andreas Schildbach 2014-04-26 19:12:12 +02:00 committed by Mike Hearn
parent 3e846901cb
commit 49adaeed16
5 changed files with 55 additions and 55 deletions

View File

@ -62,6 +62,23 @@ public final class Coin implements Comparable<Coin>, Serializable {
return new Coin(satoshis);
}
/**
* Parses an amount expressed in the way humans are used to.<p>
* <p/>
* This takes string in a format understood by {@link BigDecimal#BigDecimal(String)},
* for example "0", "1", "0.10", "1.23E3", "1234.5E-5".
*
* @throws ArithmeticException if you try to specify fractional satoshis, or a value out of range.
*/
public static Coin parseCoin(final String str) {
Coin coin = Coin.valueOf(new BigDecimal(str).movePointRight(8).toBigIntegerExact().longValue());
if (coin.signum() < 0)
throw new ArithmeticException("Negative coins specified");
if (coin.compareTo(NetworkParameters.MAX_MONEY) > 0)
throw new ArithmeticException("Amount larger than the total quantity of Bitcoins possible specified.");
return coin;
}
public Coin add(final Coin value) {
return new Coin(LongMath.checkedAdd(this.value, value.value));
}
@ -108,23 +125,6 @@ public final class Coin implements Comparable<Coin>, Serializable {
return this.value;
}
/**
* Convert an amount expressed in the way humans are used to into nanocoins.<p>
* <p/>
* This takes string in a format understood by {@link BigDecimal#BigDecimal(String)},
* for example "0", "1", "0.10", "1.23E3", "1234.5E-5".
*
* @throws ArithmeticException if you try to specify fractional nanocoins, or nanocoins out of range.
*/
public static Coin toNanoCoins(String coins) {
Coin bigint = Coin.valueOf(new BigDecimal(coins).movePointRight(8).toBigIntegerExact().longValue());
if (bigint.signum() < 0)
throw new ArithmeticException("Negative coins specified");
if (bigint.compareTo(NetworkParameters.MAX_MONEY) > 0)
throw new ArithmeticException("Amount larger than the total quantity of Bitcoins possible specified.");
return bigint;
}
/**
* Convert an amount expressed in the way humans are used to into nanocoins.
*/

View File

@ -208,7 +208,7 @@ public class BitcoinURI {
if (FIELD_AMOUNT.equals(nameToken)) {
// Decode the amount (contains an optional decimal component to 8dp).
try {
Coin amount = Coin.toNanoCoins(valueToken);
Coin amount = Coin.parseCoin(valueToken);
putWithValidation(FIELD_AMOUNT, amount);
} catch (NumberFormatException e) {
throw new OptionalFieldValidationException(String.format("'%s' is not a valid amount", valueToken), e);

View File

@ -26,13 +26,13 @@ import org.junit.Test;
public class CoinTest {
@Test
public void testToNanoCoins() {
public void testParseCoin() {
// String version
assertEquals(CENT, toNanoCoins("0.01"));
assertEquals(CENT, toNanoCoins("1E-2"));
assertEquals(COIN.add(CENT), toNanoCoins("1.01"));
assertEquals(CENT, parseCoin("0.01"));
assertEquals(CENT, parseCoin("1E-2"));
assertEquals(COIN.add(CENT), parseCoin("1.01"));
try {
toNanoCoins("2E-20");
parseCoin("2E-20");
org.junit.Assert.fail("should not have accepted fractional nanocoins");
} catch (ArithmeticException e) {
}
@ -49,7 +49,7 @@ public class CoinTest {
fail();
} catch (IllegalArgumentException e) {}
try {
toNanoCoins("-1");
parseCoin("-1");
fail();
} catch (ArithmeticException e) {}
}
@ -68,29 +68,29 @@ public class CoinTest {
@Test
public void testToPlainString() {
assertEquals("0.0015", Coin.valueOf(150000).toPlainString());
assertEquals("1.23", toNanoCoins("1.23").toPlainString());
assertEquals("1.23", parseCoin("1.23").toPlainString());
assertEquals("0.1", toNanoCoins("0.1").toPlainString());
assertEquals("1.1", toNanoCoins("1.1").toPlainString());
assertEquals("21.12", toNanoCoins("21.12").toPlainString());
assertEquals("321.123", toNanoCoins("321.123").toPlainString());
assertEquals("4321.1234", toNanoCoins("4321.1234").toPlainString());
assertEquals("54321.12345", toNanoCoins("54321.12345").toPlainString());
assertEquals("654321.123456", toNanoCoins("654321.123456").toPlainString());
assertEquals("7654321.1234567", toNanoCoins("7654321.1234567").toPlainString());
assertEquals("0.1", parseCoin("0.1").toPlainString());
assertEquals("1.1", parseCoin("1.1").toPlainString());
assertEquals("21.12", parseCoin("21.12").toPlainString());
assertEquals("321.123", parseCoin("321.123").toPlainString());
assertEquals("4321.1234", parseCoin("4321.1234").toPlainString());
assertEquals("54321.12345", parseCoin("54321.12345").toPlainString());
assertEquals("654321.123456", parseCoin("654321.123456").toPlainString());
assertEquals("7654321.1234567", parseCoin("7654321.1234567").toPlainString());
try {
assertEquals("87654321.12345678", toNanoCoins("87654321.12345678").toPlainString());
assertEquals("87654321.12345678", parseCoin("87654321.12345678").toPlainString());
Assert.fail(); // More than MAX_MONEY
} catch (Exception e) {}
// check there are no trailing zeros
assertEquals("1", toNanoCoins("1.0").toPlainString());
assertEquals("2", toNanoCoins("2.00").toPlainString());
assertEquals("3", toNanoCoins("3.000").toPlainString());
assertEquals("4", toNanoCoins("4.0000").toPlainString());
assertEquals("5", toNanoCoins("5.00000").toPlainString());
assertEquals("6", toNanoCoins("6.000000").toPlainString());
assertEquals("7", toNanoCoins("7.0000000").toPlainString());
assertEquals("8", toNanoCoins("8.00000000").toPlainString());
assertEquals("1", parseCoin("1.0").toPlainString());
assertEquals("2", parseCoin("2.00").toPlainString());
assertEquals("3", parseCoin("3.000").toPlainString());
assertEquals("4", parseCoin("4.0000").toPlainString());
assertEquals("5", parseCoin("5.00000").toPlainString());
assertEquals("6", parseCoin("6.000000").toPlainString());
assertEquals("7", parseCoin("7.0000000").toPlainString());
assertEquals("8", parseCoin("8.00000000").toPlainString());
}
}

View File

@ -25,7 +25,7 @@ import org.junit.Test;
import java.io.UnsupportedEncodingException;
import static com.google.bitcoin.core.Coin.toNanoCoins;
import static com.google.bitcoin.core.Coin.*;
import static org.junit.Assert.*;
public class BitcoinURITest {
@ -38,25 +38,25 @@ public class BitcoinURITest {
Address goodAddress = new Address(MainNetParams.get(), MAINNET_GOOD_ADDRESS);
// simple example
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage", BitcoinURI.convertToBitcoinURI(goodAddress, toNanoCoins("12.34"), "Hello", "AMessage"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello&message=AMessage", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", "AMessage"));
// example with spaces, ampersand and plus
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello%20World&message=Mess%20%26%20age%20%2B%20hope", BitcoinURI.convertToBitcoinURI(goodAddress, toNanoCoins("12.34"), "Hello World", "Mess & age + hope"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello%20World&message=Mess%20%26%20age%20%2B%20hope", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello World", "Mess & age + hope"));
// no amount, label present, message present
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", "glory"));
// amount present, no label, message present
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, toNanoCoins("0.1"), null, "glory"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, toNanoCoins("0.1"), "", "glory"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("0.1"), null, "glory"));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=0.1&message=glory", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("0.1"), "", "glory"));
// amount present, label present, no message
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, toNanoCoins("12.34"), "Hello", null));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, toNanoCoins("12.34"), "Hello", ""));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", null));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=12.34&label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("12.34"), "Hello", ""));
// amount present, no label, no message
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, toNanoCoins("1000"), null, null));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, toNanoCoins("1000"), "", ""));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("1000"), null, null));
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?amount=1000", BitcoinURI.convertToBitcoinURI(goodAddress, parseCoin("1000"), "", ""));
// no amount, label present, no message
assertEquals("bitcoin:" + MAINNET_GOOD_ADDRESS + "?label=Hello", BitcoinURI.convertToBitcoinURI(goodAddress, null, "Hello", null));

View File

@ -66,7 +66,7 @@ import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import static com.google.bitcoin.core.Coin.toNanoCoins;
import static com.google.bitcoin.core.Coin.parseCoin;
/**
* A command line tool for manipulating wallets and working with Bitcoin.
@ -128,7 +128,7 @@ public class WalletTool {
public boolean matchBitcoins(Coin comparison) {
try {
Coin units = toNanoCoins(value);
Coin units = parseCoin(value);
switch (type) {
case LT: return comparison.compareTo(units) < 0;
case GT: return comparison.compareTo(units) > 0;
@ -327,7 +327,7 @@ public class WalletTool {
} else if (options.has(outputFlag)) {
Coin fee = Coin.ZERO;
if (options.has("fee")) {
fee = toNanoCoins((String)options.valueOf("fee"));
fee = parseCoin((String)options.valueOf("fee"));
}
String lockTime = null;
if (options.has("locktime")) {
@ -427,7 +427,7 @@ public class WalletTool {
}
String destination = parts[0];
try {
Coin value = toNanoCoins(parts[1]);
Coin value = parseCoin(parts[1]);
if (destination.startsWith("0")) {
// Treat as a raw public key.
byte[] pubKey = new BigInteger(destination, 16).toByteArray();