commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jörg Schaible <joerg.schai...@gmx.de>
Subject RE: [lang] LANG-510
Date Sun, 07 Mar 2010 13:54:09 GMT
Gary Gregory wrote:

> When I replaced the current implementation of StringUtils.left(String,int)
> with:
> 
>     @SuppressWarnings("unchecked")
>     public static <T extends CharSequence> T left(T cs, int len) {
>         if (cs == null) {
>             return null;
>         }
>         if (len < 0) {
>             return (T) cs.subSequence(0, 0);
>         }
>         if (cs.length() <= len) {
>             return cs;
>         }
>         return (T) cs.subSequence(0, len);
>     }
> 
> Everything compiled, all tests passed, and no Unnecessary cast warnings
> came up (as provided by Eclipse 3.6M5)
> 
> The problem is what happens when you pass in a non-Strings, like a
> StringBuilder. The implementation of subsequence for StringBuilder returns
> a new String, not new StringBuilder.

Then why not use already proposed:

     public static String left(CharSequence str, int len) {
         if (str == null) {
             return null;
         }
         return str.subSequence(0, len).toString();
     }

??

- Jörg


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


Mime
View raw message