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 15:48:57 GMT
Well, sure, I was just hoping there was a way to make match input and output types.

Gary 

> -----Original Message-----
> From: Jörg Schaible [mailto:joerg.schaible@gmx.de]
> Sent: Sunday, March 07, 2010 05:54
> To: dev@commons.apache.org
> Subject: RE: [lang] LANG-510
> 
> 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