commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <GGreg...@seagullsoftware.com>
Subject RE: [lang] LANG-510
Date Sun, 07 Mar 2010 02:05:07 GMT
Here is another issue that I find confusing at first glance with Generics. I redefined defaultIfEmpty
in my sandbox as follows:

    public static <T extends CharSequence> T defaultIfEmpty(T str, T defaultStr) {
        return StringUtils.isEmpty(str) ? defaultStr : str;
    }

This is simple enough but I was surprised that this compiled:

assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuilder("abc"), "NULL"));

I thought that all T's should match, but they do not, the compiler works its type inference
magic to match up the call site to the method, such that both arguments are of type CharSequence.
The API as coded can return both a String and a StringBuilder depending on the value of the
input. That's the confusing part.

The question is: Is it worth this potential confusion to allow any CharSequence to be used?

In this case, the "<T extends CharSequence>" version is the same as:

    public static CharSequence defaultIfEmpty(CharSequence str, CharSequence defaultStr) {
        return StringUtils.isEmpty(str) ? defaultStr : str;
    }

Are there cases where the "<T extends CharSequence>" version is needed?

Gary 

> -----Original Message-----
> From: Henri Yandell [mailto:flamefew@gmail.com]
> Sent: Friday, March 05, 2010 01:33
> To: Commons Developers List
> Subject: [lang] LANG-510
> 
> Thinking further on moving StringUtils to CharSequence, I'd like to
> take the String left(String, int) method as an example. It depends on
> substring(int, int), so is entirely possibly to move over to
> subSequence(int, int).
> 
> Hypothetical new method:
> 
>     CharSequence left(CharSequence, int)
> 
> The downside here is that users currently storing this in a String are
> going to have to cast. Generics to the rescue.
> 
>     <T extends CharSequence> T left(T, int)
> 
> This hits two problems:
> 
> 1) EMPTY is returned when the int is less than 0; EMPTY is a String and not T.
> 2) subSequence returns CharSequence and not T.
> 
> I could add a wrapper method to make Strings nicer:
> 
>     public static String left(String str, int len) {
>         if (str == null) {
>             return null;
>         }
>         return left( (CharSequence) str, len).toString();
>     }
> 
> But that doesn't help the StringBuffer/StrBuilder/StringBuilder user;
> they still get a sucky API.
> 
> Am I missing anything obvious here, or should I give up the ghost on
> trying to take these methods to CharSequence APIs?
> 
> Hen
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message