@Override public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) { return configureFormatterFrom(annotation); }
@Override public String print(Number number, Locale locale) { return getNumberFormat(locale).format(number); }
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) { String pattern = resolveEmbeddedValue(annotation.pattern()); if (StringUtils.hasLength(pattern)) { return new NumberStyleFormatter(pattern); } else { Style style = annotation.style(); if (style == Style.CURRENCY) { return new CurrencyStyleFormatter(); } else if (style == Style.PERCENT) { return new PercentStyleFormatter(); } else { return new NumberStyleFormatter(); } } }
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) { String pattern = resolveEmbeddedValue(annotation.pattern()); if (StringUtils.hasLength(pattern)) { return new PatternDecoratingFormatter(pattern); } else { Style style = annotation.style(); if (style == Style.NUMBER) { return new NumberDecoratingFormatter(new NumberStyleFormatter()); } else if (style == Style.PERCENT) { return new NumberDecoratingFormatter(new PercentStyleFormatter()); } else { return new NumberDecoratingFormatter(new CurrencyStyleFormatter()); } } }
@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); }
@Before public void setUp() { DefaultConversionService.addDefaultConverters(conversionService); conversionService.setEmbeddedValueResolver(new StringValueResolver() { @Override public String resolveStringValue(String strVal) { if ("${pattern}".equals(strVal)) { return "#,##.00"; } else { return strVal; } } }); conversionService.addFormatterForFieldType(Number.class, new NumberStyleFormatter()); conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); LocaleContextHolder.setLocale(Locale.US); binder = new DataBinder(new TestBean()); binder.setConversionService(conversionService); }
@Test(expected = ParseException.class) public void parseBogusValue() throws ParseException { formatter.parse("bogus", Locale.US); }
@Test public void parseValueDefaultRoundDown() throws ParseException { this.formatter.setRoundingMode(RoundingMode.DOWN); assertEquals(new BigDecimal("23.56"), formatter.parse("$23.567", Locale.US)); }
@Test public void formatValue() { assertEquals("$23.00", formatter.print(new BigDecimal("23"), Locale.US)); }
@Override public BigDecimal parse(String text, Locale locale) throws ParseException { BigDecimal decimal = (BigDecimal) super.parse(text, locale); if (this.roundingMode != null) { decimal = decimal.setScale(this.fractionDigits, this.roundingMode); } else { decimal = decimal.setScale(this.fractionDigits); } return decimal; }
@Test(expected = ParseException.class) public void parsePercentValueNotLenientFailure() throws ParseException { formatter.parse("23.56bogus", Locale.US); }
@Test(expected = ParseException.class) public void parsePercentValueNotLenientFailure() throws ParseException { formatter.parse("23.56%bogus", Locale.US); }
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) { String pattern = resolveEmbeddedValue(annotation.pattern()); if (StringUtils.hasLength(pattern)) { return new NumberStyleFormatter(pattern); } else { Style style = annotation.style(); if (style == Style.CURRENCY) { return new CurrencyStyleFormatter(); } else if (style == Style.PERCENT) { return new PercentStyleFormatter(); } else { return new NumberStyleFormatter(); } } }
@Override public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) { return configureFormatterFrom(annotation); }
@Test(expected = ParseException.class) public void parseValueNotLenientFailure() throws ParseException { formatter.parse("$23.56bogus", Locale.US); }
@Override public BigDecimal parse(String text, Locale locale) throws ParseException { BigDecimal decimal = (BigDecimal) super.parse(text, locale); if (this.roundingMode != null) { decimal = decimal.setScale(this.fractionDigits, this.roundingMode); } else { decimal = decimal.setScale(this.fractionDigits); } return decimal; }
@Test(expected = ParseException.class) public void parseBogusValue() throws ParseException { formatter.parse("bogus", Locale.US); }
@Test(expected = ParseException.class) public void parseBogusValue() throws ParseException { formatter.parse("bogus", Locale.US); }
@Override public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) { return configureFormatterFrom(annotation); }
@Override public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) { return configureFormatterFrom(annotation); }