In the current CVS HEAD, you can do
IteratorUtils.toArray(Iterator).length or IteratorUtils.toList(Iterator).size()
But, your suggestion may be a good addition also.
Also, I think that the method definitions you've provided will all cause infinite loops -- hasNext() will always return true if you don't call next(). We'd need to find a way to copy the iterator so we don't modify it's state when we count the elements.
Anyone else have an opinion?
Lee Crawford wrote:
I'd like to propose the following simple additions to the
o.a.c.c.IteratorUtils
class.
The following is a simple convenience method for counting the number of
elements
in an iteration, something that I find myself recreating somewhat
frequently:
public static
int countElements (final Iterator iter) {
int cnt = 0; while (iter.hasNext ()) { cnt ++; }
return cnt; }
There isn't an EnumeratorUtils present that might contain an equivalent:
public static
int countElements (final Enumerator enum) {
int cnt = 0; while (enum.hasNext ()) { cnt ++; }
return cnt; }
Or the slightly less performant:
public static
int countElements (final Enumerator enum) {
return IteratorUtils.countElements (asIterator (enum)); }
Thoughts?
--lee
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
