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.

So it looks like StringUtils APIs need to be considered one at a time, and 
tested with more than one CharSequence implementation (String, StringBuilder, 
StringBuffer at least)

Gary Gregory
Senior Software Engineer
Seagull Software
email: ggreg...@seagullsoftware.com
email: ggreg...@apache.org
www.seagullsoftware.com 



> -----Original Message-----
> From: Niall Pemberton [mailto:niall.pember...@gmail.com]
> Sent: Friday, March 05, 2010 02:41
> To: Commons Developers List
> Subject: Re: [lang] LANG-510
> 
> On Fri, Mar 5, 2010 at 9:32 AM, Henri Yandell <flame...@gmail.com> wrote:
> > 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.
> 
> From CharSequence's subSequence() javadoc "if start == end  then an
> empty sequence is returned". So you could do:
> 
>      return (T)str.subSequence(0, 0);
> 
> Niall
> 
> 
> > 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-unsubscr...@commons.apache.org
> For additional commands, e-mail: dev-h...@commons.apache.org


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

Reply via email to