rzel / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Iterators/Iterable Enhancement/Addition request #287

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am undertaking some re-factoring in my simulation code (the library has
done wonders at reducing clutter and improving performance, thanks!), and
found myself frequently confronted with intermediate wrapping of
factories-with-memory into an Iterable to make use of the various methods
that have Iterable arguments.

So, I extracted an abstract Generator class, which implements
Iterable+Iterator, and requires the client only override a generate()
method (see attached, very short).

Basically, this extends the Collections.nCopies() behavior to take a
factory-with-memory, by using the newX(Iterable o) methods with a Generator
argument.

The other thing is that for some code I have an Iterator instead of an
Iterable; wrapping that for use with the Iterable-ready methods is a little
tedious - an Iterables/Iterators class method to convert between the two
would be very useful.  Perhaps I've just missed that somewhere else.

Original issue reported on code.google.com by blank...@gmail.com on 31 Oct 2009 at 11:26

Attachments:

GoogleCodeExporter commented 9 years ago
Your Generator class is similar to the Collection Library class 
AbstractIterator.
Both simplify the task of building an iterator.

We've discussed the rejected the idea of having a method that converts an 
Iterator
into a Iterable, for the following reason. If you call iterable.iterator() 
twice on
the same iterable, distinct iterators should be returned. Returning the same 
iterator
or having the second iterator() call fail violates the Iterable contract.

Original comment by jared.l....@gmail.com on 2 Nov 2009 at 12:05

GoogleCodeExporter commented 9 years ago
Oh wow, guess I should have read the javadocs more thoroughly.

I see the point on the Iterator->Iterable conversion; slurping an entire 
iterator
into some backing list isn't a desirable option either, I suppose.

Original comment by blank...@gmail.com on 2 Nov 2009 at 12:55

GoogleCodeExporter commented 9 years ago
Copying an iterator into a list is totally reasonable. Google Collection Library
includes Lists.newArrayList(Iterator) and ImmutableList.copyOf(Iterator) 
methods for
that purpose.

Original comment by jared.l....@gmail.com on 2 Nov 2009 at 1:18

GoogleCodeExporter commented 9 years ago
Ah, I was offering that as the library solution for converting an iterator to
iterable.  Once slurped into the backing list, independent and non-failing 
iterators
could be returned from it.

Original comment by blank...@gmail.com on 2 Nov 2009 at 1:48

GoogleCodeExporter commented 9 years ago
Right, this is what we recommend Lists.newArrayList() for.

Original comment by kevin...@gmail.com on 2 Nov 2009 at 4:22