xom-interest AT lists.ibiblio.org
Subject: XOM API for Processing XML with Java
List archive
Re: [XOM-interest] adding an iterator() method to nodes
- From: "Christian Vest Hansen" <karmazilla AT gmail.com>
- To: "Steve Loughran" <steve.loughran AT gmail.com>
- Cc: xom-interest AT lists.ibiblio.org
- Subject: Re: [XOM-interest] adding an iterator() method to nodes
- Date: Tue, 31 Oct 2006 19:56:41 +0100
2006/10/31, Steve Loughran <steve.loughran AT gmail.com>:
On 31/10/06, Ed Davies <edavies AT nildram.co.uk> wrote:
> Steve Loughran wrote:
> > ...
> > I make my iterators implement iterable instead:
> > public Iterator<Node> iterator() {
> > return this;
> > }
> > ...
>
> Clever way to save garbage collection of a separate Iterable
> object, but shouldn't an Iterable return a new Iterator each
> time iterator() is called?
That's actually not specified according to the Javadoc:
"Returns an iterator over the elements in this list in proper sequence."
If you can do it with One Iterator to Return to Them All, then have fun.
However, just creating a new Iterator instance is probably easier.
That's precisely why I think what I am doing consistutes an abuse. :)
Not as long as you can guarentee that the returned iterator is
positioned so that it will return the first element in the collection
(if any) upon the first call to next() ;)
> I realise that's not likely to happen in your common usage
> pattern but, still, wouldn't it be safer to at least throw an
> exception if iterator() is called twice? That wouldn't guard
> against a direct cast from Iterable to Iterator, of course,
> but that would be cheating.
Good point. I could store a reference to myself in the constructor,
return that the first time and set it to null. Second time round:NPE.
On the other hand, this *would* be abuse.
First, having iterator() throw an exception is something I would file
under "Bad surprises". It is not part of the interface, and is not
even hintet as a posibility in the documentation.
Second, NPE has a special meaning. At least in my mind. It means,
directly translated: "You, the programmer, made a 'bad assumption',
aka. bug, somewhere in your code. Now please be so kind and go fix
it."
What I'de do instead is just create new instances of some Iterator
implementation in the iterator() method, and return this new instance.
And then be more concerned with the object creation happening *inside*
the foreach-loop ;)
Cheers
--
"All good software releases were accidents corrected in the next version."
-
[XOM-interest] adding an iterator() method to nodes,
Steve Loughran, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Randall R Schulz, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Steve Loughran, 10/31/2006
- Re: [XOM-interest] adding an iterator() method to nodes, Randall R Schulz, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Ed Davies, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Steve Loughran, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Christian Vest Hansen, 10/31/2006
- Re: [XOM-interest] adding an iterator() method to nodes, Michael Kay, 10/31/2006
- Re: [XOM-interest] adding an iterator() method to nodes, Steve Loughran, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Christian Vest Hansen, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Steve Loughran, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Steve Loughran, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Elliotte Harold, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Steve Loughran, 10/31/2006
-
[XOM-interest] adding NamespaceContext.getNamespaces(),
Wolfgang Hoschek, 10/31/2006
-
Re: [XOM-interest] adding NamespaceContext.getNamespaces(),
Elliotte Harold, 10/31/2006
-
Re: [XOM-interest] adding NamespaceContext.getNamespaces(),
Wolfgang Hoschek, 10/31/2006
-
Re: [XOM-interest] adding NamespaceContext.getNamespaces(),
Elliotte Harold, 10/31/2006
- Re: [XOM-interest] adding NamespaceContext.getNamespaces(), Christian Vest Hansen, 10/31/2006
- Re: [XOM-interest] adding NamespaceContext.getNamespaces(), Elliotte Harold, 10/31/2006
-
Re: [XOM-interest] adding NamespaceContext.getNamespaces(),
Elliotte Harold, 10/31/2006
-
Re: [XOM-interest] adding NamespaceContext.getNamespaces(),
Wolfgang Hoschek, 10/31/2006
-
Re: [XOM-interest] adding NamespaceContext.getNamespaces(),
Elliotte Harold, 10/31/2006
-
[XOM-interest] adding NamespaceContext.getNamespaces(),
Wolfgang Hoschek, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Steve Loughran, 10/31/2006
-
Re: [XOM-interest] adding an iterator() method to nodes,
Randall R Schulz, 10/31/2006
Archive powered by MHonArc 2.6.24.