/** * Get the deferred flag. Exchange rates can be deferred or real.time. * * @return the deferred flag, or {code null}. */ public RateType getRateType() { return get(RateType.class); }
/** * Ensure additional ConversionContext is passed correctly to SPIs.<p> * Hint: this assertion will require some custom SPIs to be registered and selected for chain inclusion! */ @Test(description = "4.3.3 Ensure additional ConversionQuery data is passed correctly to SPIs.") @SpecAssertion(id = "433-A2", section = "4.3.3") public void testPassingOverConversionContextToSPIs() { ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider("TestRateProvider"); ConversionQuery ctx = ConversionQueryBuilder.of().set(Locale.CANADA).set("Foo", "bar").setBaseCurrency(FOO_UNIT) .setTermCurrency(Monetary.getCurrency("XXX")).build(); ExchangeRate rate = prov.getExchangeRate(ctx); AssertJUnit.assertNotNull("No test rate returned by getExchangeRate(ConversionQuery), " + "probably TestProvider is not correct registered."); AssertJUnit.assertEquals( "Text parameter Locale.class was not correctly passed to ExchangeRateProvider implementation.", "bar", rate.getContext().getText("Foo")); AssertJUnit.assertEquals( "Object parameter Locale.class was not correctly passed to ExchangeRateProvider implementation.", Locale.CANADA, rate.getContext().get(Locale.class)); }
/** * 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)); } }