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

[jsr372-experts] Re: Null safe converters/validators?

From: Josh Juneau <juneau001_at_gmail.com>
Date: Fri, 13 May 2016 06:18:56 -0500

Arjan,

Thanks for sending this idea to the group. I think it is a fantastic idea
that will certainly remove boiler plate code from Converters and
Validators, thus, reducing complexity and making them easier to
read/code/maintain. I really like the idea of simply adding the "nullSafe"
attribute to the existing annotations.

+1!


Josh Juneau
juneau001_at_gmail.com
http://jj-blogger.blogspot.com
https://www.apress.com/index.php/author/author/view/id/1866


On Fri, May 13, 2016 at 6:06 AM, arjan tijms <arjan.tijms_at_gmail.com> wrote:

> 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
>
>
>
>
>