private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer, Parser<?> parser, Class<?>... fieldTypes) { for (Class<?> fieldType : fieldTypes) { registry.addFormatterForFieldType(fieldType, printer, parser); } }
@Override public void registerFormatters(FormatterRegistry registry) { addDateConverters(registry); registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); // In order to retain back compatibility we only register Date/Calendar // types when a user defined formatter is specified (see SPR-10105) if (this.dateFormatter != null) { registry.addFormatter(this.dateFormatter); registry.addFormatterForFieldType(Calendar.class, this.dateFormatter); } }
@Override public void addFormatterForFieldAnnotation(AnnotationFormatterFactory<? extends Annotation> annotationFormatterFactory) { Class<? extends Annotation> annotationType = getAnnotationType(annotationFormatterFactory); if (this.embeddedValueResolver != null && annotationFormatterFactory instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver); } Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes(); for (Class<?> fieldType : fieldTypes) { addConverter(new AnnotationPrinterConverter(annotationType, annotationFormatterFactory, fieldType)); addConverter(new AnnotationParserConverter(annotationType, annotationFormatterFactory, fieldType)); } }
@Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new Converter<TestBean, String>() { @Override public String convert(TestBean source) { return "converted"; } }); }
private void registerFormatters(FormattingConversionService conversionService) { if (this.formatters != null) { for (Object formatter : this.formatters) { if (formatter instanceof Formatter<?>) { conversionService.addFormatter((Formatter<?>) formatter); } else if (formatter instanceof AnnotationFormatterFactory<?>) { conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter); } else { throw new IllegalArgumentException( "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory"); } } } if (this.formatterRegistrars != null) { for (FormatterRegistrar registrar : this.formatterRegistrars) { registrar.registerFormatters(conversionService); } } }
@Override public void registerFormatters(FormatterRegistry registry) { registry.addFormatter(new TestBeanFormatter()); } }
/** * Add formatters useful for most Spring Boot applications. * @param registry the service to register default formatters with */ public static void addApplicationFormatters(FormatterRegistry registry) { registry.addFormatter(new CharArrayFormatter()); registry.addFormatter(new InetAddressFormatter()); registry.addFormatter(new IsoOffsetFormatter()); }
@Override public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.hasText(text)) { try { setValue(this.formatter.parse(text, LocaleContextHolder.getLocale())); } catch (IllegalArgumentException ex) { throw ex; } catch (Throwable ex) { throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex); } } else { setValue(null); } }
@Override public String print(MonetaryAmount object, Locale locale) { return this.numberFormatter.print(object.getNumber(), locale); }
@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 @SuppressWarnings("unchecked") public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (!sourceType.isAssignableTo(this.printerObjectType)) { source = this.conversionService.convert(source, sourceType, this.printerObjectType); } if (source == null) { return ""; } return this.printer.print(source, LocaleContextHolder.getLocale()); }
@Override public void registerFormatters(FormatterRegistry registry) { addDateConverters(registry); registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); // In order to retain back compatibility we only register Date/Calendar // types when a user defined formatter is specified (see SPR-10105) if (this.dateFormatter != null) { registry.addFormatter(this.dateFormatter); registry.addFormatterForFieldType(Calendar.class, this.dateFormatter); } }
@Override public void addFormatterForFieldAnnotation(AnnotationFormatterFactory<? extends Annotation> annotationFormatterFactory) { Class<? extends Annotation> annotationType = getAnnotationType(annotationFormatterFactory); if (this.embeddedValueResolver != null && annotationFormatterFactory instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver); } Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes(); for (Class<?> fieldType : fieldTypes) { addConverter(new AnnotationPrinterConverter(annotationType, annotationFormatterFactory, fieldType)); addConverter(new AnnotationParserConverter(annotationType, annotationFormatterFactory, fieldType)); } }
private void registerFormatters(FormattingConversionService conversionService) { if (this.formatters != null) { for (Object formatter : this.formatters) { if (formatter instanceof Formatter<?>) { conversionService.addFormatter((Formatter<?>) formatter); } else if (formatter instanceof AnnotationFormatterFactory<?>) { conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter); } else { throw new IllegalArgumentException( "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory"); } } } if (this.formatterRegistrars != null) { for (FormatterRegistrar registrar : this.formatterRegistrars) { registrar.registerFormatters(conversionService); } } }
@Override public String print(MonetaryAmount object, Locale locale) { return this.numberFormatter.print(object.getNumber(), locale); }
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer, Parser<?> parser, Class<?>... fieldTypes) { for (Class<?> fieldType : fieldTypes) { registry.addFormatterForFieldType(fieldType, printer, parser); } }
@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 @SuppressWarnings("unchecked") public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (!sourceType.isAssignableTo(this.printerObjectType)) { source = this.conversionService.convert(source, sourceType, this.printerObjectType); } if (source == null) { return ""; } return this.printer.print(source, LocaleContextHolder.getLocale()); }