javax.money.convert.ConversionContext Java Examples

The following examples show how to use javax.money.convert.ConversionContext. 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: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void testNumberInsignificanceForRates(){
	ExchangeRate rateFromString = new ExchangeRateBuilder(ConversionContext.HISTORIC_CONVERSION)
			.setBase(Monetary.getCurrency("USD"))
			.setTerm(Monetary.getCurrency("EUR"))
			.setFactor(DefaultNumberValue.of(new BigDecimal("1.1")))
			.build();

	ExchangeRate rateFromDouble = new ExchangeRateBuilder(ConversionContext.HISTORIC_CONVERSION)
			.setBase(Monetary.getCurrency("USD"))
			.setTerm(Monetary.getCurrency("EUR"))
			.setFactor(DefaultNumberValue.of(1.1))
			.build();

	assertEquals(rateFromDouble, rateFromString, "Rates are not equal for same factor.");
}
 
Example #2
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 #3
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 #4
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 #5
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 #6
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateExchangeRateFromExchangeRate() {
	ExchangeRate rate = new ExchangeRateBuilder(ConversionContext.ANY_CONVERSION).setBase(CURRENCY)
			.setTerm(CURRENCY).setFactor(NUMBER_FACTOR).build();

	ExchangeRate rate2 = new ExchangeRateBuilder(rate).build();

	assertEquals(rate.getContext(), rate2.getContext());
	assertEquals(rate.getBaseCurrency(), rate2.getBaseCurrency());
	assertEquals(rate.getFactor(), rate2.getFactor());
}
 
Example #7
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateExchangeRate() {
	ExchangeRate rate = new ExchangeRateBuilder(ConversionContext.ANY_CONVERSION).setBase(CURRENCY)
			.setTerm(CURRENCY).setFactor(NUMBER_FACTOR).build();

	assertEquals(rate.getContext(), ConversionContext.ANY_CONVERSION);
	assertEquals(rate.getBaseCurrency(), CURRENCY);
	assertEquals(rate.getFactor(), NUMBER_FACTOR);
}
 
Example #8
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions  = NullPointerException.class)
public void shouldReturnNPEWhenTermIsNull() {
	new ExchangeRateBuilder(ConversionContext.ANY_CONVERSION).setBase(CURRENCY)
			.setTerm(null).setFactor(NUMBER_FACTOR).build();
}
 
Example #9
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions  = NullPointerException.class)
public void shouldReturnNPEWhenConversionContextIsNull() {
	ConversionContext context = null;
	new ExchangeRateBuilder(context).setBase(CURRENCY)
			.setTerm(CURRENCY).setFactor(NUMBER_FACTOR).build();
}
 
Example #10
Source File: CurrencyConversionMock.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public ConversionContext getContext() {
    return null;
}
 
Example #11
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions  = NullPointerException.class)
public void shouldReturnNPEWhenBaseIsNull() {
	new ExchangeRateBuilder(ConversionContext.ANY_CONVERSION).setBase(null)
			.setTerm(CURRENCY).setFactor(NUMBER_FACTOR).build();
}
 
Example #12
Source File: ExchangeRateBuilderTest.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions  = NullPointerException.class)
public void shouldReturnNPEWhenFactorIsNull() {
	new ExchangeRateBuilder(ConversionContext.ANY_CONVERSION).setBase(CURRENCY)
			.setTerm(CURRENCY).setFactor(null).build();
}
 
Example #13
Source File: LazyBoundCurrencyConversion.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@Override
public CurrencyConversion with(ConversionContext conversionContext) {
    return new LazyBoundCurrencyConversion(conversionQuery, rateProvider,
            conversionContext);
}
 
Example #14
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);
    }
}
 
Example #15
Source File: KnowmExchangeRateProvider.java    From consensusj with Apache License 2.0 4 votes vote down vote up
@Override
public CurrencyConversion getCurrencyConversion(ConversionQuery conversionQuery) {
    return new LazyBoundCurrencyConversion(conversionQuery, this, ConversionContext
            .of(getContext().getProviderName(), getContext().getRateTypes().iterator().next()));
}
 
Example #16
Source File: AbstractCurrencyConversion.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
public AbstractCurrencyConversion(CurrencyUnit termCurrency, ConversionContext conversionContext) {
    Objects.requireNonNull(termCurrency);
    Objects.requireNonNull(conversionContext);
    this.termCurrency = termCurrency;
    this.conversionContext = conversionContext;
}
 
Example #17
Source File: Conversions.java    From javamoney-examples with Apache License 2.0 2 votes vote down vote up
/**
 * Evaluate the current default Conversion Context.
 *
 * @param src the source/base {@link javax.money.CurrencyUnit}, not null
 * @param tgt the target {@link javax.money.CurrencyUnit}, not null
 * @return the converted amount, not null.
 * @see javax.money.convert.MonetaryConversions
 * @see javax.money.convert.CurrencyConversion
 * @see javax.money.MonetaryOperator
 */
public ConversionContext getDefaultConversionContext(CurrencyUnit src, CurrencyUnit tgt) {
    throw new UnsupportedOperationException();
}
 
Example #18
Source File: DefaultExchangeRate.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
    * Access the {@link javax.money.convert.ConversionContext} of {@link javax.money.convert.ExchangeRate}.
    *
    * @return the conversion context, never null.
    */
   @Override
public final ConversionContext getContext() {
       return this.conversionContext;
   }
 
Example #19
Source File: ExchangeRateBuilder.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the provider to be applied.
 *
 * @param conversionContext the {@link javax.money.convert.ConversionContext}, not null.
 * @return The builder.
 */
public ExchangeRateBuilder setContext(ConversionContext conversionContext) {
    Objects.requireNonNull(conversionContext);
    this.conversionContext = conversionContext;
    return this;
}
 
Example #20
Source File: ExchangeRateBuilder.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the exchange rate type
 *
 * @param context the {@link javax.money.convert.ConversionContext} to be applied
 */
public ExchangeRateBuilder(ConversionContext context) {
    setContext(context);
}
 
Example #21
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));
}
 
Example #22
Source File: AbstractCurrencyConversion.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Access the target {@link ConversionContext} of this conversion instance.
 *
 * @return the target {@link ConversionContext}.
 */
@Override
public ConversionContext getContext() {
    return conversionContext;
}
 
Example #23
Source File: AbstractCurrencyConversion.java    From jsr354-ri with Apache License 2.0 votes vote down vote up
public abstract CurrencyConversion with(ConversionContext conversionContext);