41 * Enumerations are also used to specify the input streams to a
42 * <code>SequenceInputStream</code>.
43 * <p>
44 * NOTE: The functionality of this interface is duplicated by the Iterator
45 * interface. In addition, Iterator adds an optional remove operation, and
46 * has shorter method names. New implementations should consider using
47 * Iterator in preference to Enumeration.
48 *
49 * @see java.util.Iterator
50 * @see java.io.SequenceInputStream
51 * @see java.util.Enumeration#nextElement()
52 * @see java.util.Hashtable
53 * @see java.util.Hashtable#elements()
54 * @see java.util.Hashtable#keys()
55 * @see java.util.Vector
56 * @see java.util.Vector#elements()
57 *
58 * @author Lee Boynton
59 * @since 1.0
60 */
61 public interface Enumeration<E> {
62 /**
63 * Tests if this enumeration contains more elements.
64 *
65 * @return <code>true</code> if and only if this enumeration object
66 * contains at least one more element to provide;
67 * <code>false</code> otherwise.
68 */
69 boolean hasMoreElements();
70
71 /**
72 * Returns the next element of this enumeration if this enumeration
73 * object has at least one more element to provide.
74 *
75 * @return the next element of this enumeration.
76 * @exception NoSuchElementException if no more elements exist.
77 */
78 E nextElement();
79 }
|
41 * Enumerations are also used to specify the input streams to a
42 * <code>SequenceInputStream</code>.
43 * <p>
44 * NOTE: The functionality of this interface is duplicated by the Iterator
45 * interface. In addition, Iterator adds an optional remove operation, and
46 * has shorter method names. New implementations should consider using
47 * Iterator in preference to Enumeration.
48 *
49 * @see java.util.Iterator
50 * @see java.io.SequenceInputStream
51 * @see java.util.Enumeration#nextElement()
52 * @see java.util.Hashtable
53 * @see java.util.Hashtable#elements()
54 * @see java.util.Hashtable#keys()
55 * @see java.util.Vector
56 * @see java.util.Vector#elements()
57 *
58 * @author Lee Boynton
59 * @since 1.0
60 */
61 public interface Enumeration<E> extends Iterable<E> {
62 /**
63 * Tests if this enumeration contains more elements.
64 *
65 * @return <code>true</code> if and only if this enumeration object
66 * contains at least one more element to provide;
67 * <code>false</code> otherwise.
68 */
69 boolean hasMoreElements();
70
71 /**
72 * Returns the next element of this enumeration if this enumeration
73 * object has at least one more element to provide.
74 *
75 * @return the next element of this enumeration.
76 * @exception NoSuchElementException if no more elements exist.
77 */
78 E nextElement();
79
80 /**
81 * Returns an {@link Iterator} whose elements are the same as the
82 * elements of this {@code Enumeration}. The results are undefined
83 * if this method is called more than once, or if this Enumeration's
84 * other methods are called after {@code asIterator} has been called.
85 *
86 * @apiNote
87 * This method is intended to help adapt code that produces
88 * {@code Enumeration} instances to code that consumes {@code Iterator}
89 * or {@code Iterable} instances. For example, the
90 * {@link java.security.PermissionCollection PermissionCollection.elements()}
91 * method returns an {@code Enumeration<Permission>}. This can be adapted
92 * for use in an enhanced-for loop as follows:
93 *
94 * <pre>{@code
95 * PermissionCollection pc = ... ;
96 * for (Permission p : pc.elements()) {
97 * doSomethingWithPermission(p);
98 * }
99 * }</pre>
100 *
101 * A {@code Stream<Permission>} could be created from an
102 * {@code Enumeration<Permission>} as follows:
103 *
104 * <pre>{@code
105 * Stream<Permission> permsStream = StreamSupport.stream(
106 * Spliterators.spliteratorUnknownSize(
107 * pc.elements().iterator(),
108 * Spliterator.ORDERED | Spliterator.IMMUTABLE),
109 * false);
110 * }</pre>
111 *
112 * @implSpec
113 * The returned Iterator's {@link Iterator#hasNext hasNext} method calls and returns
114 * the value from this Enumeration's {@code hasMoreElements} method; its
115 * {@link Iterator#next next} method calls and returns the value from this Enumeration's
116 * {@code nextElement} method; and its {@link Iterator#remove remove} method throws
117 * {@code UnsupportedOperationException}.
118 *
119 * @return an Iterator derived from this Enumeration
120 *
121 * @since 1.9
122 */
123 default Iterator<E> iterator() {
124 return new Iterator<E>() {
125 @Override public boolean hasNext() {
126 return Enumeration.this.hasMoreElements();
127 }
128 @Override public E next() {
129 return Enumeration.this.nextElement();
130 }
131 };
132 }
133 }
|