@Override public String queryFrom(MonetaryAmount amount) { return Optional .ofNullable(amount) .map(m -> decimalFormat.format(amount.getNumber().numberValue( BigDecimal.class))).orElse("null"); }
/** * Creates a new instance os {@link FastMoney}. * * @param currency the currency, not null. * @param numberValue the numeric value, not null. */ private FastMoney(NumberValue numberValue, CurrencyUnit currency, boolean allowInternalRounding) { Objects.requireNonNull(currency, "Currency is required."); this.currency = currency; Objects.requireNonNull(numberValue, "Number is required."); this.number = getInternalNumber(numberValue.numberValue(BigDecimal.class), allowInternalRounding); }
@Override public boolean isGreaterThanOrEqualTo(MonetaryAmount amount) { MoneyUtils.checkAmountParameter(amount, currency); return number.stripTrailingZeros() .compareTo(amount.getNumber().numberValue(BigDecimal.class).stripTrailingZeros()) >= 0; }
@Override public boolean isLessThanOrEqualTo(MonetaryAmount amount) { MoneyUtils.checkAmountParameter(amount, this.currency); return number.stripTrailingZeros() .compareTo(amount.getNumber().numberValue(BigDecimal.class).stripTrailingZeros()) <= 0; }
@Override public boolean isGreaterThan(MonetaryAmount amount) { MoneyUtils.checkAmountParameter(amount, this.currency); return number.stripTrailingZeros() .compareTo(amount.getNumber().numberValue(BigDecimal.class).stripTrailingZeros()) > 0; }
@Override public MonetaryAmountFactory setAmount(MonetaryAmount amount) { this.currency = amount.getCurrency(); this.number = amount.getNumber().numberValue(BigDecimal.class); return this; }
@Override public boolean isLessThanOrEqualTo(MonetaryAmount amount) { checkAmountParameter(amount); return getBigDecimal().compareTo(amount.getNumber().numberValue(BigDecimal.class)) <= 0; }
@Override public boolean isGreaterThan(MonetaryAmount amount) { checkAmountParameter(amount); return getBigDecimal().compareTo(amount.getNumber().numberValue(BigDecimal.class)) > 0; }
@Override public boolean isEqualTo(MonetaryAmount amount) { checkAmountParameter(amount); return getBigDecimal().compareTo(amount.getNumber().numberValue(BigDecimal.class)) == 0; }
@Override public Long toNonNullValue(MonetaryAmount value) { if (!currencyUnit.equals(value.getCurrency())) { throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency()); } BigDecimal val = value.getNumber().numberValue(BigDecimal.class); return val.movePointRight(currencyUnit.getDefaultFractionDigits()).longValue(); }
@Override public Long toNonNullValue(MonetaryAmount value) { if (!currencyUnit.equals(value.getCurrency())) { throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency()); } BigDecimal val = value.getNumber().numberValue(BigDecimal.class); return val.movePointRight(currencyUnit.getDefaultFractionDigits()).longValue(); }
@Override public Long toNonNullValue(MonetaryAmount value) { if (!currencyUnit.equals(value.getCurrency())) { throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency()); } BigDecimal val = value.getNumber().numberValue(BigDecimal.class); return val.movePointRight(currencyUnit.getDefaultFractionDigits()).longValue(); }
@Override protected Object[] toConvertedColumns(MonetaryAmount value) { BigDecimal minorVal = value.getNumber().numberValue(BigDecimal.class); minorVal = minorVal.movePointRight(value.getCurrency().getDefaultFractionDigits()); return new Object[] { value.getCurrency(), minorVal.longValue() }; }
@Override public BigDecimal toNonNullValue(MonetaryAmount value) { if (!currencyUnit.equals(value.getCurrency())) { throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency()); } return value.getNumber().numberValue(BigDecimal.class); }
@Override public void serialize(final MonetaryAmount monetaryAmount, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { final BigDecimal value = monetaryAmount.getNumber().numberValue(BigDecimal.class); final String currencyCode = monetaryAmount.getCurrency().getCurrencyCode(); final MoneyRepresentation moneyRepresentation = new MoneyRepresentation(value, currencyCode); jsonGenerator.writeObject(moneyRepresentation); } }
@Override public MonetaryAmount apply(MonetaryAmount value) { return value.getFactory() .setNumber(value.getNumber().numberValue(BigDecimal.class).setScale(1, RoundingMode.DOWN)) .create(); } };
@Override public MonetaryAmount apply(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); CurrencyUnit currency = amount.getCurrency(); int scale = scaleOptional.orElse(currency.getDefaultFractionDigits()); BigDecimal value = amount.getNumber().numberValue(BigDecimal.class).setScale(scale, roundingMode); return amount.getFactory().setNumber(value).create(); }
@Override public RoundedMoney add(MonetaryAmount amount) { MoneyUtils.checkAmountParameter(amount, currency); if (amount.isZero()) { return this; } return new RoundedMoney(number.add(amount.getNumber().numberValue(BigDecimal.class)), currency, rounding).with(rounding); }
@Override public MonetaryAmount apply(MonetaryAmount amount) { RoundedMoney roundedMoney = RoundedMoney.from(Objects.requireNonNull(amount)); BigDecimal numberValue = roundedMoney.getNumber().numberValue(BigDecimal.class); BigDecimal numberRounded = numberValue.round(mathContext); return RoundedMoney.of(numberRounded, roundedMoney.getCurrency(), this); }
@Override public Money subtract(MonetaryAmount subtrahend) { MoneyUtils.checkAmountParameter(subtrahend, this.currency); if (subtrahend.isZero()) { return this; } MathContext mc = MoneyUtils.getMathContext(monetaryContext, RoundingMode.HALF_EVEN); return new Money(this.number.subtract(subtrahend.getNumber().numberValue(BigDecimal.class), mc), getCurrency(), monetaryContext); }