MedwynLiang / google-collections

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

Add Iterables.fromIterator() #227

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Iterables is missing some methods found in Iterators, such as
Iterators.forEnumeration(). Instead of having to keep these classes in
sync, why not simply provide add Iterables.fromIterator(Iterator)?

Original issue reported on code.google.com by gili.tza...@gmail.com on 1 Sep 2009 at 6:08

GoogleCodeExporter commented 9 years ago
Here is my contribution :)

    /**
     * Wraps an Iterator inside an Iterable.
     *
     * @param <T> the iterator type
     * @param iterator the iterator
     * @return the Iterable wrapper
     */
    private <T> Iterable<T> forIterator(final Iterator<T> iterator)
    {
        return new Iterable<T>()
        {
            @Override
            public Iterator<T> iterator()
            {
                return iterator;
            }
        };
    }

now code sniplets like: Iterable.forIterator(Iterators.forEnumeration(e)) work.

Note I renamed the method from Iterables.fromIterator() to 
Iterables.forIterator().
If you don't like this RFE, please add Iterables.forEnumeration() -- and other
missing methods -- instead.

Original comment by gili.tza...@gmail.com on 1 Sep 2009 at 6:19

GoogleCodeExporter commented 9 years ago
There's a problem with an Iterables.fromIterator() method. If you call 
iterator()
multiple times you'll get the same iterator. That keeps you from iterating 
across the
elements twice, which is something people expect an iterable to support.

Generating an Iterable from an Enumeration would lead to the same issue.

Original comment by jared.l....@gmail.com on 2 Sep 2009 at 1:03

GoogleCodeExporter commented 9 years ago
Iterators and Iterables are not the same thing, and to view one as the other is
madness. This will get a proper treatment in the FAQ when I update the FAQ. 

Original comment by kevin...@gmail.com on 2 Sep 2009 at 4:28

GoogleCodeExporter commented 9 years ago
Jared, I don't see the problem with an Iterator or Iterable view of an 
Enumeration.
Users understand that you can only "walk" this object once, and constructing 
multiple
Iterators or Iterables to the same object will not magically transport you to 
the
beginning (where does it imply otherwise in the Javadoc anyway?). All I expect 
to be
able to do is:

for (Foo foo: Iterable.forEnumeration(e))
{
  // do stuff
}

What's wrong with that?

Original comment by gili.tza...@gmail.com on 2 Sep 2009 at 5:35

GoogleCodeExporter commented 9 years ago
I've dedicated about 34 hours of my life already to endlessly repeated 
explanations of 
this issue spread out over the last four years, so please give me a chance to 
write it 
up properly for the faq.  The issue has been debated to death and settled long 
ago.

Original comment by kevin...@gmail.com on 2 Sep 2009 at 5:43

GoogleCodeExporter commented 9 years ago
No problem. Please post a link to the FAQ entry when it's ready. Thanks :)

Original comment by gili.tza...@gmail.com on 2 Sep 2009 at 9:46

GoogleCodeExporter commented 9 years ago
Looking forward to see that FAQ entry as well.

I also think the forIterator and forEnumeration methods would be extremely 
useful when 
writing for loops. The generated Iterator could simply throw 
IllegalStateException on 
subsequent calls to iterator().

While I agree that such an interable is limited, but perfectly valid in for 
loops where 
it will be mostly used, I cannot see how it breaks the Iterable contract.

Original comment by marius.s...@gmail.com on 1 Dec 2009 at 6:06

GoogleCodeExporter commented 9 years ago
Now that version 1.0 is out you have plenty of time to write up that FAQ entry 
you
were mentioning :) Please post a link when it's ready.

Original comment by gili.tza...@gmail.com on 6 Jan 2010 at 1:22

GoogleCodeExporter commented 9 years ago
3 months later, still no FAQ entry...

Original comment by gili.tza...@gmail.com on 11 Mar 2010 at 2:43