Search in sources :

Example 1 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri by JavaMoney.

the class ExchangeRateBuilderTest method testNumberInsignificanceForRates.

@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.");
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 2 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri by JavaMoney.

the class ExchangeRateBuilderTest method shouldCreateExchangeRateFromExchangeRate.

@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());
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) Test(org.testng.annotations.Test)

Example 3 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri by JavaMoney.

the class ECBAbstractRateProvider method createExchangeRate.

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());
    }
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) CurrencyConversionException(javax.money.convert.CurrencyConversionException)

Example 4 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri by JavaMoney.

the class ECBRateReadingHandler method addRate.

/**
 * Method to add a currency exchange rate.
 *
 * @param term      the term (target) currency, mapped from EUR.
 * @param rate      The rate.
 */
void addRate(CurrencyUnit term, LocalDate localDate, Number rate) {
    RateType rateType = RateType.HISTORIC;
    ExchangeRateBuilder builder;
    if (Objects.nonNull(localDate)) {
        // TODO check/test!
        if (localDate.equals(LocalDate.now())) {
            rateType = RateType.DEFERRED;
        }
        builder = new ExchangeRateBuilder(ConversionContextBuilder.create(context, rateType).set(localDate).build());
    } else {
        builder = new ExchangeRateBuilder(ConversionContextBuilder.create(context, rateType).build());
    }
    builder.setBase(ECBHistoricRateProvider.BASE_CURRENCY);
    builder.setTerm(term);
    builder.setFactor(DefaultNumberValue.of(rate));
    ExchangeRate exchangeRate = builder.build();
    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);
}
Also used : ExchangeRateBuilder(org.javamoney.moneta.convert.ExchangeRateBuilder) ExchangeRate(javax.money.convert.ExchangeRate) RateType(javax.money.convert.RateType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri by JavaMoney.

the class DefaultExchangeRate method hashCode.

/*
     * (non-Javadoc)
     *
     * @see java.lang.Object#hashCode()
     */
@Override
public int hashCode() {
    // Numerically equal factors should produce the same hash code, so hash a normalized
    // version of the factor rather than the NumberValue object.
    BigDecimal normalizedFactor = factor.numberValue(BigDecimal.class).stripTrailingZeros();
    // The exchange rate chain includes a reference to "this" if the caller doesn't explicitly
    // set a chain in the builder, so we can't naively hash the chain or we'll get infinite
    // recursion.
    int chainHash = 0;
    for (ExchangeRate chainedRate : chain) {
        if (chainedRate == this) {
            // Use a constant to represent the presence of this object in the chain.
            chainHash = Objects.hash(chainHash, "this");
        } else {
            chainHash = Objects.hash(chainHash, chainedRate);
        }
    }
    return Objects.hash(base, conversionContext, normalizedFactor, term, chainHash);
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) BigDecimal(java.math.BigDecimal)

Aggregations

ExchangeRate (javax.money.convert.ExchangeRate)30 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)12 Test (org.testng.annotations.Test)12 BigDecimal (java.math.BigDecimal)9 CurrencyUnit (javax.money.CurrencyUnit)8 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)7 MonetaryException (javax.money.MonetaryException)6 CurrencyConversionException (javax.money.convert.CurrencyConversionException)5 LocalDate (java.time.LocalDate)4 RateType (javax.money.convert.RateType)4 Map (java.util.Map)3 InputStream (java.io.InputStream)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Objects (java.util.Objects)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2