javax.money.convert.RateType Java Examples

The following examples show how to use javax.money.convert.RateType. 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: ReactiveKnowmExchangeProvider.java    From consensusj with Apache License 2.0 6 votes vote down vote up
private synchronized void initialize()  {
    if (!initialized) {
        log.info("initializing, calling createExchange()");
        // createExchange w/o exchangeSpecification does a remote initialization that takes approximately 1.5 seconds
        if (exchangeSpecification != null) {
            exchange = ExchangeFactory.INSTANCE.createExchange(exchangeSpecification);
        } else {
            exchange = ExchangeFactory.INSTANCE.createExchange(exchangeClassName);
        }
        log.info("initializing, getting name");
        name = exchange.getExchangeSpecification().getExchangeName();
        providerContext = ProviderContext.of(name, RateType.DEFERRED);
        marketDataService = exchange.getMarketDataService();
        initialized = true;
        log.info("initialized");
    }
    log.info("exiting");
}
 
Example #2
Source File: USFederalReserveRateReadingHandler.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
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: YahooRateReadingHandler.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
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 #4
Source File: CompoundRateProvider.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
private static ProviderContext createContext(Iterable<ExchangeRateProvider> providers) {
    Set<RateType> rateTypeSet = new HashSet<>();
    StringBuilder providerName = new StringBuilder("Compound: ");
    List<ProviderContext> childContextList = new ArrayList<>();
    for (ExchangeRateProvider exchangeRateProvider : providers) {
        childContextList.add(exchangeRateProvider.getContext());
        providerName.append(exchangeRateProvider.getContext().getProviderName());
        providerName.append(',' );
        rateTypeSet.addAll(exchangeRateProvider.getContext().getRateTypes());
    }
    providerName.setLength(providerName.length() - 1);

    ProviderContextBuilder builder = ProviderContextBuilder.of(providerName.toString(), rateTypeSet);
    builder.set(CHILD_PROVIDER_CONTEXTS_KEY, childContextList);
    return builder.build();
}
 
Example #5
Source File: KnowmExchangeRateProvider.java    From consensusj with Apache License 2.0 5 votes vote down vote up
public synchronized void initialize()  {
    if (!initialized) {
        initialized = true;
        exchange = ExchangeFactory.INSTANCE.createExchange(exchangeClassName);
        name = exchange.getExchangeSpecification().getExchangeName();
        providerContext = ProviderContext.of(name, RateType.DEFERRED);
        marketDataService = exchange.getMarketDataService();
    }
}
 
Example #6
Source File: KnowmExchangeRateProvider.java    From consensusj with Apache License 2.0 5 votes vote down vote up
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 #7
Source File: YahooAbstractRateProvider.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Override
protected ConversionContext getExchangeContext(String key) {
	int scale = getScale(key);
       if(scale < 0) {
         return ConversionContext.of(this.context.getProviderName(), RateType.HISTORIC);
       } else {
       	return ConversionContext.of(this.context.getProviderName(), RateType.HISTORIC).toBuilder().set(KEY_SCALE, scale).build();
       }
}
 
Example #8
Source File: AbstractRateProvider.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #9
Source File: AbstractRateProvider.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
protected ConversionContext getExchangeContext(String key) {
	int scale = getScale(key);
       if(scale < 0) {
         return ConversionContext.of(this.context.getProviderName(), RateType.HISTORIC);
       } else {
       	return ConversionContext.of(this.context.getProviderName(), RateType.HISTORIC).toBuilder().set(KEY_SCALE, scale).build();
       }
}
 
Example #10
Source File: IdentityRateProvider.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@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 #11
Source File: ExchangeRateSimpleTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@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 #12
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void equalsTest() {
	DefaultNumberValue factor = new DefaultNumberValue(1.1);
	DefaultNumberValue bigDecimalFactor = new DefaultNumberValue(new BigDecimal("1.1"));
	CurrencyUnit EUR = Monetary.getCurrency("EUR");
	CurrencyUnit GBP = Monetary.getCurrency("GBP");
	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 #13
Source File: IMFRateReadingHandler.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
private RateType getRateType(LocalDate fromTS) {
	RateType rateType = RateType.HISTORIC;
	if (fromTS.equals(LocalDate.now())) {
		rateType = RateType.DEFERRED;
	}
	return rateType;
}
 
Example #14
Source File: ExchangeRateBuilder.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the exchange rate type
 *
 * @param rateType the {@link javax.money.convert.RateType} contained
 */
public ExchangeRateBuilder(String provider, RateType rateType) {
    this(ConversionContext.of(provider, rateType));
}