Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8072726

add adapter to convert Enumeration to Iterator

    Details

      Backports

        Description

        This is primarily to support legacy code which might still provide only Enumerations.

        One can easily write one's own Iterator class that wraps an enumeration, but the library should provide this.

        An alternative is

            Collections.list(enumeration).iterator()

        but this copies out and stores all elements of the enumeration before returning the iterator.

        Once the Enumeration is converted to an Iterator, it can then be converted to a Stream by using

            StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, ...), ...);

        We will not support any means to convert Iterators or Streams to Enumeration, since Enumeration is a legacy API.

          Issue Links

            Activity

            Hide
            smarks Stuart Marks added a comment -
            A default method on Enumeration like

                public Iterator<E> asIterator()

            would do the trick. Note that this is on Enumeration to avoid adding clutter to the
            newer and still-current Iterator interface. If we think adding a default method is too risky,
            then we could add this static method to Iterator:

                public static <T> Iterator<T> fromEnumeration(Enumeration<? extends T>)

            or the dual of it on Enumeration.
            Show
            smarks Stuart Marks added a comment - A default method on Enumeration like     public Iterator<E> asIterator() would do the trick. Note that this is on Enumeration to avoid adding clutter to the newer and still-current Iterator interface. If we think adding a default method is too risky, then we could add this static method to Iterator:     public static <T> Iterator<T> fromEnumeration(Enumeration<? extends T>) or the dual of it on Enumeration.
            Hide
            briangoetz Brian Goetz added a comment -
            I strongly prefer the default method on Enumeration, as Stuart proposed. Reasons:
             - More discoverable. Users who have an Enumeration will be able to find the conversion method they want through IDE completion.
             - Puts the Enumeration cruft in Enumeration, not Iterator.

            The risk of conflict is extremely low (unlike putting default methods on Iterable.)
            Show
            briangoetz Brian Goetz added a comment - I strongly prefer the default method on Enumeration, as Stuart proposed. Reasons:  - More discoverable. Users who have an Enumeration will be able to find the conversion method they want through IDE completion.  - Puts the Enumeration cruft in Enumeration, not Iterator. The risk of conflict is extremely low (unlike putting default methods on Iterable.)
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/099bc2e05376
            User: smarks
            Date: 2015-06-05 01:30:16 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/099bc2e05376 User: smarks Date: 2015-06-05 01:30:16 +0000
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/099bc2e05376
            User: lana
            Date: 2015-06-10 21:24:34 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/099bc2e05376 User: lana Date: 2015-06-10 21:24:34 +0000

              People

              • Assignee:
                smarks Stuart Marks
                Reporter:
                smarks Stuart Marks
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: