DaveAKing / guava-libraries

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

Add function to convert Iterable to Supplier<Iterator> and vice versa #1511

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

There are lot of useful helper functions for suppliers including wrapping 
synchronization etc... Thus I almost prefer Supplier<Iterator<T>> over 
Iteratable<T> in certain situations.

So I wonder if others think it would be useful to have two static methods in 
Iterables such that you can convert back and forth from Supplier<Iterator<T>> 
<=====> Iteratable<T> .

Original issue reported on code.google.com by adam.g...@snaphop.com on 20 Aug 2013 at 6:16

GoogleCodeExporter commented 9 years ago
Most of the Supplier methods (memoization and constant suppliers) could be 
trouble for stateful objects like Iterators. Synchronization should be safe, 
though I'm unsure when it would be sufficient to synchronize Iterator creation 
but not the next/hasNext calls themselves. Can you go into more detail about 
this or about any other Supplier methods that you'd use with Iterator?

Original comment by cpov...@google.com on 20 Aug 2013 at 6:22

GoogleCodeExporter commented 9 years ago
This reminds me of 
https://code.google.com/p/guava-libraries/wiki/IdeaGraveyard#A_method_to_view_an
_iterator_as_an_iterable

Original comment by kak@google.com on 20 Aug 2013 at 6:25

GoogleCodeExporter commented 9 years ago
I don't have the original code in front of me and it looks like Iterables now 
has far more methods than I last looked.

That being said I think the reason Supplier<Iterator<>> was used is ironically 
because of the Supplier interface not making any assumptions where as the 
Iterable interface it is assumed that a collection is generally backing it. An 
InputSupplier is probably a better fit (even though an IOException is not 
thrown).

Original comment by adam.g...@snaphop.com on 20 Aug 2013 at 6:48

GoogleCodeExporter commented 9 years ago
I see. If it's something lightweight, I wouldn't let the lack of an underlying 
collection keep you from using Iterable. If it's heavier, then you may be 
interested in issue 973, which proposes a heavyweight, Iterator-like class 
(possibly with its own Iterable/Supplier-like interface). We don't expect to 
get to it anytime soon, though.

Original comment by cpov...@google.com on 20 Aug 2013 at 6:56

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08