RFR(s): 8072726: add adapter to convert Enumeration to Iterator

Stuart Marks stuart.marks at oracle.com
Sun May 17 00:53:41 UTC 2015

On 5/16/15 3:00 PM, Remi Forax wrote:
> Hi Stuart,
> this change is pretty cool,

Hi Rémi, thanks for looking at this.

> In the javadoc of Iterator instead of:
>    Iterable<Permission> permsIterable = () -> pc.elements().asIterator();
> one can write:
>    Iterable<Permission> permsIterable = pc.elements()::asIterator;

Ah, I carefully tailored that example to capture "pc" instead of the result of 
calling pc.elements(). With the latter, if iterator() is called a second time, 
the two Iterator instances will share the same underlying Enumeration and 
probably interfere with each other. (Maybe I'll add a note to the example 
explaining that.)

> and I wonder if an example with NetworkInterface.getNetworkInterfaces() is not
> better,
> I'm not sure a lot of people have to play with permissions outside people of
> this list.

Yeah I had to look for a long time to find a good example of Enumeration in the 
JDK libraries. One problem with NetworkInterface.getNetworkInterfaces() is that 
it's declared to throw SocketException, which is checked, making it clumsy to 
use within a lambda. There are other possibilities (e.g., getSubInterfaces()) 
but that's pretty obscure too. Although the permissions stuff is pretty obscure 
too, they're pretty easy to get, either by getting one from a class' 
ProtectionDomain, or creating one yourself.

> and for the implementation of asIterator, I think the code can be written like
> this:
>     default Iterator<E> asIterator() {
>          return new Iterator<>() {
>              @Override public boolean hasNext() {
>                  return hasMoreElements();
>              }
>              @Override public E next() {
>                  return nextElement();
>              }
>          };
>      }
> using diamond inference on inner-classes (I think the patch that allows that was
> integrated) and making calls to Enumeration.this implicit.

Right, I think I wrote my initial prototype before this compiler change went in! 
Good catch on the Enumeration.this too; I'll make these updates.


> regards,
> Rémi
> On 05/16/2015 02:37 AM, Stuart Marks wrote:
>> Hi all,
>> Please review this small API enhancement to add a default method
>> "asIterator()" to Enumeration that converts it into an Iterator.
>> Webrev:
>>     http://cr.openjdk.java.net/~smarks/reviews/8072726/webrev.0/
>> Bug:
>>     https://bugs.openjdk.java.net/browse/JDK-8072726
>> Thanks,
>> s'marks

More information about the core-libs-dev mailing list