commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephen Colebourne <scolebou...@btopenworld.com>
Subject Re: [lang] LANG-510
Date Sun, 14 Mar 2010 13:44:07 GMT
I do fear that this discussion may be over-thinking. Most users will, I 
believe, be working with String and want a String back. Many of these 
calls may be in time-critical code, so the conversion from a 
CharSequence (even though trivial) may be a small overhead.

We also have StrBuilder for those cases where a user needs a kind of 
builder approach. Personally, I use StrBuilder instead of 
StringBuffer/StringBuilder whenever I can.

With CharSequence you need to ask if it makes sense for the operation to 
be called on an input stream or similar - because that is, as much as 
anything, what the interface was designed for.

In summary, I don't overly object to the methods being changed to take a 
CharSequence, although I do think its a waste. StringUtils should never 
return CharSequence. I also have string doubts over the utilty of a 
CharSequenceUtils.

Stephen


Gary Gregory wrote:
> Working with (trunk) StringUtils (SU) I see the following emerge: 
> 
> - In SVN already and continuing: Change StringUtils arguments from String to CharSequence
(CS).
> 
> - 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().
> 
> Thoughts?
> 
> 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
> 

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


Mime
View raw message