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