commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pwomack <pwom...@papermule.co.uk>
Subject [collections] Iterator Exception behaviour
Date Thu, 08 Jan 2004 15:36:30 GMT
I note from the spec that IteratorChain (amonst others)
throws an exception if it is empty;

     Throws:
         UnsupportedOperationException - if the IteratorChain does not 
contain at least one Iterator

This results in the following unfortunate behaviour; if I
transform this code:

for(Iterator ib = a.iterator(); ib.hasNext();) {
     ObjB b = (ObjB)ib.next();
     for(Iterator ic = b.iterator(); ic.hasNext();) {
         ObjC c = (ObjC)ic.next();
         // do something with a 'C'
     }
}

into this form:

IteratorChain allC = new IteratorChain();
for(Iterator ib = a.iterator(); ib.hasNext();) {
     ObjB b = (ObjB)ib.next();
     allC.addIterator(b.iterator());
}

for(;allC.hasNext();) {
    ObjC c = (ObjC)allC.next();
    // do something with a 'C'
}

If the original 'a' is empty, the second version throws an Exception,
(since allC has no Iterators) whereas the first version correctly
does nothing.

I assert that the second version should be as equivalent
as possible to this first; in short I believe that
the Exception should not be thrown.

      BugBear


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


Mime
View raw message