org.javamoney.moneta.convert.ExchangeRateBuilder Java Examples
The following examples show how to use
org.javamoney.moneta.convert.ExchangeRateBuilder.
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: 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 #2
Source File: USFederalReserveRateReadingHandler.java From javamoney-lib with Apache License 2.0 | 6 votes |
private void addRate(CurrencyUnit term, LocalDate localDate, Number rate, boolean inverse) { RateType rateType = RateType.HISTORIC; ExchangeRateBuilder builder = new ExchangeRateBuilder(ConversionContextBuilder.create(context, rateType).set(localDate).build()); builder.setBase(inverse ? term : USFederalReserveRateProvider.BASE_CURRENCY); builder.setTerm(inverse ? USFederalReserveRateProvider.BASE_CURRENCY : term); builder.setFactor(DefaultNumberValue.of(rate)); ExchangeRate exchangeRate = builder.build(); if(inverse) { exchangeRate = USFederalReserveRateProvider.reverse(exchangeRate); } Map<String, ExchangeRate> rateMap = this.historicRates.get(localDate); if (Objects.isNull(rateMap)) { synchronized (this.historicRates) { rateMap = Optional.ofNullable(this.historicRates.get(localDate)).orElse(new ConcurrentHashMap<>()); this.historicRates.putIfAbsent(localDate, rateMap); } } rateMap.put(term.getCurrencyCode(), exchangeRate); }
Example #3
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 #4
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 #5
Source File: YahooRateReadingHandler.java From javamoney-lib with Apache License 2.0 | 6 votes |
private void addRate(YahooQuoteItemInformation information) { final ExchangeRateBuilder builder = new ExchangeRateBuilder( ConversionContextBuilder.create(context, RateType.DEFERRED).build()); builder.setBase(YahooAbstractRateProvider.BASE_CURRENCY); builder.setTerm(information.getCurrency()); builder.setFactor(DefaultNumberValue.of(information.getValue())); final ExchangeRate exchangeRate = builder.build(); Map<String, ExchangeRate> rateMap = this.excangeRates.get(information.getLocalDate()); if (Objects.isNull(rateMap)) { synchronized (this.excangeRates) { rateMap = Optional.ofNullable(this.excangeRates.get(information.getLocalDate())).orElse(new ConcurrentHashMap<>()); this.excangeRates.putIfAbsent(information.getLocalDate(), rateMap); } } rateMap.put(information.getCurrency().getCurrencyCode(), exchangeRate); }
Example #6
Source File: YahooAbstractRateProvider.java From javamoney-lib with Apache License 2.0 | 5 votes |
private ExchangeRate reverse(ExchangeRate rate) { if (Objects.isNull(rate)) { throw new IllegalArgumentException("Rate null is not reversible."); } return new ExchangeRateBuilder(rate).setRate(rate).setBase(rate.getCurrency()) .setTerm(rate.getBaseCurrency()).setContext(getExchangeContext(DIGIT_FRACTION_KEY)) .setFactor(divide(DefaultNumberValue.ONE, rate.getFactor(), MathContext.DECIMAL64)).build(); }
Example #7
Source File: ECBAbstractRateProvider.java From jsr354-ri with Apache License 2.0 | 5 votes |
private ExchangeRate reverse(ExchangeRate rate) { if (Objects.isNull(rate)) { throw new IllegalArgumentException("Rate null is not reversible."); } return new ExchangeRateBuilder(rate).setRate(rate).setBase(rate.getCurrency()).setTerm(rate.getBaseCurrency()) .setFactor(divide(DefaultNumberValue.ONE, rate.getFactor(), MathContext.DECIMAL64)).build(); }
Example #8
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 #9
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 #10
Source File: ExchangeRateSimpleTest.java From jsr354-ri with Apache License 2.0 | 5 votes |
@Test public void equalsTest() { DefaultNumberValue factor = new DefaultNumberValue(1.1); DefaultNumberValue bigDecimalFactor = new DefaultNumberValue(new BigDecimal("1.1")); ExchangeRate rate1 = new ExchangeRateBuilder("myprovider", RateType.ANY) .setBase(EUR) .setTerm(GBP) .setFactor(factor) .build(); ExchangeRate rate2 = new ExchangeRateBuilder("myprovider", RateType.ANY) .setBase(EUR) .setTerm(GBP) .setFactor(factor) .build(); ExchangeRate rate3 = new ExchangeRateBuilder("myprovider", RateType.ANY) .setBase(EUR) .setTerm(GBP) .setFactor(bigDecimalFactor) .build(); assertEquals(rate1, rate2, "Rates with same factor"); assertEquals(rate1, rate3, "Rates with numerically equal factor"); assertEquals(rate1.hashCode(), rate2.hashCode(), "Hashes with same factor"); assertEquals(rate1.hashCode(), rate3.hashCode(), "Hashes with numerically equal factor"); }
Example #11
Source File: IdentityRateProvider.java From jsr354-ri with Apache License 2.0 | 5 votes |
@Override public ExchangeRate getReversed(ExchangeRate rate) { if (rate.getContext().getProviderName().equals(CONTEXT.getProviderName())) { return new ExchangeRateBuilder(rate.getContext()).setTerm(rate.getBaseCurrency()) .setBase(rate.getCurrency()).setFactor(new DefaultNumberValue(BigDecimal.ONE)).build(); } return null; }
Example #12
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 #13
Source File: KnowmExchangeRateProvider.java From consensusj with Apache License 2.0 | 5 votes |
private ExchangeRate buildExchangeRate(CurrencyUnitPair pair, Ticker ticker) { return new ExchangeRateBuilder(name, RateType.DEFERRED) .setBase(pair.getBase()) .setTerm(pair.getTarget()) .setFactor(DefaultNumberValue.of(ticker.getLast())) .build(); }
Example #14
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 #15
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 #16
Source File: USFederalReserveRateProvider.java From javamoney-lib with Apache License 2.0 | 5 votes |
protected static ExchangeRate reverse(ExchangeRate rate) { if (Objects.isNull(rate)) { throw new IllegalArgumentException("Rate null is not reversible."); } return new ExchangeRateBuilder(rate).setRate(rate).setBase(rate.getCurrency()).setTerm(rate.getBaseCurrency()) .setFactor(divide(DefaultNumberValue.ONE, rate.getFactor(), MathContext.DECIMAL64)).build(); }
Example #17
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 #18
Source File: USFederalReserveRateProvider.java From javamoney-lib with Apache License 2.0 | 4 votes |
private ExchangeRateBuilder getBuilder(ConversionQuery query, LocalDate localDate) { ExchangeRateBuilder builder = new ExchangeRateBuilder(getExchangeContext("frb.digit.fraction")); builder.setBase(query.getBaseCurrency()); builder.setTerm(query.getCurrency()); return builder; }
Example #19
Source File: IMFAbstractRateProvider.java From jsr354-ri with Apache License 2.0 | 4 votes |
@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); } }