swlnet / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Utility methods 'isEmpty' and 'size' of Iterables and Iterators utility classes should be null safe #278

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently the 'isEmpty' and 'size' utility methods of the Iterables and
Iterators utility classes do not allow a null value as its parameter (it
will implicitly cause a NullPointerException).

I'm currently in the process of replacing (Apache) 'Commons Collections'
code to 'Google Collections'.
And I would like to replace the call to the 'Commons Collection' utility
method 'CollectionUtils.isEmpty(Collection)' to
'Iterables.isEmpty(Iterables)' without the need for an additional (verbose)
prepended "not null" check.

Also 'size' should allow null safe, since a lot of developers will tent to
write 'Iterators.size(aIterable) == 0' instead of
'Iterators.isEmpty(aIterable)'

Original issue reported on code.google.com by emil.van...@logica.com on 27 Oct 2009 at 10:17

GoogleCodeExporter commented 9 years ago
We have a strong philosophical difference.  We have opposed these kinds of
null-tolerant APIs for years.  To our minds, 'null' is not an empty Iterable; 
it's
'null', and no one should ever be passing it around as if it were an empty 
Iterable.

You should work to banish null from your codebase, or get used to the constant
checking, or continue using libraries like the Apache one.

As a side point, no one should ever use "Iterables.size() == 0" in place of
Iterables.isEmpty()!

Original comment by kevin...@gmail.com on 27 Oct 2009 at 1:54

GoogleCodeExporter commented 9 years ago
I would have to concur with Kevin. Additionally, GCL is not going to be close a 
1-1 replacement for commons-
collections. If you merely replace all apache calls with GCL calls with similar 
functions, your code will almost 
certainly break.

Besides the one philosophical difference that Kevin mentions, there are others. 
Another is that most every thing 
commons-collections returns is a new detached collection, while most everything 
GCL returns is lazy if possible. 
There's a big difference, and this happens to be my personal biggest pet peeve 
with the apache project.

Original comment by ray.a.co...@gmail.com on 28 Oct 2009 at 10:03

GoogleCodeExporter commented 9 years ago
Kevin, if you're not handling null inputs, then what benefit does isEmpty() 
provide?  

In other words, why should I call Iterables.isEmpty(iterator) instead of 
iter.iterator.hasNext(), if not for an easy way to avoid null checks?

Original comment by mark.jef...@gmail.com on 29 Aug 2012 at 12:42