/** * Obtains an instance of {@code Money} for the specified currency and amount. * * @param currency the currency the amount is in * @param amount the amount of the currency to represent, as an instance of {@link BigDecimal} * @return the currency amount */ public static Money of(Currency currency, BigDecimal amount) { return new Money(currency, amount); }
/** * Obtains an instance of {@code Money} for the specified currency and amount. * * @param currency the currency the amount is in * @param amount the amount of the currency to represent * @return the currency amount */ public static Money of(Currency currency, double amount) { return new Money(currency, BigDecimal.valueOf(amount)); }
/** * Obtains an instance of {@code Money} for the specified {@link CurrencyAmount}. * * @param currencyAmount the instance of {@link CurrencyAmount} wrapping the currency and amount. * @return the currency amount */ public static Money of(CurrencyAmount currencyAmount) { return new Money(currencyAmount.getCurrency(), BigDecimal.valueOf(currencyAmount.getAmount())); }
/** * Parses the string to produce a {@link Money}. * <p> * This parses the {@code toString} format of '${currency} ${amount}'. * * @param amountStr the amount string * @return the currency amount * @throws IllegalArgumentException if the amount cannot be parsed */ @FromString public static Money parse(String amountStr) { ArgChecker.notNull(amountStr, "amountStr"); List<String> split = Splitter.on(' ').splitToList(amountStr); if (split.size() != 2) { throw new IllegalArgumentException("Unable to parse amount, invalid format: " + amountStr); } try { Currency cur = Currency.parse(split.get(0)); return new Money(cur, new BigDecimal(split.get(1))); } catch (RuntimeException ex) { throw new IllegalArgumentException("Unable to parse amount: " + amountStr, ex); } }