Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider introducing Printer/Parser specializations of Formatter [SPR-6272] #10939

Closed
spring-projects-issues opened this issue Oct 24, 2009 · 2 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Keith Donald opened SPR-6272 and commented

Formatter is a convenient interface to implement for parsing and printing objects. However, there could be cases where you want to register specific Printer or Parser functions for certain types for more flexibility. For example, consider Joda Time DateTimeFormatter. Such a Formatter can parse/format a Joda DateTime object, which is nice and useful. However, the Joda format library can actually print any ReadableInstant (which includes DateTime) and ReadablePartial, while only being able to parse DateTime objects. In this case, it would make logical sense to have separate ReadableInstantPrinter, ReadablePartialPrinter, and DateTimeParser classes. For the current DateTimeFormatter, ReadablePartial implementations such as LocalDate must first be coersed to DateTime before they can be printed. Having a separate Printer interface that accepted a ReadablePartial would not require such a coersion step.


Affects: 3.0 RC1

@spring-projects-issues
Copy link
Collaborator Author

Keith Donald commented

Some code ideas showing formatting rules registered for a @ShortDate annotation where any @ShortDate property that is a ReadablePartial, ReadableInstant, or Long can be printed, and DateTime objects will be parsed. This will require no coersion to print; a coersion from DateTime to the target @ShortDate property type may still be required.


@spring-projects-issues
Copy link
Collaborator Author

Keith Donald commented

addFormatter(ShortDate.class).
	printer(new Printer<ReadablePartial>() {
		String print(ReadablePartial partial, Locale locale) {
			return DateTimeFormat.shortDate().withLocale(locale).print(partial);
		}
	}).
	printer(new Printer<ReadableInstant>() {
		String print(ReadableInstant instant, Locale locale) {
			return DateTimeFormat.shortDate().withLocale(locale).print(instant);
		}		
	}).
	printer(new Printer<Long>() {
		String print(Long instant, Locale locale) {
			return DateTimeFormat.shortDate().withLocale(locale).print(instant);
		}
	}).
	parser(new Parser<DateTime>() {
		DateTime parse(String printed, Locale locale) {
			return DateTimeFormat.shortDate().withLocale(locale).parse(printed);
		}
	});

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0 RC2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant