< prev index next >

src/java.base/share/classes/java/util/Enumeration.java

Print this page
rev 11962 : 8072726: add adapter to convert Enumeration to Iterator
Reviewed-by: XXX

*** 1,7 **** /* ! * Copyright (c) 1994, 2005, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 1994, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 74,79 **** --- 74,134 ---- * * @return the next element of this enumeration. * @exception NoSuchElementException if no more elements exist. */ E nextElement(); + + /** + * Returns an {@link Iterator} whose elements are the same as the + * elements of this {@code Enumeration}. The results are undefined + * if this method is called more than once, or if this Enumeration's + * other methods are called after {@code asIterator} has been called. + * + * @apiNote + * This method is intended to help adapt code that produces + * {@code Enumeration} instances to code that consumes {@code Iterator} + * or {@code Iterable} instances. For example, the + * {@link java.security.PermissionCollection PermissionCollection.elements()} + * method returns an {@code Enumeration<Permission>}. This can be adapted + * for use in an enhanced-for loop as follows: + * + * <pre>{@code + * PermissionCollection pc = ... ; + * Iterable<Permission> permsIterable = () -> pc.elements().asIterator(); + * for (Permission p : permsIterable) { + * doSomethingWithPermission(p); + * } + * }</pre> + * + * A {@code Stream<Permission>} could be created from an + * {@code Enumeration<Permission>} as follows: + * + * <pre>{@code + * Stream<Permission> permsStream = StreamSupport.stream( + * Spliterators.spliteratorUnknownSize( + * pc.elements().asIterator(), + * Spliterator.ORDERED | Spliterator.IMMUTABLE), + * false); + * }</pre> + * + * @implSpec + * The returned Iterator's {@link Iterator#hasNext hasNext} method calls and returns + * the value from this Enumeration's {@code hasMoreElements} method; its + * {@link Iterator#next next} method calls and returns the value from this Enumeration's + * {@code nextElement} method; and its {@link Iterator#remove remove} method throws + * {@code UnsupportedOperationException}. + * + * @return an Iterator derived from this Enumeration + * + * @since 1.9 + */ + default Iterator<E> asIterator() { + return new Iterator<E>() { + @Override public boolean hasNext() { + return Enumeration.this.hasMoreElements(); + } + @Override public E next() { + return Enumeration.this.nextElement(); + } + }; + } }
< prev index next >