Path: blob/master/src/java.base/share/classes/java/util/Enumeration.java
41152 views
/*1* Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627/**28* An object that implements the Enumeration interface generates a29* series of elements, one at a time. Successive calls to the30* {@code nextElement} method return successive elements of the31* series.32* <p>33* For example, to print all elements of a {@code Vector<E>} <i>v</i>:34* <pre>35* for (Enumeration<E> e = v.elements(); e.hasMoreElements();)36* System.out.println(e.nextElement());</pre>37* <p>38* Methods are provided to enumerate through the elements of a39* vector, the keys of a hashtable, and the values in a hashtable.40* Enumerations are also used to specify the input streams to a41* {@code SequenceInputStream}.42*43* @apiNote44* The functionality of this interface is duplicated by the {@link Iterator}45* interface. In addition, {@code Iterator} adds an optional remove operation,46* and has shorter method names. New implementations should consider using47* {@code Iterator} in preference to {@code Enumeration}. It is possible to48* adapt an {@code Enumeration} to an {@code Iterator} by using the49* {@link #asIterator} method.50*51* @see java.util.Iterator52* @see java.io.SequenceInputStream53* @see java.util.Enumeration#nextElement()54* @see java.util.Hashtable55* @see java.util.Hashtable#elements()56* @see java.util.Hashtable#keys()57* @see java.util.Vector58* @see java.util.Vector#elements()59*60* @author Lee Boynton61* @since 1.062*/63public interface Enumeration<E> {64/**65* Tests if this enumeration contains more elements.66*67* @return {@code true} if and only if this enumeration object68* contains at least one more element to provide;69* {@code false} otherwise.70*/71boolean hasMoreElements();7273/**74* Returns the next element of this enumeration if this enumeration75* object has at least one more element to provide.76*77* @return the next element of this enumeration.78* @throws NoSuchElementException if no more elements exist.79*/80E nextElement();8182/**83* Returns an {@link Iterator} that traverses the remaining elements84* covered by this enumeration. Traversal is undefined if any methods85* are called on this enumeration after the call to {@code asIterator}.86*87* @apiNote88* This method is intended to help adapt code that produces89* {@code Enumeration} instances to code that consumes {@code Iterator}90* instances. For example, the {@link java.util.jar.JarFile#entries()91* JarFile.entries()} method returns an {@code Enumeration<JarEntry>}.92* This can be turned into an {@code Iterator}, and then the93* {@code forEachRemaining()} method can be used:94*95* <pre>{@code96* JarFile jarFile = ... ;97* jarFile.entries().asIterator().forEachRemaining(entry -> { ... });98* }</pre>99*100* (Note that there is also a {@link java.util.jar.JarFile#stream()101* JarFile.stream()} method that returns a {@code Stream} of entries,102* which may be more convenient in some cases.)103*104* @implSpec105* The default implementation returns an {@code Iterator} whose106* {@link Iterator#hasNext hasNext} method calls this Enumeration's107* {@code hasMoreElements} method, whose {@link Iterator#next next}108* method calls this Enumeration's {@code nextElement} method, and109* whose {@link Iterator#remove remove} method throws110* {@code UnsupportedOperationException}.111*112* @return an Iterator representing the remaining elements of this Enumeration113*114* @since 9115*/116default Iterator<E> asIterator() {117return new Iterator<>() {118@Override public boolean hasNext() {119return hasMoreElements();120}121@Override public E next() {122return nextElement();123}124};125}126}127128129