Java Code Examples for javax.money.convert.ConversionQuery#getCurrency()

The following examples show how to use javax.money.convert.ConversionQuery#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: CompoundRateProvider.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Override
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
    for (ExchangeRateProvider prov : this.providers) {
        try {
            if (prov.isAvailable(conversionQuery)) {
                ExchangeRate rate = prov.getExchangeRate(conversionQuery);
                if (Objects.nonNull(rate)) {
                    return rate;
                }
            }
        } catch (Exception e) {
            throw new CurrencyConversionException(conversionQuery.getBaseCurrency(), conversionQuery.getCurrency(), null,
                "Rate Provider did not return data though at check before data was flagged as available," +
                    " provider=" + prov.getContext().getProviderName() + ", query=" + conversionQuery, e);
        }
    }
    throw new CurrencyConversionException(conversionQuery.getBaseCurrency(), conversionQuery.getCurrency(), null,
            "All delegate providers failed to deliver rate, providers=" + this.providers +
                    ", query=" + conversionQuery);
}
 
Example 2
Source File: USFederalReserveRateProvider.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
private ExchangeRate createExchangeRate(ConversionQuery query, ExchangeRateBuilder builder,
    ExchangeRate sourceRate, ExchangeRate target) {

    if (areBothBaseCurrencies(query)) {
        builder.setFactor(DefaultNumberValue.ONE);
        return builder.build();
    } else if (BASE_CURRENCY_CODE.equals(query.getCurrency().getCurrencyCode())) {
        if (Objects.isNull(sourceRate)) {
            return null;
        }
        return reverse(sourceRate);
    } else if (BASE_CURRENCY_CODE.equals(query.getBaseCurrency().getCurrencyCode())) {
        return target;
    } else {

        ExchangeRate rate1 =
            getExchangeRate(query.toBuilder().setTermCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE)).build());
        ExchangeRate rate2 =
            getExchangeRate(query.toBuilder().setBaseCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE))
                .setTermCurrency(query.getCurrency()).build());
        if (Objects.nonNull(rate1) && Objects.nonNull(rate2)) {
            builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
            builder.setRateChain(rate1, rate2);
            return builder.build();
        }
        throw new CurrencyConversionException(query.getBaseCurrency(), query.getCurrency(), sourceRate.getContext());
    }
}
 
Example 3
Source File: YahooAbstractRateProvider.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
private ExchangeRate createExchangeRate(ConversionQuery query,
		ExchangeRateBuilder builder, ExchangeRate sourceRate,
		ExchangeRate target) {

	if (areBothBaseCurrencies(query)) {
		builder.setFactor(DefaultNumberValue.ONE);
		return builder.build();
	} else if (BASE_CURRENCY_CODE.equals(query.getCurrency()
			.getCurrencyCode())) {
		if (Objects.isNull(sourceRate)) {
			return null;
		}

		return reverse(sourceRate);
	} else if (BASE_CURRENCY_CODE.equals(query.getBaseCurrency()
			.getCurrencyCode())) {
		return target;
	} else {

		ExchangeRate rate1 = getExchangeRate(query.toBuilder()
				.setTermCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE))
				.build());

		ExchangeRate rate2 = getExchangeRate(query.toBuilder()
				.setBaseCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE))
				.setTermCurrency(query.getCurrency()).build());

		if (Objects.nonNull(rate1) && Objects.nonNull(rate2)) {
			builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
			builder.setRateChain(rate1, rate2);
			return builder.build();
		}

		throw new CurrencyConversionException(query.getBaseCurrency(),
				query.getCurrency(), sourceRate.getContext());
	}
}
 
Example 4
Source File: LazyBoundCurrencyConversion.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
public LazyBoundCurrencyConversion(ConversionQuery conversionQuery, ExchangeRateProvider rateProvider,
                                   ConversionContext conversionContext) {

    super(conversionQuery.getCurrency(), conversionContext);
    this.conversionQuery = conversionQuery;
    this.rateProvider = rateProvider;
}
 
Example 5
Source File: ECBAbstractRateProvider.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
private ExchangeRate createExchangeRate(ConversionQuery query,
                                        ExchangeRateBuilder builder, ExchangeRate sourceRate,
                                        ExchangeRate target) {

    if (areBothBaseCurrencies(query)) {
        builder.setFactor(DefaultNumberValue.ONE);
        return builder.build();
    } else if (BASE_CURRENCY_CODE.equals(query.getCurrency().getCurrencyCode())) {
        if (Objects.isNull(sourceRate)) {
            return null;
        }
        return reverse(sourceRate);
    } else if (BASE_CURRENCY_CODE.equals(query.getBaseCurrency()
            .getCurrencyCode())) {
        return target;
    } else {

        ExchangeRate rate1 = getExchangeRate(
                query.toBuilder().setTermCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE)).build());
        ExchangeRate rate2 = getExchangeRate(
                query.toBuilder().setBaseCurrency(Monetary.getCurrency(BASE_CURRENCY_CODE))
                        .setTermCurrency(query.getCurrency()).build());
        if (Objects.nonNull(rate1) && Objects.nonNull(rate2)) {
            builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
            builder.setRateChain(rate1, rate2);
            return builder.build();
        }
        throw new CurrencyConversionException(query.getBaseCurrency(),
                query.getCurrency(), sourceRate.getContext());
    }
}
 
Example 6
Source File: IMFAbstractRateProvider.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
    try {
        if (loadLock.await(30, TimeUnit.SECONDS)) {
            if (currencyToSdr.isEmpty()) {
                return null;
            }
            if (!isAvailable(conversionQuery)) {
                return null;
            }
            CurrencyUnit base = conversionQuery.getBaseCurrency();
            CurrencyUnit term = conversionQuery.getCurrency();
            LocalDate[] times = getQueryDates(conversionQuery);
            ExchangeRate rate1 = getExchangeRate(currencyToSdr.get(base), times);
            ExchangeRate rate2 = getExchangeRate(sdrToCurrency.get(term), times);
            if (base.equals(SDR)) {
                return rate2;
            } else if (term.equals(SDR)) {
                return rate1;
            }
            if (Objects.isNull(rate1) || Objects.isNull(rate2)) {
                return null;
            }

            ConversionContext context = getExchangeContext("imf.digit.fraction");

            ExchangeRateBuilder builder =
                    new ExchangeRateBuilder(context);
            builder.setBase(base);
            builder.setTerm(term);
            builder.setFactor(multiply(rate1.getFactor(), rate2.getFactor()));
            builder.setRateChain(rate1, rate2);

            return builder.build();
        }else{
            // Lets wait for a successful load only once, then answer requests as data is present.
            loadLock.countDown();
            throw new MonetaryException("Failed to load currency conversion data: " + loadState);
        }
    }
    catch(InterruptedException e){
        throw new MonetaryException("Failed to load currency conversion data: Load task has been interrupted.", e);
    }
}