jsr372-experts@javaserverfaces-spec-public.java.net

[jsr372-experts] Null safe converters/validators?

From: arjan tijms <arjan.tijms_at_gmail.com>
Date: Fri, 13 May 2016 13:06:24 +0200

Hi,

In practice, what we often do in Converters and Validators is something
like the following:

@FacesConverter("toLowerCaseConverter")
public class ToLowerCaseConverter implements Converter<String> {

@Override
public String getAsObject(FacesContext context, UIComponent component,
String value) {
if (value == null) {
return null;
}

return value.toLowerCase(getLocale());
}

@Override
public String getAsString(FacesContext context, UIComponent component,
String value) {
return value;
}

}

The null check is pretty repetitive.

What about:

@FacesConverter("toLowerCaseConverter", nullSafe= true)
public class ToLowerCaseConverter implements Converter<String> {

@Override
public String getAsObject(FacesContext context, UIComponent component,
String value) {
return value.toLowerCase(getLocale());
}

@Override
public String getAsString(FacesContext context, UIComponent component,
String value) {
return value;
}

}

The nullSafe=true would let the runtime do the == null return null thing
(for both methods, the example only shows one).

Thoughts?

Kind regards,
Arjan Tijms