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-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to