yangxu998 / guava-libraries

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

Add an optional Predicate argument to Iterators.consumingIterator (same for Iterables) #645

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I think it's quite a valid case to want to remove elements from an iterator 
based on a predicate and propagate them for further handling.

For example I have an Iterable containing A:s and B:s. I want to first process 
all A:s and then all B:s. If an Exception is thrown in the process I want to be 
able to resume later (or list all unprocessed elements). 

Original issue reported on code.google.com by andreas....@gmail.com on 14 Jun 2011 at 3:19

GoogleCodeExporter commented 9 years ago
Unless I'm misunderstanding you, you can just compose consuming* and filter:

  Iterable<A> filteredConsuming = Iterables.consumingIterable(
      Iterables.filter(iterable, predicate));

Original comment by cgdec...@gmail.com on 14 Jun 2011 at 3:43

GoogleCodeExporter commented 9 years ago
No, because filter returns UnmodifiableIterator. I guess it has to do with 
hasNext advancing the unfiltered iterator and closing the window to issue a 
remove. It's strange that Iterator doesn't specify whether a remove is valid 
after a call to hasNext.

Original comment by andreas....@gmail.com on 14 Jun 2011 at 4:06

GoogleCodeExporter commented 9 years ago
Oh, right... stupid me.

Original comment by cgdec...@gmail.com on 14 Jun 2011 at 4:14

GoogleCodeExporter commented 9 years ago
consumingIterator() is already a fairly niche utility as it is, so I don't 
think there's a good place for variations on it in Guava.

Original comment by kevinb@google.com on 13 Jul 2011 at 6:40

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

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

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

GoogleCodeExporter commented 9 years ago

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