@Override public String print(CurrencyUnit object, Locale locale) { return object.getCurrencyCode(); }
@Override public String print(CurrencyUnit object, Locale locale) { return object.getCurrencyCode(); }
@Override public String print(MonetaryAmount object, Locale locale) { CurrencyStyleFormatter formatter = new CurrencyStyleFormatter(); formatter.setCurrency(Currency.getInstance(object.getCurrency().getCurrencyCode())); formatter.setPattern(this.pattern); return formatter.print(object.getNumber(), locale); }
@Override public String print(MonetaryAmount object, Locale locale) { CurrencyStyleFormatter formatter = new CurrencyStyleFormatter(); formatter.setCurrency(Currency.getInstance(object.getCurrency().getCurrencyCode())); formatter.setPattern(this.pattern); return formatter.print(object.getNumber(), locale); }
@Test public void testAmountWithNumberFormat2() { FormattedMoneyHolder2 bean = new FormattedMoneyHolder2(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "10.50"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10.5", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testAmountWithNumberFormat3() { FormattedMoneyHolder3 bean = new FormattedMoneyHolder3(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "10%"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10%", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 0.1d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testAmountWithNumberFormat4() { FormattedMoneyHolder4 bean = new FormattedMoneyHolder4(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "010.500"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("010.500", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testAmountWithNumberFormat1() { FormattedMoneyHolder1 bean = new FormattedMoneyHolder1(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "$10.50"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("$10.50", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); LocaleContextHolder.setLocale(Locale.CANADA); binder.bind(propertyValues); LocaleContextHolder.setLocale(Locale.US); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("$10.50", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("CAD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testAmountWithNumberFormat5() { FormattedMoneyHolder5 bean = new FormattedMoneyHolder5(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "USD 10.50"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); LocaleContextHolder.setLocale(Locale.CANADA); binder.bind(propertyValues); LocaleContextHolder.setLocale(Locale.US); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testAmountAndUnit() { MoneyHolder bean = new MoneyHolder(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "USD 10.50"); propertyValues.add("unit", "USD"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount")); assertEquals("USD", binder.getBindingResult().getFieldValue("unit")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); LocaleContextHolder.setLocale(Locale.CANADA); binder.bind(propertyValues); LocaleContextHolder.setLocale(Locale.US); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount")); assertEquals("USD", binder.getBindingResult().getFieldValue("unit")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Override public String toNonNullValue(CurrencyUnit value) { return value.getCurrencyCode(); } }
/** * Registers a bew currency unit under its currency code. * * @param currencyUnit the new currency to be registered, not null. * @return any unit instance registered previously by this instance, or null. */ public static CurrencyUnit registerCurrencyUnit(CurrencyUnit currencyUnit) { Objects.requireNonNull(currencyUnit); return ConfigurableCurrencyUnitProvider.CURRENCY_UNITS.put(currencyUnit.getCurrencyCode(), currencyUnit); }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof CurrencyUnit) { CurrencyUnit other = (CurrencyUnit) obj; return Objects.equals(getCurrencyCode(), other.getCurrencyCode()); } return false; }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof CurrencyUnit) { CurrencyUnit other = (CurrencyUnit) obj; return Objects.equals(getCurrencyCode(), other.getCurrencyCode()); } return false; }
/** * Serializer to convert the given money currency into the accepted format. * @return the serializer for currency data. */ public static TypeSerializer<CurrencyUnit> ofCurrency() { return new TypeSerializer<>(v -> withQuotes(v.getCurrencyCode().toUpperCase())); }
@Override public Long toNonNullValue(MonetaryAmount value) { if (!currencyUnit.equals(value.getCurrency())) { throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency()); } return MonetaryQueries.extractMajorPart().queryFrom(value).longValue(); }
private static String identifierToSearchFor(final CountryCode country, @Nullable final String state, @Nullable final CurrencyUnit currency) { final UrlQueryBuilder urlQueryBuilder = UrlQueryBuilder.of(); urlQueryBuilder.add("country", country.getAlpha2()); Optional.ofNullable(state).ifPresent(x -> urlQueryBuilder.add("state", x)); Optional.ofNullable(currency).ifPresent(x -> urlQueryBuilder.add("currency", x.getCurrencyCode())); return urlQueryBuilder.toStringWithOptionalQuestionMark(); } }
@Override public BigDecimal toNonNullValue(MonetaryAmount value) { if (!currencyUnit.equals(value.getCurrency())) { throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency()); } return value.getNumber().numberValue(BigDecimal.class); }
@Override public BigDecimal toNonNullValue(MonetaryAmount value) { if (!currencyUnit.equals(value.getCurrency())) { throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency()); } return value.getNumber().numberValue(BigDecimal.class); }
@Override public Long toNonNullValue(MonetaryAmount value) { if (!currencyUnit.equals(value.getCurrency())) { throw new IllegalStateException("Expected currency " + currencyUnit.getCurrencyCode() + " but was " + value.getCurrency()); } return MonetaryUtil.majorPart().apply(value).getNumber().longValue(); }