1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.collection.Enumeration;
13 
14 import std.range;
15 
16 /**
17  * An object that implements the Enumeration interface generates a
18  * series of elements, one at a time. Successive calls to the
19  * <code>nextElement</code> method return successive elements of the
20  * series.
21  * <p>
22  * For example, to print all elements of a <tt>Vector&lt;E&gt;</tt> <i>v</i>:
23  * <pre>
24  *   for (Enumeration&lt;E&gt; e = v.elements(); e.hasMoreElements();)
25  *       System.out.println(e.nextElement());</pre>
26  * <p>
27  * Methods are provided to enumerate through the elements of a
28  * vector, the keys of a hashtable, and the values in a hashtable.
29  * Enumerations are also used to specify the input streams to a
30  * <code>SequenceInputStream</code>.
31  * <p>
32  * NOTE: The functionality of this interface is duplicated by the Iterator
33  * interface.  In addition, Iterator adds an optional remove operation, and
34  * has shorter method names.  New implementations should consider using
35  * Iterator in preference to Enumeration.
36  *
37  * @see     hunt.collection.Iterator
38  * @see     java.io.SequenceInputStream
39  * @see     java.util.Enumeration#nextElement()
40  * @see     java.util.Hashtable
41  * @see     java.util.Hashtable#elements()
42  * @see     java.util.Hashtable#keys()
43  * @see     java.util.Vector
44  * @see     java.util.Vector#elements()
45  *
46  * @author  Lee Boynton
47  */
48 interface Enumeration(E) {
49     /**
50      * Tests if this enumeration contains more elements.
51      *
52      * @return  <code>true</code> if and only if this enumeration object
53      *           contains at least one more element to provide;
54      *          <code>false</code> otherwise.
55      */
56     bool hasMoreElements();
57 
58     /**
59      * Returns the next element of this enumeration if this enumeration
60      * object has at least one more element to provide.
61      *
62      * @return     the next element of this enumeration.
63      * @exception  NoSuchElementException  if no more elements exist.
64      */
65     E nextElement();
66 }
67 
68 
69 
70 /**
71 */
72 class RangeEnumeration(T) : Enumeration!T
73 {
74     private InputRange!T _range;
75 
76     this(InputRange!T range)
77     {
78         _range = range;
79     }
80 
81     bool hasMoreElements() { return !_range.empty; }
82 
83     /**
84      * Returns the next element of this enumeration if this enumeration
85      * object has at least one more element to provide.
86      *
87      * @return     the next element of this enumeration.
88      * @exception  NoSuchElementException  if no more elements exist.
89      */
90     T nextElement() {
91         T r = _range.front();
92         _range.popFront();
93         return r;
94     }
95 }