dnrajugade / guava-libraries

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

synchronized Iterator wrapper #1161

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would like Guava to include a synchronized Iterator wrapper, similar to the 
unmodifiable wrapper in com.google.common.collect.Iterators.

Original issue reported on code.google.com by g...@maginatics.com on 5 Oct 2012 at 6:02

GoogleCodeExporter commented 9 years ago
What's your use case?

Generally, you have to synchronize around an entire iteration, not just 
individual calls to hasNext() or next().  For that, a "synchronized wrapper" 
can't possibly help -- there's no alternative but to do the synchronization 
yourself.

Original comment by lowas...@google.com on 5 Oct 2012 at 6:05

GoogleCodeExporter commented 9 years ago
In my use case, I vend port numbers to tests via an Iterators.cycle(ports).  
Thus hasNext always succeeds and I want to synchronize calls to next().  One 
can imagine other variants of this, for example vending sequence numbers, 
temporary file names, etc.  One could use an AtomicInteger for some of these 
although that exposes additional mutators.

Original comment by g...@maginatics.com on 5 Oct 2012 at 6:17

GoogleCodeExporter commented 9 years ago
These almost sound like a BlockingQueue of some kind would be more appropriate 
than an Iterator, I guess?

Original comment by lowas...@google.com on 5 Oct 2012 at 6:30

GoogleCodeExporter commented 9 years ago
Since you want to hide mutators and have no need for #hasNext and #remove, I'd 
say a Supplier is more suitable. It's easy to create a synchronized supplier 
which delegates #get calls to some underlying iterator.

Original comment by stephan...@gmail.com on 5 Oct 2012 at 8:19

GoogleCodeExporter commented 9 years ago
Stephan, thank you for suggesting Supplier -- this is a better match for what I 
want.  Also Suppliers has a synchronized wrapper.

Original comment by g...@maginatics.com on 6 Oct 2012 at 12:07

GoogleCodeExporter commented 9 years ago
Marking as WorkingAsIntended.

Original comment by lowas...@google.com on 11 Oct 2012 at 6:53

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:13

GoogleCodeExporter commented 9 years ago

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