javax.money.convert.ConversionQuery Java Examples
The following examples show how to use
javax.money.convert.ConversionQuery.
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: ECBAbstractRateProvider.java From jsr354-ri with Apache License 2.0 | 7 votes |
private RateResult findExchangeRate(ConversionQuery conversionQuery) { LocalDate[] dates = getQueryDates(conversionQuery); if (dates == null) { Comparator<LocalDate> comparator = Comparator.naturalOrder(); LocalDate date = this.rates.keySet().stream() .max(comparator) .orElseThrow(() -> new MonetaryException("There is not more recent exchange rate to rate on ECBRateProvider.")); return new RateResult(this.rates.get(date)); } else { for (LocalDate localDate : dates) { Map<String, ExchangeRate> targets = this.rates.get(localDate); if(Objects.nonNull(targets)) { return new RateResult(targets); } } String datesOnErros = Stream.of(dates).map(date -> date.format(DateTimeFormatter.ISO_LOCAL_DATE)).collect(Collectors.joining(",")); throw new MonetaryException("There is not exchange on day " + datesOnErros + " to rate to rate on ECBRateProvider."); } }
Example #2
Source File: DefaultMonetaryConversionsSingletonSpi.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Override public ExchangeRateProvider getExchangeRateProvider(ConversionQuery conversionQuery) { Collection<String> providers = getProvidersToUse(conversionQuery); List<ExchangeRateProvider> provInstances = new ArrayList<>(); for (String provName : providers) { ExchangeRateProvider prov = Optional.ofNullable( this.conversionProviders.get(provName)) .orElseThrow( () -> new MonetaryException( "Unsupported conversion/rate provider: " + provName)); provInstances.add(prov); } if (provInstances.isEmpty()) { throw new MonetaryException("No such providers: " + conversionQuery); } if (provInstances.size() == 1) { return provInstances.get(0); } return new CompoundRateProvider(provInstances); }
Example #3
Source File: IMFHistoricRateProviderTest.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Test public void shouldSetTimeInLocalDateTime() { LocalDate localDate = YearMonth.of(2014, Month.JANUARY).atDay(9); ConversionQuery conversionQuery = ConversionQueryBuilder.of() .setTermCurrency(EURO).set(localDate).build(); CurrencyConversion currencyConversion = provider .getCurrencyConversion(conversionQuery); assertNotNull(currencyConversion); MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR); MonetaryAmount result = currencyConversion.apply(money); assertEquals(result.getCurrency(), EURO); assertTrue(result.getNumber().doubleValue() > 0); }
Example #4
Source File: IMFHistoricRateProviderTest.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Test(expectedExceptions = MonetaryException.class) public void shouldReturnErrorWhenFindFromHistoricalUsingPeriod() { LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9); ConversionQuery conversionQuery = ConversionQueryBuilder.of() .setTermCurrency(EURO) .set(withDaysBetween(localDate.minusDays(1), localDate)) .build(); CurrencyConversion currencyConversion = provider .getCurrencyConversion(conversionQuery); assertNotNull(currencyConversion); MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR); currencyConversion.apply(money); fail(); }
Example #5
Source File: IMFHistoricRateProviderTest.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Test public void shouldSetTimeInLocalDateTime2() { LocalDate localDate = LocalDate.now().minusDays(90) .with(TemporalAdjusters.next(DayOfWeek.FRIDAY)); ConversionQuery conversionQuery = ConversionQueryBuilder.of() .setTermCurrency(EURO).set(localDate).build(); CurrencyConversion currencyConversion = provider .getCurrencyConversion(conversionQuery); assertNotNull(currencyConversion); MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR); MonetaryAmount result = currencyConversion.apply(money); assertEquals(result.getCurrency(), EURO); assertTrue(result.getNumber().doubleValue() > 0); }
Example #6
Source File: IMFHistoricRateProvider.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Override public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) { LocalDate[] times = getQueryDates(conversionQuery); if(Objects.isNull(times)) { return super.getExchangeRate(conversionQuery); } for (YearMonth yearMonth : Stream.of(times).map(YearMonth::from) .collect(Collectors.toSet())) { if(!cachedHistoric.contains(yearMonth)){ Map<IMFHistoricalType, InputStream> resources = IMFRemoteSearch.INSTANCE.getResources(yearMonth, getContext().get("User-Agent", String.class)); loadFromRemote(resources); cachedHistoric.add(yearMonth); } } return super.getExchangeRate(conversionQuery); }
Example #7
Source File: ECBHistoric90RateProviderTest.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Test(expectedExceptions = MonetaryException.class) public void shouldSetTimeInLocalDateTime() { LocalDate localDate = YearMonth.of(2014, Month.JANUARY).atDay(9); ConversionQuery conversionQuery = ConversionQueryBuilder.of() .setTermCurrency(EURO).set(localDate).build(); CurrencyConversion currencyConversion = provider .getCurrencyConversion(conversionQuery); assertNotNull(currencyConversion); MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR); MonetaryAmount result = currencyConversion.apply(money); assertEquals(result.getCurrency(), EURO); assertTrue(result.getNumber().doubleValue() > 0); }
Example #8
Source File: ECBHistoric90RateProviderTest.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Test(expectedExceptions = MonetaryException.class) public void shouldReturnErrorWhenFindFromHistoricalUsingPeriod() { LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9); ConversionQuery conversionQuery = ConversionQueryBuilder.of() .setTermCurrency(EURO) .set(withDaysBetween(localDate.minusDays(1), localDate)) .build(); CurrencyConversion currencyConversion = provider .getCurrencyConversion(conversionQuery); assertNotNull(currencyConversion); MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR); currencyConversion.apply(money); fail(); }
Example #9
Source File: ECBHistoric90RateProviderTest.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Test public void shouldSetTimeInLocalDateTime2() { LocalDate localDate = LocalDate.now().minusDays(90) .with(TemporalAdjusters.next(DayOfWeek.FRIDAY)); ConversionQuery conversionQuery = ConversionQueryBuilder.of() .setTermCurrency(EURO).set(localDate).build(); CurrencyConversion currencyConversion = provider .getCurrencyConversion(conversionQuery); assertNotNull(currencyConversion); MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR); MonetaryAmount result = currencyConversion.apply(money); assertEquals(result.getCurrency(), EURO); assertTrue(result.getNumber().doubleValue() > 0); }
Example #10
Source File: ECBAbstractRateProvider.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Override public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) { Objects.requireNonNull(conversionQuery); try { if (loadLock.await(30, TimeUnit.SECONDS)) { if (rates.isEmpty()) { return null; } RateResult result = findExchangeRate(conversionQuery); ExchangeRateBuilder builder = getBuilder(conversionQuery); ExchangeRate sourceRate = result.targets.get(conversionQuery.getBaseCurrency() .getCurrencyCode()); ExchangeRate target = result.targets .get(conversionQuery.getCurrency().getCurrencyCode()); return createExchangeRate(conversionQuery, builder, sourceRate, target); }else{ 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); } }
Example #11
Source File: DefaultMonetaryConversionsSingletonSpi.java From jsr354-ri with Apache License 2.0 | 6 votes |
private Collection<String> getProvidersToUse(ConversionQuery query) { List<String> providersToUse = new ArrayList<>(); List<String> providerNames = query.getProviderNames(); if (providerNames.isEmpty()) { providerNames = getDefaultProviderChain(); if (providerNames.isEmpty()) { throw new IllegalStateException("No default provider chain available."); } } for (String provider : providerNames) { ExchangeRateProvider prov = this.conversionProviders.get(provider); if (prov == null) { throw new MonetaryException("Invalid ExchangeRateProvider (not found): " + provider); } providersToUse.add(provider); } return providersToUse; }
Example #12
Source File: CompoundRateProvider.java From jsr354-ri with Apache License 2.0 | 6 votes |
@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 #13
Source File: YahooAbstractRateProvider.java From javamoney-lib with Apache License 2.0 | 6 votes |
@Override protected LocalDate[] getQueryDates(ConversionQuery query) { if (Objects.nonNull(query.get(LocalDate.class)) || Objects.nonNull(query.get(LocalDateTime.class))) { LocalDate localDate = Optional.ofNullable(query.get(LocalDate.class)).orElseGet(() -> query.get(LocalDateTime.class).toLocalDate()); return new LocalDate[]{localDate}; } else if(Objects.nonNull(query.get(LocalDate[].class))) { return query.get(LocalDate[].class); } // Return latest dates in data List<LocalDate> keys = new ArrayList<>(this.rates.keySet()); if(!keys.isEmpty()) { keys.sort(Comparator.reverseOrder()); return new LocalDate[]{keys.get(0)}; } return null; }
Example #14
Source File: YahooAbstractRateProvider.java From javamoney-lib with Apache License 2.0 | 6 votes |
@Override public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) { Objects.requireNonNull(conversionQuery); try { if (loadLock.await(30, TimeUnit.SECONDS)) { if (rates.isEmpty()) { return null; } RateResult result = findExchangeRate(conversionQuery); ExchangeRateBuilder builder = getBuilder(conversionQuery, result.date); ExchangeRate sourceRate = result.targets.get(conversionQuery.getBaseCurrency() .getCurrencyCode()); ExchangeRate target = result.targets .get(conversionQuery.getCurrency().getCurrencyCode()); return createExchangeRate(conversionQuery, builder, sourceRate, target); }else{ 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); } }
Example #15
Source File: USFederalReserveRateProvider.java From javamoney-lib with Apache License 2.0 | 6 votes |
@Override public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) { Objects.requireNonNull(conversionQuery); try { if (loadLock.await(30, TimeUnit.SECONDS)) { if (rates.isEmpty()) { return null; } RateResult result = findExchangeRate(conversionQuery); ExchangeRateBuilder builder = getBuilder(conversionQuery, result.date); ExchangeRate sourceRate = result.targets.get(conversionQuery.getBaseCurrency().getCurrencyCode()); ExchangeRate target = result.targets.get(conversionQuery.getCurrency().getCurrencyCode()); return createExchangeRate(conversionQuery, builder, sourceRate, target); }else{ 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); } }
Example #16
Source File: YahooAbstractRateProvider.java From javamoney-lib with Apache License 2.0 | 6 votes |
private RateResult findExchangeRate(ConversionQuery conversionQuery) { LocalDate[] dates = getQueryDates(conversionQuery); if (dates == null) { Comparator<LocalDate> comparator = Comparator.naturalOrder(); LocalDate date = this.rates.keySet().stream().sorted(comparator.reversed()).findFirst().orElseThrow(() -> new MonetaryException("There is not more recent exchange rate to rate on ECBRateProvider.")); return new RateResult(date, this.rates.get(date)); } else { for (LocalDate localDate : dates) { Map<String, ExchangeRate> targets = this.rates.get(localDate); if(Objects.nonNull(targets)) { return new RateResult(localDate, targets); } } String datesOnErros = Stream.of(dates).map(date -> date.format(DateTimeFormatter.ISO_LOCAL_DATE)).collect(Collectors.joining(",")); throw new MonetaryException("There is not exchange on day " + datesOnErros + " to rate to rate on ECBRateProvider."); } }
Example #17
Source File: ECBHistoric90RateProviderTest.java From jsr354-ri with Apache License 2.0 | 5 votes |
@Test(expectedExceptions = MonetaryException.class) public void shouldReturnErrorWhenDoesNotFindTheExchangeRate() { LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9); ConversionQuery conversionQuery = ConversionQueryBuilder.of() .set(localDate).setTermCurrency(EURO).build(); CurrencyConversion currencyConversion = provider .getCurrencyConversion(conversionQuery); assertNotNull(currencyConversion); MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR); currencyConversion.apply(money); fail(); }
Example #18
Source File: ECBAbstractRateProvider.java From jsr354-ri with Apache License 2.0 | 5 votes |
private ExchangeRateBuilder getBuilder(ConversionQuery query) { ExchangeRateBuilder builder = new ExchangeRateBuilder(getExchangeContext("ecb.digit.fraction")); builder.setBase(query.getBaseCurrency()); builder.setTerm(query.getCurrency()); return builder; }
Example #19
Source File: USFederalReserveRateProvider.java From javamoney-lib with Apache License 2.0 | 5 votes |
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 #20
Source File: ECBAbstractRateProvider.java From jsr354-ri with Apache License 2.0 | 5 votes |
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 #21
Source File: YahooAbstractRateProvider.java From javamoney-lib with Apache License 2.0 | 5 votes |
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 #22
Source File: ExchangeRateProviderMock.java From jsr354-ri with Apache License 2.0 | 5 votes |
@Override public CurrencyConversion getCurrencyConversion( ConversionQuery conversionQuery) { CurrencyUnit currencyUnit = conversionQuery.get("Query.termCurrency", CurrencyUnit.class); return new CurrencyConversionMock(currencyUnit); }
Example #23
Source File: USFederalReserveRateProvider.java From javamoney-lib with Apache License 2.0 | 5 votes |
private RateResult findExchangeRate(ConversionQuery conversionQuery) { LocalDate[] dates = getQueryDates(conversionQuery); if (dates == null) { Comparator<LocalDate> comparator = Comparator.naturalOrder(); LocalDate date = this.rates .keySet() .stream() .sorted(comparator.reversed()) .findFirst() .orElseThrow( () -> new MonetaryException("There is not more recent exchange rate to rate on " + getDataId())); return new RateResult(date, this.rates.get(date)); } else { for (LocalDate localDate : dates) { Map<String, ExchangeRate> targets = this.rates.get(localDate); if (Objects.nonNull(targets)) { return new RateResult(localDate, targets); } } String datesOnErros = Stream.of(dates).map(date -> date.format(DateTimeFormatter.ISO_LOCAL_DATE)) .collect(Collectors.joining(",")); throw new MonetaryException("There is not exchange on day " + datesOnErros + " to rate to rate on " + getDataId() + "."); } }
Example #24
Source File: DefaultMonetaryConversionsSingletonSpi.java From jsr354-ri with Apache License 2.0 | 5 votes |
@Override public boolean isConversionAvailable(ConversionQuery conversionQuery) { try { if (isExchangeRateProviderAvailable(conversionQuery)) { return getExchangeRateProvider(conversionQuery).getCurrencyConversion(conversionQuery) != null; } } catch (Exception e) { LOG.log(Level.FINEST, "Error during availability check for conversion: " + conversionQuery, e); } return false; }
Example #25
Source File: IMFHistoricRateProviderTest.java From jsr354-ri with Apache License 2.0 | 5 votes |
@Test(expectedExceptions = MonetaryException.class) public void shouldReturnErrorWhenDoesNotFindTheExchangeRate() { LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9); ConversionQuery conversionQuery = ConversionQueryBuilder.of() .set(localDate).setTermCurrency(EURO).build(); CurrencyConversion currencyConversion = provider .getCurrencyConversion(conversionQuery); assertNotNull(currencyConversion); MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR); currencyConversion.apply(money); fail(); }
Example #26
Source File: IdentityRateProvider.java From jsr354-ri with Apache License 2.0 | 5 votes |
@Override public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) { if (conversionQuery.getBaseCurrency().getCurrencyCode().equals(conversionQuery.getCurrency().getCurrencyCode())) { ExchangeRateBuilder builder = new ExchangeRateBuilder(getContext().getProviderName(), RateType.OTHER) .setBase(conversionQuery.getBaseCurrency()); builder.setTerm(conversionQuery.getCurrency()); builder.setFactor(DefaultNumberValue.of(BigDecimal.ONE)); return builder.build(); } return null; }
Example #27
Source File: LazyBoundCurrencyConversion.java From jsr354-ri with Apache License 2.0 | 5 votes |
public LazyBoundCurrencyConversion(ConversionQuery conversionQuery, ExchangeRateProvider rateProvider, ConversionContext conversionContext) { super(conversionQuery.getCurrency(), conversionContext); this.conversionQuery = conversionQuery; this.rateProvider = rateProvider; }
Example #28
Source File: YahooAbstractRateProvider.java From javamoney-lib with Apache License 2.0 | 5 votes |
private ExchangeRateBuilder getBuilder(ConversionQuery query, LocalDate localDate) { ExchangeRateBuilder builder = new ExchangeRateBuilder(getExchangeContext(DIGIT_FRACTION_KEY)); builder.setBase(query.getBaseCurrency()); builder.setTerm(query.getCurrency()); return builder; }
Example #29
Source File: AbstractRateProvider.java From jsr354-ri with Apache License 2.0 | 5 votes |
protected LocalDate[] getQueryDates(ConversionQuery query) { if (Objects.nonNull(query.get(LocalDate.class)) || Objects.nonNull(query.get(LocalDateTime.class))) { LocalDate localDate = Optional.ofNullable(query.get(LocalDate.class)).orElseGet(() -> query.get(LocalDateTime.class).toLocalDate()); return new LocalDate[]{localDate}; } else if(Objects.nonNull(query.get(LocalDate[].class))) { return query.get(LocalDate[].class); } return null; }
Example #30
Source File: AbstractRateProvider.java From jsr354-ri with Apache License 2.0 | 5 votes |
@Override public CurrencyConversion getCurrencyConversion(ConversionQuery conversionQuery) { if (getContext().getRateTypes().size() == 1) { return new LazyBoundCurrencyConversion(conversionQuery, this, ConversionContext .of(getContext().getProviderName(), getContext().getRateTypes().iterator().next())); } return new LazyBoundCurrencyConversion(conversionQuery, this, ConversionContext.of(getContext().getProviderName(), RateType.ANY)); }