Adam Lally wrote:
On Wed, Mar 19, 2008 at 1:19 PM, Thilo Goetz <[EMAIL PROTECTED]> wrote:
<snip/>
 public Object next() {
   if (hasNext()) {
     return next;
   }
   throw new NoSuchElementException();
 }

 where the actual work happens in hasNext().  The user may or may not
 call hasNext(), doesn't matter.  Of course in the implementation of
 hasNext(), I have to make sure to remember that I have advanced the
 iterator.  Sometimes it works better that way, for internal reasons.


Well, I think you need something slightly more complicated in next(),
to avoid having hasNext() advance the iterator every time it is
called, which wouldn't be right.  Just something like this would work
though:

 public Object next() {
   if (hasNext()) {
     Object result = this.next;
     this.next = null;
     return result;
   }
   throw new NoSuchElementException();
 }

And then in hasNext() you check if this.next != null and return true
without advancing.

You're right, I just shouldn't type code into email.  It's buggy
about 50% of the time, which makes it useless ;-)


 I have read the documentation again, and I think it's fine.  The
 bit about distributing the processing makes perfect sense to me.
 So does the part about the calling sequence, but then it wouldn't
 occur to me to write an iterator whose hasNext() method may only be
 called once before next() is called.  If you think that's necessary,
 we could add a note to indicate that hasNext() may be called more
 than once.


I do agree with Eddie and Burn that we could and should do a better
job in the documentation, to educate people who might not be so
familiar with how the Iterator interface defines hasNext/next.

If you say so.  I would just like to point out that our documentation
is already so verbose in places, trying hand-hold everybody, that it
is becoming very hard to find the important information.  One
place where this is particularly striking is the installation guide,
but there are others.  Just put in a link or something so it doesn't
detract from the important stuff.


-Adam

Reply via email to