/** * Returns a new {@code Money}, returning {@code this} if possible. * <p> * This instance is immutable and unaffected by this method. * * @param newInstance the new money to use, not null * @return the new instance, never null */ private Money with(BigMoney newInstance) { if (money.equals(newInstance)) { return this; } return new Money(newInstance); }
/** * Obtains an instance of {@code Money} from an amount in minor units. * <p> * This allows you to create an instance with a specific currency and amount * expressed in terms of the minor unit. * For example, if constructing US Dollars, the input to this method represents cents. * Note that when a currency has zero decimal places, the major and minor units are the same. * For example, {@code ofMinor(USD, 2595)} creates the instance {@code USD 25.95}. * * @param currency the currency, not null * @param amountMinor the amount of money in the minor division of the currency * @return the new instance, never null */ public static Money ofMinor(CurrencyUnit currency, long amountMinor) { return new Money(BigMoney.ofMinor(currency, amountMinor)); }
/** * Obtains an instance of {@code Money} representing zero. * <p> * For example, {@code zero(USD)} creates the instance {@code USD 0.00}. * * @param currency the currency, not null * @return the instance representing zero, never null */ public static Money zero(CurrencyUnit currency) { MoneyUtils.checkNotNull(currency, "Currency must not be null"); BigDecimal bd = BigDecimal.valueOf(0, currency.getDecimalPlaces()); return new Money(BigMoney.of(currency, bd)); }
/** * Outputs the data. * * @param in the input stream * @throws IOException if an error occurs */ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { type = in.readByte(); switch (type) { case BIG_MONEY: { object = readBigMoney(in); return; } case MONEY: { object = new Money(readBigMoney(in)); return; } case CURRENCY_UNIT: { object = readCurrency(in); return; } } throw new StreamCorruptedException("Serialization input has invalid type"); }
/** * Obtains an instance of {@code Money} from a {@code BigDecimal}, rounding as necessary. * <p> * This allows you to create an instance with a specific currency and amount. * If the amount has a scale in excess of the scale of the currency then the excess * fractional digits are rounded using the rounding mode. * * @param currency the currency, not null * @param amount the amount of money, not null * @param roundingMode the rounding mode to use, not null * @return the new instance, never null * @throws ArithmeticException if the rounding fails */ public static Money of(CurrencyUnit currency, BigDecimal amount, RoundingMode roundingMode) { MoneyUtils.checkNotNull(currency, "CurrencyUnit must not be null"); MoneyUtils.checkNotNull(amount, "Amount must not be null"); MoneyUtils.checkNotNull(roundingMode, "RoundingMode must not be null"); amount = amount.setScale(currency.getDecimalPlaces(), roundingMode); return new Money(BigMoney.of(currency, amount)); }
/** * Obtains an instance of {@code Money} from a provider, rounding as necessary. * <p> * This allows you to create an instance from any class that implements the * provider, such as {@code BigMoney}. * The rounding mode is used to adjust the scale to the scale of the currency. * * @param moneyProvider the money to convert, not null * @param roundingMode the rounding mode to use, not null * @return the new instance, never null * @throws ArithmeticException if the rounding fails */ public static Money of(BigMoneyProvider moneyProvider, RoundingMode roundingMode) { MoneyUtils.checkNotNull(moneyProvider, "BigMoneyProvider must not be null"); MoneyUtils.checkNotNull(roundingMode, "RoundingMode must not be null"); return new Money(BigMoney.of(moneyProvider).withCurrencyScale(roundingMode)); }