use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.
the class PrecisionContextRoundedOperatorTest method shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney.
@Test
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() {
int scale = 4;
MathContext mathContext = new MathContext(scale, RoundingMode.HALF_EVEN);
CurrencyUnit real = Monetary.getCurrency("BRL");
MonetaryAmount money = Money.of(BigDecimal.valueOf(35.34567), real);
PrecisionContextRoundedOperator monetaryOperator = PrecisionContextRoundedOperator.of(mathContext);
MonetaryAmount result = monetaryOperator.apply(money);
assertTrue(RoundedMoney.class.isInstance(result));
assertEquals(result.getCurrency(), real);
assertEquals(result.getNumber().getPrecision(), scale);
assertEquals(BigDecimal.valueOf(35.35), result.getNumber().numberValue(BigDecimal.class));
}
use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.
the class ReciprocalOperatorTest method shouldReturnPositiveValue.
@Test
public void shouldReturnPositiveValue() {
CurrencyUnit currency = Monetary.getCurrency("EUR");
MonetaryAmount money = Money.parse("EUR 2.0");
MonetaryAmount result = operator.apply(money);
assertEquals(result.getCurrency(), currency);
assertEquals(result.getNumber().doubleValue(), 0.5);
}
use of javax.money.CurrencyUnit 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.CurrencyUnit in project jsr354-ri by JavaMoney.
the class IMFHistoricRateProvider method combine.
private Map<CurrencyUnit, List<ExchangeRate>> combine(Map<CurrencyUnit, List<ExchangeRate>> source, Map<CurrencyUnit, List<ExchangeRate>> destination) {
for (CurrencyUnit currency : source.keySet()) {
destination.putIfAbsent(currency, new ArrayList<>());
List<ExchangeRate> rates = source.get(currency);
destination.merge(currency, rates, IMFHistoricRateProvider::merge);
}
return destination;
}
use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.
the class ConfigurableCurrencyUnitProvider method getCurrencies.
/**
* Return a {@link CurrencyUnit} instances matching the given
* {@link javax.money.CurrencyContext}.
*
* @param currencyQuery the {@link javax.money.CurrencyQuery} containing the parameters determining the query. not null.
* @return the corresponding {@link CurrencyUnit}, or null, if no such unit
* is provided by this provider.
*/
public Set<CurrencyUnit> getCurrencies(CurrencyQuery currencyQuery) {
Set<CurrencyUnit> result = new HashSet<>(CURRENCY_UNITS.size());
if (currencyQuery.get(LocalDateTime.class) != null || currencyQuery.get(LocalDate.class) != null) {
return Collections.emptySet();
}
if (!currencyQuery.getCurrencyCodes().isEmpty()) {
for (String code : currencyQuery.getCurrencyCodes()) {
CurrencyUnit cu = CURRENCY_UNITS.get(code);
if (cu != null) {
result.add(cu);
}
}
return result;
}
if (!currencyQuery.getCountries().isEmpty()) {
for (Locale locale : currencyQuery.getCountries()) {
CurrencyUnit cu = CURRENCY_UNITS_BY_LOCALE.get(locale);
if (cu != null) {
result.add(cu);
}
}
return result;
}
result.addAll(CURRENCY_UNITS.values());
return result;
}
Aggregations