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<E></tt> <i>v</i>: 23 * <pre> 24 * for (Enumeration<E> 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 * @since JDK1.0 48 */ 49 interface Enumeration(E) { 50 /** 51 * Tests if this enumeration contains more elements. 52 * 53 * @return <code>true</code> if and only if this enumeration object 54 * contains at least one more element to provide; 55 * <code>false</code> otherwise. 56 */ 57 bool hasMoreElements(); 58 59 /** 60 * Returns the next element of this enumeration if this enumeration 61 * object has at least one more element to provide. 62 * 63 * @return the next element of this enumeration. 64 * @exception NoSuchElementException if no more elements exist. 65 */ 66 E nextElement(); 67 } 68 69 70 71 /** 72 */ 73 class RangeEnumeration(T) : Enumeration!T 74 { 75 private InputRange!T _range; 76 77 this(InputRange!T range) 78 { 79 _range = range; 80 } 81 82 bool hasMoreElements() { return !_range.empty; } 83 84 /** 85 * Returns the next element of this enumeration if this enumeration 86 * object has at least one more element to provide. 87 * 88 * @return the next element of this enumeration. 89 * @exception NoSuchElementException if no more elements exist. 90 */ 91 T nextElement() { 92 T r = _range.front(); 93 _range.popFront(); 94 return r; 95 } 96 }