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

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

From: Stephan Knitelius <stephan_at_knitelius.com>
Date: Fri, 13 May 2016 11:21:28 +0000

+100 just had the pleasure today.

On Fri, 13 May 2016 at 13:19 Josh Juneau <juneau001_at_gmail.com> wrote:

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