commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <>
Subject RE: [lang] LANG-510
Date Mon, 08 Mar 2010 22:15:12 GMT
Working with (trunk) StringUtils (SU) I see the following emerge: 

- In SVN already and continuing: Change StringUtils arguments from String to CharSequence

- This leads to replacing calls to String.substring(int[,int]) with calls to CharSequence.subSequence(int)

- This leads to creating a CharSequenceUtils class (in SVN now, more on this new class below)
and CharSequenceUtils.subSequence(CharSequence,int) to avoid changing "str.substring(start)"
over and over to "str.subSequence(start, str.length())". For examples, see new versions of
capitalize and uncapitalize.

- We end up using a toString() on CharSequence to return a String from StringUtil when working
with a CharSequence.

So we have StringUtils using CharSequence inputs as much as possible instead of String, which
is nice. 

The CharSequence method subSequence returns a CharSequence; though the Javadoc states "Returns
a new CharSequence that is a subsequence of this sequence.", this does not guaranteed the
return value to be the same kind of CharSequence as the receiver). Since we are after all
in a class called StringUtil, calling toString() is a must.

I propose that we create when possible the methods that are now StringUtils CharSequence methods
into CharSequenceUtils and let StringUtil call CharSequenceUtils and then do its toString()
and other String specific logic. Later we could have other CharSequence type of utils (for
CharBuffer, StringBuiler, StringBuffer, etc) that use the 'primitives' from CharSequenceUtils.
This means that for methods that are based solely on methods that are now in CharSequence,
these can be moved to CharSequenceUtils without effort (all is* methods only call CharSequence#length()
and charAt() for example and are now typed as CS, still in SU). 

We can leave @deprecateds method in SU as a nicety to avoid too much porting pain: First change
the package to lang3 then you can 'optimize' by changing call sites from SU to CSU.

As a start, I put in SVN a CharSequenceUtils (CSU) implementation for length() and subSequence().


> -----Original Message-----
> From: Jörg Schaible []
> Sent: Sunday, March 07, 2010 05:54
> To:
> 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:
> For additional commands, e-mail:

View raw message