Java Code Examples for javax.money.MonetaryAmount#getCurrency()

The following examples show how to use javax.money.MonetaryAmount#getCurrency() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCurrencyConversion.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
/**
 * Method that converts the source {@link MonetaryAmount} to an
 * {@link MonetaryAmount} based on the {@link ExchangeRate} of this
 * conversion.
 * @param amount The source amount
 * @return The converted amount, never null.
 * @throws CurrencyConversionException if conversion failed, or the required data is not available.
 * @see #getExchangeRate(MonetaryAmount)
 */
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
    if (termCurrency.equals(Objects.requireNonNull(amount).getCurrency())) {
        return amount;
    }
    ExchangeRate rate = getExchangeRate(amount);
    if (Objects.isNull(rate) || !amount.getCurrency().equals(rate.getBaseCurrency())) {
        throw new CurrencyConversionException(amount.getCurrency(),
                this.termCurrency, null);
    }

    NumberValue factor = rate.getFactor();
    factor = roundFactor(amount, factor);

    Integer scale = rate.getContext().get(KEY_SCALE, Integer.class);
    if(Objects.isNull(scale) || scale < 0) {
    	return amount.multiply(factor).getFactory().setCurrency(rate.getCurrency()).create();
    } else {
    	return amount.multiply(factor).getFactory().setCurrency(rate.getCurrency()).create().with(MonetaryOperators.rounding(scale));
    }
}
 
Example 2
Source File: MonetaryAmountSerializer.java    From jackson-datatype-money with MIT License 6 votes vote down vote up
@Override
public void serialize(final MonetaryAmount value, final JsonGenerator json, final SerializerProvider provider)
        throws IOException {

    final CurrencyUnit currency = value.getCurrency();
    @Nullable final String formatted = format(value, provider);

    json.writeStartObject();
    {
        json.writeObjectField(names.getAmount(), writer.write(value));
        json.writeObjectField(names.getCurrency(), currency);

        if (formatted != null) {
            json.writeStringField(names.getFormatted(), formatted);
        }
    }
    json.writeEndObject();
}
 
Example 3
Source File: LongColumnFastMoneyMajorMapper.java    From jadira with Apache License 2.0 5 votes vote down vote up
@Override
public Long toNonNullValue(MonetaryAmount value) {
	if (!currencyUnit.equals(value.getCurrency())) {
		throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency());
	}
    return MonetaryQueries.extractMajorPart().queryFrom(value).longValue();
}
 
Example 4
Source File: LongColumnFastMoneyMinorMapper.java    From jadira with Apache License 2.0 5 votes vote down vote up
@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();
  }
 
Example 5
Source File: BigDecimalColumnMoneyMapper.java    From jadira with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: LongColumnMoneyMinorMapper.java    From jadira with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 7
Source File: LongColumnMoneyMajorMapper.java    From jadira with Apache License 2.0 5 votes vote down vote up
@Override
public Long toNonNullValue(MonetaryAmount value) {
	if (!currencyUnit.equals(value.getCurrency())) {
		throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency());
	}
    return MonetaryQueries.extractMajorPart().queryFrom(value).longValue();
}
 
Example 8
Source File: BigDecimalColumnFastMoneyMapper.java    From jadira with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: PersistentMoneyMinorAmountAndCurrency.java    From jadira with Apache License 2.0 5 votes vote down vote up
@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() };
}
 
Example 10
Source File: RoundingMonetaryAmountOperator.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 11
Source File: BalloonLoanPayment.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Override
public MonetaryAmount apply(MonetaryAmount amountPV) {
    if(!balloonAmount.getCurrency().equals(amountPV.getCurrency())){
        throw new MonetaryException("Currency mismatch: " + balloonAmount.getCurrency() +
                " <> "+amountPV.getCurrency());
    }
    return calculate(amountPV, balloonAmount, rateAndPeriods);
}
 
Example 12
Source File: PersistentFastMoneyMinorAmountAndCurrency.java    From jadira with Apache License 2.0 5 votes vote down vote up
@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() };
}
 
Example 13
Source File: PersistentFastMoneyMajorAmountAndCurrency.java    From jadira with Apache License 2.0 4 votes vote down vote up
@Override
protected Object[] toConvertedColumns(MonetaryAmount value) {

    return new Object[] { value.getCurrency(), MonetaryQueries.extractMajorPart().queryFrom(value).longValue() };
}
 
Example 14
Source File: PersistentMoneyMajorAmountAndCurrency.java    From jadira with Apache License 2.0 4 votes vote down vote up
@Override
protected Object[] toConvertedColumns(MonetaryAmount value) {

    return new Object[] { value.getCurrency(), MonetaryQueries.extractMajorPart().queryFrom(value).longValue() };
}
 
Example 15
Source File: PersistentFastMoneyAmountAndCurrency.java    From jadira with Apache License 2.0 4 votes vote down vote up
@Override
protected Object[] toConvertedColumns(MonetaryAmount value) {

    return new Object[] { value.getCurrency(), value.getNumber().numberValue(BigDecimal.class) };
}
 
Example 16
Source File: PersistentMoneyAmountAndCurrency.java    From jadira with Apache License 2.0 4 votes vote down vote up
@Override
protected Object[] toConvertedColumns(MonetaryAmount value) {

    return new Object[] { value.getCurrency(), value.getNumber().numberValue(BigDecimal.class) };
}
 
Example 17
Source File: ConvertMinorPartQuery.java    From jsr354-ri with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the amount in minor units as a {@code long}.
 * <p>
 * This returns the monetary amount in terms of the minor units of the
 * currency, truncating the amount if necessary. For example, 'EUR 2.35'
 * will return 235, and 'BHD -1.345' will return -1345.
 * <p>
 * This method matches the API of {@link java.math.BigDecimal}.
 *
 * @return the minor units part of the amount
 * @throws ArithmeticException
 *             if the amount is too large for a {@code long}
 */
@Override
public Long queryFrom(MonetaryAmount amount) {
	Objects.requireNonNull(amount, "Amount required.");
	BigDecimal number = amount.getNumber().numberValue(BigDecimal.class);
	CurrencyUnit cur = amount.getCurrency();
	int scale = cur.getDefaultFractionDigits();
	if(scale<0){
		scale = 0;
	}
	number = number.setScale(scale, RoundingMode.DOWN);
	return number.movePointRight(number.scale()).longValueExact();
}
 
Example 18
Source File: MoneyUtils.java    From jsr354-ri with Apache License 2.0 3 votes vote down vote up
/**
 * Method to check if a currency is compatible with this amount instance.
 *
 * @param amount       The monetary amount to be compared to, never null.
 * @param currencyUnit the currency unit to compare, never null.
 * @throws MonetaryException If the amount is null, or the amount's {@link CurrencyUnit} is not
 *                           compatible, meaning has a different value of
 *                           {@link CurrencyUnit#getCurrencyCode()}).
 */
public static void checkAmountParameter(MonetaryAmount amount, CurrencyUnit currencyUnit) {
    requireNonNull(amount, "Amount must not be null.");
    final CurrencyUnit amountCurrency = amount.getCurrency();
    if (!currencyUnit.getCurrencyCode().equals(amountCurrency.getCurrencyCode())) {
        throw new MonetaryException("Currency mismatch: " + currencyUnit + '/' + amountCurrency);
    }
}