How should I make this (Spring) formatter implemen...
# announcements
j
How should I make this (Spring) formatter implementation support LocalDate? (nullable)
Copy code
fun localDateFormatter(): Formatter<LocalDate> {
        return object : Formatter<LocalDate> {
            override fun parse(text: String, locale: Locale): LocalDate {
                return LocalDate.parse(text, DateTimeFormatter.ISO_DATE)
            }
            override fun print(obj: LocalDate, locale: Locale): String {
                return DateTimeFormatter.ISO_DATE.format(obj)
            }
        }
    }
I can't make parse return LocalDate? no matter what
If I put
Copy code
override fun parse(text: String, locale: Locale): LocalDate? {
it gives me error Return type is LocalDate? which is not a subtype of overridden
n
LocalDate is a subtype of LocalDate?
so you need to make the type it formats nullable or handle nullability
Copy code
?: defaultDate
or throw a exception
j
Using
Copy code
override fun parse(text: String, locale: Locale): LocalDate? {
does not compile
whatever the implementation is
because changing return type LocalDate -> LocalDate? then gives compiler error
Copy code
Return type is LocalDate? which is not a subtype of overridden
n
because its for
Formatter<LocalDate>
with that signature you would need
Formatter<LocalDate?>
j
same problem
n
thats why you should isntead handle null on the parser call
LocalData.parse returns LocalDate? because the input could be junk
j
I don't understand
n
so adding elvis operator to get a default or to throw would fix that
j
because the problem is not the actual implementation but defining the nullable type
n
to me it seems like the actual problem is that the return type is non nullable but your function return null, maybe i am confused because i do not have the code in a IDE
j
Copy code
@Bean
    fun localDateFormatter(): Formatter<LocalDate?> {
        return object : Formatter<LocalDate?> {
            override fun parse(text: String, locale: Locale): LocalDate? {
                return null
            }
            override fun print(obj: LocalDate, locale: Locale): String {
                return DateTimeFormatter.ISO_DATE.format(obj)
            }
        }
    }
does not compile
"Return type is LocalDate? which is not a subtype of overridden"
n
because the generic argument is LocalDate and not LocalDate?
j
fixed, same problem
n
it could also be that formatter only takes T : Any and nullability was not considered
now the print function should take a nullable though ?
j
Formatter is Java
Copy code
public interface Formatter<T> extends Printer<T>, Parser<T> {

}
Copy code
@FunctionalInterface
public interface Printer<T> {

	/**
	 * Print the object of type T for display.
	 * @param object the instance to print
	 * @param locale the current user locale
	 * @return the printed text string
	 */
	String print(T object, Locale locale);

}
Copy code
@FunctionalInterface
public interface Parser<T> {

	/**
	 * Parse a text String to produce a T.
	 * @param text the text string
	 * @param locale the current user locale
	 * @return an instance of T
	 * @throws ParseException when a parse exception occurs in a java.text parsing library
	 * @throws IllegalArgumentException when a parse exception occurs
	 */
	T parse(String text, Locale locale) throws ParseException;

}
n
it looks like it should work, yeah
j
changing print LocalDate? give me "print overrides nothing"
n
seems broken, do you want nullable Datetime though ?
i'd just hadle it in the parser and throw exception when something is invalid