@Override public String print(CurrencyUnit object, Locale locale) { return object.getCurrencyCode(); }
@Override public CurrencyUnit parse(String text, Locale locale) { return Monetary.getCurrency(text); }
@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyUnit currencyUnit = Monetary.getCurrency(locale); Number numberValue = this.numberFormatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); } }
@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()); }
@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); }
/** * Sets the {@link CurrencyUnit} to be used. * * @param currencyCode the currencyCode of the currency to be used, not {@code null}. The currency code * will be resolved using {@link Monetary#getCurrency(String, String...)}. * @return This factory instance, for chaining. * @throws UnknownCurrencyException if the {@code currencyCode} is not resolvable. */ default MonetaryAmountFactory<T> setCurrency(String currencyCode) { return setCurrency(Monetary.getCurrency(currencyCode)); }
@Override public MonetaryAmount apply(MonetaryAmount amount) { MonetaryRounding r = Monetary.getRounding(amount.getCurrency()); return r.apply(amount); }
@Override public String print(MonetaryAmount object, Locale locale) { return this.numberFormatter.print(object.getNumber(), locale); }
@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()); }
@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyUnit currencyUnit = Monetary.getCurrency(locale); Number numberValue = this.numberFormatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); } }
@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(CurrencyUnit object, Locale locale) { return object.getCurrencyCode(); }
@Override public CurrencyUnit parse(String text, Locale locale) { return Monetary.getCurrency(text); }
@Override public String print(MonetaryAmount object, Locale locale) { return this.numberFormatter.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()); }
@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyStyleFormatter formatter = new CurrencyStyleFormatter(); Currency currency = determineCurrency(text, locale); CurrencyUnit currencyUnit = Monetary.getCurrency(currency.getCurrencyCode()); formatter.setCurrency(currency); formatter.setPattern(this.pattern); Number numberValue = formatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); }
@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()); }
@Override public MonetaryAmount parse(String text, Locale locale) throws ParseException { CurrencyStyleFormatter formatter = new CurrencyStyleFormatter(); Currency currency = determineCurrency(text, locale); CurrencyUnit currencyUnit = Monetary.getCurrency(currency.getCurrencyCode()); formatter.setCurrency(currency); formatter.setPattern(this.pattern); Number numberValue = formatter.parse(text, locale); return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create(); }
@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 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()); }