Search in sources :

Example 21 with ExchangeRate

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

the class ExchangeRateSimpleTest method equalsTest.

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

Example 22 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri-bp 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)

Example 23 with ExchangeRate

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

the class ExchangeRateBuilderTest method shouldCreateExchangeRate.

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

Example 24 with ExchangeRate

use of javax.money.convert.ExchangeRate in project jsr354-ri-bp 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 25 with ExchangeRate

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

the class ExchangeRateBuilderTest method equalsTest.

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

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