Improve Coin range check to cope with Long.MIN_VALUE correctly

This commit is contained in:
Peter Dettman 2014-10-17 12:53:59 +07:00 committed by Andreas Schildbach
parent f40785fbdf
commit bb368c9543
2 changed files with 9 additions and 2 deletions

View File

@ -82,9 +82,11 @@ public final class Coin implements Monetary, Comparable<Coin>, Serializable {
*/
public final long value;
private final long MAX_SATOSHIS = COIN_VALUE * NetworkParameters.MAX_COINS;
private Coin(final long satoshis) {
checkArgument(Math.abs(satoshis) <= COIN_VALUE * NetworkParameters.MAX_COINS,
"%s satoshis exceeds maximum possible quantity of Bitcoin.", satoshis);
checkArgument(-MAX_SATOSHIS <= satoshis && satoshis <= MAX_SATOSHIS,
"%s satoshis exceeds maximum possible quantity of Bitcoin.", satoshis);
this.value = satoshis;
}

View File

@ -62,6 +62,11 @@ public class CoinTest {
} catch (IllegalArgumentException e) {
}
try {
valueOf(Long.MIN_VALUE);
fail();
} catch (IllegalArgumentException e) {}
try {
valueOf(1, -1);
fail();