Re: List or Iterator
On Sun, 26 Jul 2009, Andreas Leitgeb wrote:
Tom Anderson <twic@urchin.earth.li> wrote:
Do you have a way of getting an Iterable interface in the mix here
somewhere? I had a related, although simpler, problem recently, and was
very happy to be able to work an Iterable in, because i could then
for-loop over it. If a DOM node can be iterated in several ways, it can't
be Iterable itself, but could you have its getChildren etc methods return
some sort of NodeSequence object which, while not necessarily a full-blown
List, was Iterable? Is there any reasonable way to combine this with
one-shotness?
In Java, the solution to this usually boils down to anonymous classes:
Iterable<Node> getChildrenIterable() {
final Iterator<Node> it= getChildrenIterator();
return new Iterable<Node>() {
public Iterator<Node> iterator() { return it; }
}
}
Right. My question, really, is is this reasonable? For instance, this:
Iterable<Node> children = foo.getChildrenIterable();
for (Node n: children) {
System.out.println(n);
}
for (Node n: children) {
System.out.println(n);
}
Will print different things in the two identical loops - all the children
the first time, and nothing the second time. Would it be better to throw
an exception from iterator() if it's called more than once? Would it be
better to do what i was thinking of:
Iterable<Node> getChildrenIterable() {
return new Iterable<Node>() {
public Iterator<Node> iterator() {
return getChildrenIterator();
}
}
}
Which *can* be called multiple times?
Would it be better not to return an Iterable, since that's misleading, and
rather return an Iterator, but to provide a utility method:
public class IteratorTools {
private IteratorTools() {}
/**
Use this as a static import. You are not expected to like the name!
*/
public static <T> Iterable<T> in(final Iterator<T> it) {
return new Iterable<T>() {
public Iterator<T> iterator() {
return it;
}
}
}
}
So you can do:
Iterator<Node> children;
for (Node n: in(children)) {
// etc
}
That is, shift the responsibility for Iterable-ifying the Iterator to the
caller?
tom
--
I know thats not really relevant but I've just typed the words and my
backspace key doesn't work. -- phorenzik