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

Remi Forax forax at univ-mlv.fr
Sat May 16 23:02:23 UTC 2015


On 05/17/2015 12:46 AM, Claes Redestad wrote:

On 2015-05-16 23:48, Remi Forax wrote:

On 05/16/2015 06:21 PM, Claes Redestad wrote: Hi, any reason not to just have Enumeration extend Iterable and default-implement iterator()? http://cr.openjdk.java.net/~redestad/scratch/enumerable.00/ I guess there are compatibility risks I haven't thought through completely, but I think concrete classes that already implement both Enumeration and Iterable should continue to work as expected (since the concrete class' implementation of iterator always takes precedence), no? Hi Claes, yes, but there is an issue for code that uses instanceof to do something different if the object is an Enumeration or if the object is an Iterable. Hi Rémi, I guess that's a possibility. You can only retrofit an existing class/interface to implements/extends an interface/class in a compatible way only if the interface/class is newly introduced in the release by example retrofitting Closeable to implement AutoCloseable is Ok in 1.7 because AutoCloseable was introduced in 1.7. Probably due to introducing ambiguity in overload selection, come to think of it. I also realized Iterable should typically allow multiple passes over the collection/enumeration, so this wouldn't feel right. Sorry! So... I'm happy with Stuart's proposal. I would also be happy (happier?) with a stream variant: pc.elements().stream().forEach(this::doSomethingWithPermission);

We are not that far : pc.elements().asIterator().forEachRemaining(this::doSomethingWithPermission);

and having a method stream() on an Enumeration as the very same issue as having a method stream() on an Iterator. A Stream need to know more than just the elements but also characteristics like if the element can be null, are ordered, the number of elements if known, etc. These characteristics can be derived from List or Set but not from Iterator/Enumeration easily.

That's why it's better to let the user to create a stream using StreamSupport if he really want one.

and remember, prefer composition to inheritance :) ducks :) /Claes

Rémi

/Claes Rémi On 2015-05-16 02:37, 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