use of javax.money.convert.ExchangeRate in project jsr354-ri-bp 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-bp by JavaMoney.
the class AbstractCurrencyConversion method apply.
/**
* Method that converts the source {@link MonetaryAmount} to an
* {@link MonetaryAmount} based on the {@link ExchangeRate} of this
* conversion.
*
* @param amount The source amount
* @return The converted amount, never null.
* @throws CurrencyConversionException if conversion failed, or the required data is not available.
* @see #getExchangeRate(MonetaryAmount)
*/
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
if (termCurrency.equals(Objects.requireNonNull(amount).getCurrency())) {
return amount;
}
ExchangeRate rate = getExchangeRate(amount);
if (rate == null || !amount.getCurrency().equals(rate.getBaseCurrency())) {
throw new CurrencyConversionException(amount.getCurrency(), this.termCurrency, this.conversionContext);
}
NumberValue factor = rate.getFactor();
factor = roundFactor(amount, factor);
Integer scale = rate.getContext().get(KEY_SCALE, Integer.class);
if (scale == null || scale < 0) {
return amount.multiply(factor).getFactory().setCurrency(rate.getCurrency()).create();
} else {
return amount.multiply(factor).getFactory().setCurrency(rate.getCurrency()).create().with(MonetaryOperators.rounding(scale));
}
}
use of javax.money.convert.ExchangeRate in project jsr354-ri-bp by JavaMoney.
the class IMFRateProvider method loadRatesTSV.
@SuppressWarnings("unchecked")
private void loadRatesTSV(InputStream inputStream) throws IOException, ParseException {
Map<CurrencyUnit, List<ExchangeRate>> newCurrencyToSdr = new HashMap<>();
Map<CurrencyUnit, List<ExchangeRate>> newSdrToCurrency = new HashMap<>();
NumberFormat f = new DecimalFormat("#0.0000000000");
f.setGroupingUsed(false);
BufferedReader pr = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line = pr.readLine();
if (line.contains("Request Rejected")) {
throw new IOException("Request has been rejected by IMF server.");
}
// int lineType = 0;
boolean currencyToSdr = true;
// SDRs per Currency unit (2)
//
// Currency January 31, 2013 January 30, 2013 January 29, 2013
// January 28, 2013 January 25, 2013
// Euro 0.8791080000 0.8789170000 0.8742470000 0.8752180000
// 0.8768020000
// Currency units per SDR(3)
//
// Currency January 31, 2013 January 30, 2013 January 29, 2013
// January 28, 2013 January 25, 2013
// Euro 1.137520 1.137760 1.143840 1.142570 1.140510
List<LocalDate> timestamps = null;
while (line != null) {
if (line.trim().isEmpty()) {
line = pr.readLine();
continue;
}
if (line.startsWith("SDRs per Currency unit")) {
currencyToSdr = false;
line = pr.readLine();
continue;
} else if (line.startsWith("Currency units per SDR")) {
currencyToSdr = true;
line = pr.readLine();
continue;
} else if (line.startsWith("Currency")) {
timestamps = readTimestamps(line);
line = pr.readLine();
continue;
}
String[] parts = line.split("\\t");
CurrencyUnit currency = currenciesByName.get(parts[0]);
if (currency == null) {
LOGGER.finest("Uninterpretable data from IMF data feed: " + parts[0]);
line = pr.readLine();
continue;
}
Double[] values = parseValues(parts);
for (int i = 0; i < values.length; i++) {
if (values[i] == null) {
continue;
}
LocalDate fromTS = timestamps != null ? timestamps.get(i) : null;
if (fromTS == null) {
continue;
}
RateType rateType = RateType.HISTORIC;
if (fromTS.equals(LocalDate.now())) {
rateType = RateType.DEFERRED;
}
if (currencyToSdr) {
// Currency -> SDR
ExchangeRate rate = new ExchangeRateBuilder(ConversionContextBuilder.create(CONTEXT, rateType).set(fromTS).build()).setBase(currency).setTerm(SDR).setFactor(new DefaultNumberValue(1d / values[i])).build();
List<ExchangeRate> rates = newCurrencyToSdr.get(currency);
if (rates == null) {
rates = new ArrayList<>(5);
newCurrencyToSdr.put(currency, rates);
}
rates.add(rate);
} else {
// SDR -> Currency
ExchangeRate rate = new ExchangeRateBuilder(ConversionContextBuilder.create(CONTEXT, rateType).set(fromTS).set("LocalTime", fromTS.toString()).build()).setBase(SDR).setTerm(currency).setFactor(DefaultNumberValue.of(1d / values[i])).build();
List<ExchangeRate> rates = newSdrToCurrency.get(currency);
if (rates == null) {
rates = new ArrayList<>(5);
newSdrToCurrency.put(currency, rates);
}
rates.add(rate);
}
}
line = pr.readLine();
}
// Cast is save, since contained DefaultExchangeRate is Comparable!
for (List<ExchangeRate> list : newSdrToCurrency.values()) {
Collections.sort(List.class.cast(list));
}
for (List<ExchangeRate> list : newCurrencyToSdr.values()) {
Collections.sort(List.class.cast(list));
}
this.sdrToCurrency = newSdrToCurrency;
this.currencyToSdr = newCurrencyToSdr;
for (Map.Entry<CurrencyUnit, List<ExchangeRate>> entry : this.sdrToCurrency.entrySet()) {
LOGGER.finest("SDR -> " + entry.getKey().getCurrencyCode() + ": " + entry.getValue());
}
for (Map.Entry<CurrencyUnit, List<ExchangeRate>> entry : this.currencyToSdr.entrySet()) {
LOGGER.finest(entry.getKey().getCurrencyCode() + " -> SDR: " + entry.getValue());
}
}
use of javax.money.convert.ExchangeRate in project jsr354-ri 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");
}
use of javax.money.convert.ExchangeRate in project jsr354-ri 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);
}
Aggregations