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");
}
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);
}
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);
}
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.");
}
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");
}
Aggregations