My view is that one of the main advantages of StringUtils is to avoid NPEs.
I actually thought that we'd been through all the methods and ensured that
none of them did throw a NPE.

As I sugested the method initially, I looked at what my own implementation
of this does and what my needs are. Basically I find the current
implementation fine:
containsOnly("", anything but null) returns true
containsOnly(null, anything) returns false

Stephen

----- Original Message -----
From: "Steve Downey" <[EMAIL PROTECTED]>
To: "Jakarta Commons Developers List" <[EMAIL PROTECTED]>
Sent: Wednesday, September 25, 2002 10:38 PM
Subject: Re: StringUtils.containsOnly


On Wednesday 25 September 2002 04:48 pm, Henri Yandell wrote:
> On Wed, 25 Sep 2002, Steve Downey wrote:
> > > > containsOnly(null, anything) returns false.
> > >
> > > I could follow this on the grounds that a null string couldn't
> > > possibly contain any characters, so should fail.  Still, I find the
> > > semantics confusing, since the same argument could be applied to the
> > > previous case as well.
> >
> > This should probably throw a NPE. It's not meaningful to ask if null
> > contains a character.
>
> This sounds like a possibility to me, though nothing else in StringUtils
> throws an NPE, and I'd think there would be other candidates in there.
>
Hmm? Yes, they do. For example, deleteWhiteSpace(null) will throw an NPE. I
think center(null, 100) does also.

As in the java.* classes, most occurances of NPE are not thrown explicitly,
but because of accessing a null object.

Looking more closely, the StringUtils classes will protect from NPE in cases
where java.lang.String will NPE. The IndexOf type methods. And we avoid
throwing IndexOutOfBounds exceptions in the left, mid, right methds, where
String.substring() will throw them.

Sometimes this makes sense. Providing a 'Safe' version of an API can be a
useful utility. But I'm not convinced it should be general policy.



--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to